首页 > 其他 > 详细

requests第三方库使用 抓取

时间:2019-10-16 14:34:17      阅读:108      评论:0      收藏:0      [点我收藏+]
import requests
r=requests.get(‘http://httpbin.org/get‘)
r.text#内容
r.json()#直接变成json格式
r.status_code#200
r.reason#ok
r=requests.post(‘http://httpbin.org/post‘,{‘a‘:‘1‘});print(r.json())#post参数传递
r=requests.get(‘http://httpbin.org/get‘,{‘a‘:‘1‘,"b":‘2‘});print(r.json())#get传参数
headers={"User-Agent":
             ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘}
r=requests.get(‘http://httpbin.org/headers‘,{‘a‘:‘1‘,"b":‘2‘},headers=headers);print(r.text)#把headers传入request    自定义headers请求
cookies=dict(userid=‘xupanfeng‘,token=‘xuxuxu‘);r=requests.get(‘http://httpbin.org/cookies‘,cookies=cookies);print(r.json())#带COKKIES的请求
r=requests.get(‘http://httpbin.org/basic-auth/xiaoxu/123456‘,auth=(‘xiaoxu‘,‘xiaoxu‘));print(r.text);#AUTH认证
S=requests.Session();S.get(‘http://httpbin.org/cookies/set/userid/123456‘);r=S.get(‘http://httpbin.org/cookies‘);print(r.json());#设置COOKES,得到COOKIES
bad_r=requests.get(‘http://httpbin.org/status/404‘);print(bad_r.status_code);#404   因为没有内容所的404应该是让他出错所以,可以用
bad_r.raise_for_status()#可以让程序出现异常
s=requests.Session();r=s.get(‘http://httpbin.org/cookies/set/xu/123‘);print(r.json())#用SESSION创建的会话访问,得到的结果是有COOKIES的
requests.get("http://httpbin.org/ip",proxies={‘http‘:‘http://iguye.com:41801‘})#使用代理访问
requests.get(‘http://httpbin.org/delay/4‘,timeout=5)#多长时间报错,就是怕爬太多他们反映慢

 

requests第三方库使用 抓取

原文:https://www.cnblogs.com/xupanfeng/p/11685199.html

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