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)#多长时间报错,就是怕爬太多他们反映慢
r.encoding='utf-8'#这个话不能忘REQUEST用的不是UTF-8