首页 > 其他 > 详细

py爬虫 —— py爬虫requests

时间:2019-11-05 19:50:31      阅读:83      评论:0      收藏:0      [点我收藏+]

安装requests库

pip3 install requests

使用 Requests 发送网络请求非常简单。

一开始要导入 Requests 模块:

 

import requests

 

然后,尝试获取某个网页。本例子中,我们来获取 CSDN 的一篇博客 :

r = requests.get("https://blog.csdn.net/acer12138")

现在,我们有一个名为 r 的 Response 对象。我们可以从这个对象中获取所有我们想要的信息。

Requests 简便的 API 意味着所有 HTTP 请求类型都是显而易见的。例如,你可以这样发送一个 HTTP POST 请求:

r = requests.post(http://httpbin.org/post, data = {key:value})

Requests对HTTP资源的7种操作

requests.put() #更新整个网页
requests.get() #得到整个网页
requests.post() #向网页尾添加信息
requests.head() #得到网页的概要
requests.patch() #修改部分网页
requests.delete() #删除网页的信息

request操作

requests.request(method,url,**kwargs)

method:新建Request对象要使用的HTTP方法,包括:GET,POST,PUT,DELETE等
url:新建Request对象的URL链接
**kwargs:13个控制访问的可选参数

(1)params:字典或字节序列,作为参数增加到url中

import requests
url = http://www.baidu.com
data = {"key" : "value"}
r = requests.request(get ,url ,params = data)
print(r.url)
#结果是http://www.baidu.com/?key=valu

(2)data:字典、字节序列,文件,作为Request对象body的内容

#字典
dataDicValue={key1:value1,key2:value2}
r=requests.request(GET,http://www.baidu.com,data=dataDicValue)
#字节序列
dataStrValue=test
r=requests.request(GET,http://www.baidu.com,data=dataStrValue)
#文件
dataFileValue={file:open(test.csv,rb)}
r=requests.request(POST,http://www.baidu.com,data=dataFileValue)

(3)json:JSON格式的数据,作为Request对象body的内容

jsonValue={key1:value1,key2:value2}
r=requests.request(POST,http://www.baidu.com,json=jsonValue)

(4)headers:字典格式,HTTP请求头,作为Request对象Header的内容

headerValue={user-agent: Chrome/10}
r=requests.request(POST,http://www.baidu.com,headers=headerValue)
print(r.headers)
#{‘Accept-Ranges‘: ‘bytes‘, ‘Cache-Control‘: ‘max-age=86400‘, ‘Connection‘: ‘Keep-Alive‘, ‘Content-Encoding‘: ‘gzip‘, ‘Content-Length‘: ‘4867‘, ‘Content-Type‘: ‘text/html‘, ‘Date‘: ‘Wed, 15 Aug 2018 10:22:42 GMT‘, ‘Etag‘: ‘"3dcd-560eb5cea6700"‘, ‘Expires‘: ‘Thu, 16 Aug 2018 10:22:42 GMT‘, ‘Last-Modified‘: ‘Fri, 22 Dec 2017 10:34:36 GMT‘, ‘P3p‘: ‘CP=" OTI DSP COR IVA OUR IND COM "‘, ‘Server‘: ‘Apache‘, ‘Set-Cookie‘: ‘BAIDUID=6AE11C63536CF472F9B403B1BE467161:FG=1; expires=Thu, 15-Aug-19 10:22:42 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1‘, ‘Vary‘: ‘Accept-Encoding,User-Agent‘}

(5)cookies:字典或CookieJar,Request中的cookie

#字典
cookieDicValue={key1:value1,key2:value2}
r=requests.request(POST,http://www.baidu.com,cookies=cookieDicValue)
print(r.cookies)
#<RequestsCookieJar[]>
#CookieJar
cookiesJarValue=cookiejar.CookieJar()
r=requests.request(POST,http://www.baidu.com,cookies=cookiesJarValue)
print(r.cookies)
#<RequestsCookieJar[]>

(6)files:字典,形式为{filename: fileobject},表示要上传的多个部分

filesValue = {file: open(test.csv, rb)}
r = requests.request(POST, http://www.baidu.com, files=filesValue)

(7)auth:Auth 句柄或 (user, pass) 元组

#Auth
authBasicValue=HTTPBasicAuth(username,password)
r = requests.request(POST, http://www.baidu.com, auth=authBasicValue)
#(user,pass)元组
authTupValue=(username,password)
r=requests.request(POST,http://www.baidu.com,auth=authTupValue)

(8)timeout:等待服务器数据的超时限制,是一个浮点数,或是一个(connect timeout, read timeout) 元组

#浮点数(单位秒)
timeoutFloatValue=12
r=requests.request(POST,http://www.baidu.com,timeout=timeoutFloatValue)
#(connect timeout, read timeout) 元组;
#connect timeout:建立连接所用的时间;适用于网络状况正常的情况下,两端所用的时间
#readtimeout:建立连接后从服务器读取到可用资源所用的时间;正常情况下,当我们发出请求时可以收到请求的结果,也就是页面上展示的内容,但是当网络状况很差的时候,就会出现页面上无法展示出内容的情况。另外当我们使用爬虫或者其他全自动的程序时,无法判断当前的网络状况是否良好,此时就有了ReadTimeout的用武之地了,通过设置ReadTimeout参数
#注意:在我们使用需要设置这两项参数的服务或程序时,应该对两项参数一起设置。 一般而言两项参数的数值可以设置成一样的,但可以把ReadTimeout设置的长一点,ConnectTimeout可以相对比较短,这是源于我们的网络状况一般较为稳定,连接时很少出现问题,但是读取时因为数据下载时的网络波动,出状况的可能性更大一些。
timeoutTupValue=(10,25)
r=requests.request(POST,http://www.baidu.com,timeout=timeoutTupValue)

(9)allow_redirects:True/False,默认为Ture,重定向开关,为True时表示允许POST/PUT/DELETE方法重定向

#禁止重定向
r=requests.request(POST,http://www.baidu.com,allow_redirects=False)

(10)proxies:字典类型,用于将协议映射为代理的URL

#以下是根据不同协议选择不同代理,也可以使用list+随机方式使用多个代理地址访问URL
poxiesValue={http:xxx.xxx.xxx.xxx:xx,https:xxx.xxx.xxx.xxx:xx}
r=requests.request(POST,http://www.baidu.com,poxies=poxiesValue)

(11)verify:True/False,默认为True,认证SSL证书开关;为True时会验证SSL证书,也可以使用cert参数提供一个CA_BUNDLE路径;为False时,忽略SSL验证

#忽略SSL验证
r=requests.request(POST,http://www.baidu.com,varify=False)
(12)stream:True/False,默认为True,获取body立即下载开关,为False会立即下载响应头和响应体;为True时会先下载响应头,当Reponse调用content方法时才下载响应体
# stream为True时下载步骤
#stream设置为True,直接下载响应头
r=requests.request(POST,http://www.baidu.com,stream=True)
#调用content方法下载响应体
r.content

(13)cert:为字符串时应是 SSL 客户端证书文件的路径(.pem格式,文件路径包含密钥和证书),如果是元组,就应该是一个(‘cert’, ‘key’) 二元值对

#文件路径
requests.request(POST,http://www.baidu.com,cert=/True_path/server.pem)
#元组
certTupValue=(/value/value.cert,/value/key)
requests.request(POST,http://www.baidu.com,cert=certTupValue)

来源

py爬虫 —— py爬虫requests

原文:https://www.cnblogs.com/Nlifea/p/11799997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!