1.GET请求
import requests
data={
'name'='gremey',
'age'=22
}
headers={
'User-Agent':'Mo.....
}
r=requests.get("http://httpbin.org/get",params=data,headers=headers)
print(type(r.text))
print(r.json())
print(type(r.jion()))
保存二进制数据
import requests
r=requests.get("http://github.com/favicon.ico")
with open('favicon.ico','wb') as f:
f.write(r.content)
2.POST请求
import requests
data={'name':'germey','age':'22'}
r=requests.post("http://httpbin.org/post",data=data)
print(type(r.status_code),r.status_code)
print(type(r.headers),r.headers)
print(type(r.cookies),r.cookies)
print(type(r.url),r.url)
print(type(r.history),r.history)
print(r.text)
3.文件上传
import requests
files={'file':open('fabicon.ico','rb')}
r=requests.post("http://httpbin.org/post",files=files)
print(r.text)
4.Cookies
获取Cookies:
import requests
r=requests.get("http://www.baidu.com")
print(r.cookies)
for key,value in r.cookies.items():
print(key+'='+value)
5.会话维持
import requests
s=requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
r=s.get('http://httpbin.org/cookies')
print(r.text)
6.SSL证书验证
import requests
#from requests.packages import urllib3
#urllib3.disable_warnings()
#logging.captureWarnings(True)
response=requests.get('http://www.12306.cn',verify=False)
response=requests.get('http://www.12306.cn',cert=('/path/server.crt','/path/key'))
print(response.status_code)
7.代理设置
大规模爬某些网站的时候,网站可能会要客户端的认证,爬取数据量大的时候甚至会导致IP被封。这个时候就可以使用代理来解决这个问题,利用requests库发送请求中携带有代理的信息。
import requests
proxies={
"http":"http://10.10.1.10:3128"
"https":"https://10.10.1.10:1080"
}
requests.get("https://www.taobao.com",proxies=proxies)
或者使用SOCKS协议的代理,但需要在环境中安装socks这个库,pip install ‘requests[socks]’
import requests
proxies={
"http":"socks5://user:password@host:post"
"https":"socks5://user:password@host:post"
}
requests.get("https://www.taobao.com",proxies=proxies,timeout=(5,11,30))
print(r.status_code)
8.身份认证
requests自带身份认证功能,示例如下:
import requests
from requests.auth import HTTPBasicAuth
r=request.get('http://localhost:5000',auth=HTTPBasicAuth('username','password'))
#(auth=('username','password'))
print(r.status_code)
OAuth认证,需要安装oauth包,pip install request_oauthlib
import requests
from requests_oauthlib import OAuth1
url='https://www.weibo.com/'
auth=OAuth1('YOUR_APP_KEY','YOUR_APP_SECRET',
'USER_OAUTH_TOKEN','UER_OAUTH_TOKEN_SECRET')
request.get(url,auth=auth)
9.Prepared Request
将请求表示为数据结构,其中各个参数都可以通过Requests对象来表示
from requests import Request,Session
url='http://httpbin.org/post'
data={
'name':'germey'
}
headers={
}
s=Session()
req=Request('POST',url,data=data,headers=headers)
prepped=s.prepare_request(req)
r=s.send(prepped)
print(r.text)