2021-09-29 python中curl的使用和curl的语法

本文主要写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=/

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
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参数
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amoor123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值