curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
1.不带任何参数请求,就是GET请求
$ curl https://www.example.com
2.-A参数,指定代理标头
User-agent相当于身份标识,代表浏览器标识,网络爬虫默认的用户标头为:Python-urllib/3.6
这样我们进行爬虫,我们浏览器可以对其禁止就好,我们可以改成浏览器的标头借身份证
下面命令会移除访问google的代理表头,为空
$ curl -A '' https://google.com
3.-H参数指定标头
$ curl -H 'User-Agent: php/1.0' https://google.com
4.-b参数向服务器发送Cookie
$ curl -b 'foo=bar' https://google.com
$ curl -b cookies.txt https://www.google.com
5.-c参数读取文件,并将服务器响应的呢容写道文件中
$ curl -c cookies.txt https://www.google.com
6.-d参数用户发送POST请求体的数据体
$ curl -d'login=emma&password=123'-X POST https://google.com/login
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login
7.-H参数添加HTTP标头
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
8.-o参数将服务器的响应保存为文件,类似于wget命令
$ curl -o example.html https://www.example.com
9.-k参数跳过SSL检测
$ curl -k https://www.example.com
wget -b https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
2 继续在后台运行,pid 为 1463。