本文主要写curl的用法
安装的话根据正常的系统提供的方法安装,ubuntu就是apt install,centos就是yum install
这里演示在windows下的Pycharm演示
第一种是用os的popen方法,不仅可以获取结果,还可以用readlines或者read方法 储存结果,在 linux中效果比较好,在windows中因为编码的问题无法显示,如果大家有办法解决,记得给我 留言哦
import os
xx=os.popen('curl www.baidu.com').readlines()
第二种是subprocess的call方法
from subprocess import call
xx=call('curl www.baidu.com'.split())
print(xx)
这个方法只能控制台显示运行的结果,运行的返回值是数字。
第三种 是subprocess的getoutput方法
from subprocess import getoutput
xx=getoutput('curl www.baidu.com')
也是有返回数据的,但是同样在windows下编码有问题
所以 如果需要储存结果的话,建议在linux下运行第一种和第三种方法代码,这里在 windows下的Pycharm中用第二种方法演示,演示的目标是httpbin.org这个专门做测试 的网站
call('curl httpbin.org')
设置用户代理user-agent
call(“curl -A ‘Chrome’ httpbin.org”)
只返回头信息
call("curl -A 'Chrome' --head httpbin.org")
或者
call("curl -A 'Chrome' -I httpbin.org")
如果是小写的I,则会既有头,又有网页数据
跟随重定向请求call("curl -iL https://baidu.com")
-L即为跟随重定向 请求,加i参数只是为了更方便查看信息
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 161 100 161 0 0 72 0 0:00:02 0:00:02 --:--:-- 72
HTTP/1.1 302 Moved Temporarily
Server: bfe/1.0.8.18
Date: Wed, 29 Sep 2021 06:18:08 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://www.baidu.com/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Wed, 29 Sep 2021 06:18:08 GMT
Etag: "588604f8-94d"
Last-Modified: Mon, 23 Jan 2017 13:28:24 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
百度一下,你就知道 新闻 hao123 地图 视频 贴吧 更多产品 关于百度 about Baidu
©2017 Baidu 使用百度前必读 意见反馈 京ICP证030173号
100 2381 100 2381 0 0 1040 0 0:00:02 0:00:02 --:--:-- 1040 选择http方法这里演示的是post动作,-F传递参数
call('curl -X POST -F a=1 -F b=2 httpbin.org/post')
以post方法发送参数
call('curl -d x=1&b=2 httpbin.org/post')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 443 100 436 100 7 795 12 --:--:-- --:--:-- --:--:-- 806
{
"args": {},
"data": "",
"files": {},
"form": {
"b": "2",
"x": "1"
},
"headers": {
"Accept": "*/*",
"Content-Length": "7",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.65.3",
"X-Amzn-Trace-Id": "Root=1-615405fa-187e2dce08292a9b37083f41"
},
"json": null,
"origin": "223.66.194.217",
"url": "http://httpbin.org/post"
}
-d 后面的参数还可以是文件,文件内是 x=1&b=2样式的字符串
接@文件名即可
call('curl -d @abc httpbin.org/post')
下载 文件
以服务器文件名命名,大写的O
call('curl -O httpbin.org/image/jpeg')
以自己的名字命名,小写的o
call('curl -o xx.jpg httpbin.org/image/jpeg')
下载的是这样的一张图片
前一种文件没有后缀,需要自己重命名后打开,名字是服务器上的jpeg,位置都在代码的当前文件夹里面
就是设置headers中的参数
不同的接受数据类型获得的文件是不一样的
call('curl -o image.png -H "accept:image/png" http://httpbin.org/image')
call('curl -o image.webp -H "accept:image/webp" http://httpbin.org/image')
call('curl -o image.jpg -H "accept:image/jpeg" http://httpbin.org/image')
设置头信息在 http://httpbin.org/get网址测试会更明显,返回的json数据可以清晰的看到变化
设置前
st='''curl http://httpbin.org/get''' call(st)
设置后
st='''curl -H "accept:image/png" -A "Chrome" http://httpbin.org/get''' call(st)设置cookie
call('curl -b a=test http://httpbin.org/cookies')
允许发起不安全的ssl请求
有些网站ssl证书不是授信的,加-k参数
不显示其他无关信息-s参数
显示连接过程中的所有信息-v参数



