本文僅供學習參考,建議調試階段使用, 生產階段可以使用進階用法
(***)
表示經常會用到, (**)
表示會用到但不經常, (*)
表示很少用到requests.[method](url)
In [1]: import requests
In [2]: url = 'http://www.test.com'
In [3]: params = {
"key1":"val1", "key2":"val2"}
In [4]: r = requests.get(url, params)
In [5]: r.url
Out[5]: 'https://www.test.com/?key1=val1&key2=val2'
In [6]: params2 = {
"key1": "val1", "key2":["v2", "val2"]}
# 最近碰到個問題,極個別接口在get請求的參數params時,若數據類型為dict,則報500,json.dumps()後則200, 將參數名params改為data,也200, 改為json也200. 若采用get(url, params=json.dumps(dict), ) 方案,這樣的話,其它接口可能就會400了,最後判斷返回是否500,若500 則dumps.
In [7]: r = requests.get(url,params2)
# 因網址問題,可能會報錯
In [8]: r.url
Out[8]: 'https://www.test.com/?key1=val1&key2=v2&key2=val2'
- requests.get(url, params={“key1”: “val1”, “key2”: “val2”}), 相當於在url後面拼接一些參數
params
參數只在get中使用.
In [3]: r = requests.get("https://www.baidu.com")
In [4]: r.url
Out[4]: 'https://www.baidu.com/'
In [5]: r.text
Out[5]: '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=conten
t-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=
IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css hr
ef=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css
><title>莽\x99戮氓潞婁盲賂\x80盲賂\x8b茂錄\x8c盲陸\xa0氓擄鹵莽\x9f樓茅\x81\x93</title></head> <bo
dy link=#0000cc> ... 此處省略幾千字...</body> </html>\r\n'
In [6]: r.encoding
Out[6]: 'ISO-8859-1'
In [7]: r.encoding= 'utf-8'
In [9]: r.encoding
Out[9]: 'utf-8'
In [23]: r.content
Out[23]: ...省略幾千字... 如果r的鏈接地址是個圖片, 可以非常好的作為示例.
In [24]: from PIL import Image
In [25]: from io import BytesIO
In [26]: i = Image.open(BytesIO(r.content)) # 可以作為下載圖片或二進制文檔的方法
In [1]: r = requests.get('https://api.github.com/events')
In [2]: r.json()
Out[2]:
[{
'id': '13092490355',
'type': 'PushEvent',
'actor': {
'id': 46406730,
'login': 'NekoSilverFox
...
}]
In [1]: r = requests.get('https://api.github.com/events', stream=True) # 可能會報錯
In [2]: r.json()
Out[2]:
[{
'id': '13092490355',
'type': 'PushEvent',
'actor': {
'id': 46406730,
'login': 'NekoSilverFox
...
}]
In [3]: r.raw
Out[3]: <urllib3.response.HTTPResponse at 0x1b4c491ddd8>
In [4]: r.raw.read(10)
Out[4]: b''
一般情況下使用如下代碼替代文件流:
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {
'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
requests.post(url, data={"key1": "val1", "key2": "val2"})
字典數據
傳給data
參數, 還可以傳輸元組列表
, 也可以傳string
(此時需要用json.dumps()處理下).requests.post(url, json={"key1": "val1", "key2": "val2"})
字典數據
傳給json
參數, 會自動編碼
- post請求可以 接受
data
和json
兩個參數,不接受params- 若 post請求傳參要求是body中raw的格式josn(application/json), 則在post請求參數中添加headers參數, 參數值包含{“Content-Type”: “application/json; charset=UTF-8”}
import requests
headers = {
"Content-Type": "application/json; charset=UTF-8",
}
resp = requests.post(url, data=(), headers=headers)
# 將字典數據傳給data參數
>>> data = {
'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=data)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
# 將元組數據傳給data參數
>>> payload = (('key1', 'value1'), ('key1', 'value2'))
>>> r = requests.post('http://httpbin.org/post', data=payload)
>>> print(r.text)
{
...
"form": {
"key1": [
"value1",
"value2"
]
},
...
}
# 將json數據傳給data參數
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> json_dict = {
"some": "data"}
>>> r = requests.post(url, data=json.dumps(payload))
# 將字典數據傳給json參數
>>> url = 'https://api.github.com/some/endpoint'
>>> json_dict = {
"some": "data"}
>>> r = requests.post(url, json=json_dict)
格式如下:r = requests.post(url, files={'file': open('report.xls', 'rb')})
>>> url = 'http://httpbin.org/post'
>>> files = {
'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
{
...
"files": {
"file": "<censored...binary...data>"
},
...
}
>>> url = 'http://httpbin.org/post'
>>> files = {
'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {
'Expires': '0'})}
>>> r = requests.post(url, files=files)
>>> r.text
{
...
"files": {
"file": "<censored...binary...data>"
},
...
}
>>> r = requests.get('https://www.baidu.com')
>>> r.status_code # 獲取請求狀態碼
200
>>> r.status_code == requests.codes.ok # 可以用此來判斷是否請求成功
True
>>> bad_r = requests.get("http://httpbin.org/status/404")
>>> bad_r.status_code
404
>>> bad_r.raise_for_status() # 可以用此來查找 請求錯誤的原因
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Anaconda3\lib\site-packages\requests\models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: http://httpbin.org/sta
tus/404
>>> r.raise_for_status() # 如果請求成功, 那麼沒有錯誤原因的, 所以返回為None.
>>>
>>> r.headers
{
'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connecti
on': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Mon,
03 Aug 2020 09:15:49 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:23:51 GMT', 'Pragma': 'no
-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baid
u.com; path=/', 'Transfer-Encoding': 'chunked'}
>>> r.headers.get('content-type') # 此處get的key大小寫不敏感
'text/html'
>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)
>>> r.cookies['example_cookie_name']
'example_cookie_value'
>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)
>>> r.cookies
<RequestsCookieJar[]>
>>> r.cookies.get('example_cookie_name')
>>> test_cookies = dict(cookes_are='working')
>>> r = requests.get(url, cookies=test_cookies)
>>> r.text
'...省略幾千字...'
>>>
對象為RequestsCookiesJar
, 和字典非常相似, 也可以將jar傳到cookies參數中
>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
>>> jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
>>> url = 'http://httpbin.org/cookies'
>>> r = requests.get(url, cookies=jar)
>>> r.text
'{"cookies": {"tasty_cookie": "yum"}}'
# 配置超時處理(以下兩種方法都可以), 生產代碼必須使用這一參數.
requests.get(url, timeout=(3, 7))
requests.get(url, timeout=10)