1、Requests 用法
例子:
import requests
response = requests.get('https://www.baidu.com')
print(type(response))
print(response.status_code) //200
print(type(response.text))
print(response.text)
print(response.cookies) //不需要声明cookiejar
各种请求方式
import requests
requests.post('http://httpbin.org/post')
requests.put('http://httpbin.org/post')
requests.delete('http://httpbin.org/post')
requests.options('http://httpbin.org/post')
基本Get请求
import requests
response =requests.get('http://httpbin.org/get')
print(response.text)
带参数GET请求
import requests
response =requests.get('http://httpbin.org/get?name=germey&age=22')
print(response.text)
或者
data={
'name':'germey',
'age':22
}
response =requests.get('http://httpbin.org/get',params=data)
print(response.text)
解析Json 方法
import requests
response =requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json()) //json
或
print(json.loads(response.text))
print(type(response.json()))
获取二进制数据
import requests
response=requests.get('https://github.com/favicon.ico')
print(type(response.text),type(response.content))
print(response.text)
print(response.content)
保存图片
import requests
response=requests.get('https://github.com/favicon.ico')
with open('favicon.icon','wb') as f:
f.write(response.content)
f.close()
添加headers
import requests
response= requests.get('https://www.zhihu.com/expiore')
print(response.text)
添加 headers
import requests
headers={
}
response= requests.get('https://www.zhihu.com/expiore',headers=headers)
print(response.text)
基本Post请求
import requests
data ={'name':'germey','age':'22'}
response = requests.post('http://httpbin.org/post',data=data)
print(response.text)
添加 headers
import requests
data ={'name':'germey','age':'22'}
headers={
}
response = requests.post('http://httpbin.org/post',data=data,headers=headers)
print(response.text)
响应
response 属性
import requests
response =requests.get('http://www.jianshu.com')
print(type(response.status_code),response.status_code)
print(type(response.headers),response.headers)
print(type(response.cookies),response.cookies)
print(type(response.url),response.url)
print(type(response.history),response.history)
高级操作
文件上传
import requests
files ={'file':open('favicon.ico','rb')}
response= requests.post('http://httpbin.org/post',files=files)
print(response.text)
获取cookies
import requests
response =requests.get('https://www.baidu.com')
print(response.cookies)
for key,value in response.cookies.items():
print(key+"="+value)
会话维持
模拟登陆
import requests
requests.get('http://httpbin.org/cookies/set/number/1234567')
response = requests.get('http://httpbin.org/cookies')
print(response.text)
如果同一个浏览器多页面共享cookie 用session()
import requests
s= requests.Session()
s.get('http://httpbin.org/cookies/set/number/1234567')
response=s.get('http://httpbin.org/cookies')
print(response.text)
代理设置
import requests
proxies={
'http':'http://127.0.0.19743',
'https':'http://127.0.0.19743'
}
response=requests.get('https://www.taobao.com',proxies=proxies)
print(response.status_code)
有用户密码情况的
import requests
proxies={
'http':'http://user:password@127.0.0.19743',
}
response=requests.get('https://www.taobao.com',proxies=proxies)
print(response.status_code)
超时设置
import requests
response=requests.get('https://www.taobao.com',timeout=1)
print(response.status_code)
认证设置
import requests
from requests.auth import HTTPBasicAuth
r =requests.get('http://120.27.34.24:9001',auth=HTTPBasicAuth('user','123'))
print(r.status_code)
第二种方式
import requests
r =requests.get('http://120.27.34.24:9001',auth=('user','123'))
print(r.status_code)
异常处理
import requests
from requests.exceptions import ReadTimeout,HTTPError,RequestException
try:
response=requests.get('http://httpbin.org/get',timeout=0.5)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except HTTPError
print('Http error')
except RequestException:
print('Error')