curl命令参数及用法详解

洼地云 ai-quyi.png

curl是常用的命令行工具,用来请求 Web 服务器。其本身含义为客户端(client) URL工具。

参数及含义

  • 不带参数

功能就是发送一个get请求,并将结果输出到终端。Also see :

  • -o, --output <file>

将输出写入到文件。可以使用{}[]变量抓取多个网页文档。See also: -o, --output <file>

  • -A, --user-agent <name>

指定发送到HTTP服务器的User-Agent字符串,字符串如果包含空格,则需要使用单引号''包含。See also: -A, --user-agent <name>

  • -L, --location

如果服务器告知请求地址已经移到新地址,使用这个参数,会跳转到对应的新地址。 See also: -L, –location

用法指南

  1. curl 地址 -o 文件名

命令:

curl "https://{www,fanyi}.baidu.com" -o "file_#1.txt"

含义:

  • -o表示保存结果到文件,这是相当于wget命令;
  • #1表示第一个变量,即{www,fanyi},最终以变量值替代#1

示例如下:

[michael@centos7 curl]$ curl "https://{www,fanyi}.baidu.com" -o "file_#1.txt"

[1/2]: https://www.baidu.com --> file_www.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2443  100  2443    0     0   5810      0 --:--:-- --:--:-- --:--:--  5816

[2/2]: https://fanyi.baidu.com --> file_fanyi.txt
100  143k    0  143k    0     0   103k      0 --:--:--  0:00:01 --:--:--  122k
[michael@centos7 curl]$ ll
total 148
-rw-r--r--. 1 michael www 146810 Sep  7 01:59 file_fanyi.txt
-rw-r--r--. 1 michael www   2443 Sep  7 01:59 file_www.txt
[michael@centos7 curl]$ 
  1. curl -A User-Agent值 地址

命令:

curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://www.baidu.com

扩展:

移除User-Agent标头

curl -A '' https://google.com

更改User-Agent标头

curl -H 'User-Agent: php/1.0' https://google.com

参考文章:

  1. Curl Document-the man page
  2. curl网站开发指南
  3. curl 的用法指南
赞(0)
未经允许禁止转载:优米格 » curl命令参数及用法详解

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册