Python?Requests庫知識匯總
什么是Requests庫?
requests庫github地址:https://github.com/requests/requests
Reqyests庫主要用來準(zhǔn)備Request和處理Response。
為什么要學(xué)習(xí)Requests庫?
web開發(fā)和爬蟲都需要學(xué)習(xí)的東西,在服務(wù)端編程中理解好Requests庫可以更好的編寫Restful API的程序,還是自動化測試的工具箱。
快速開始
發(fā)送請求
>>> import requests >>> r = requests.get('https://api.github.com/events') # GET >>> r = requests.post('https://httpbin.org/post', data={'key': 'value'}) # POST >>> r = requests.put('https://httpbin.org/put', data={'key': 'value'}) # PUT >>> r = requests.delete('https://httpbin.org/delete') # DELETE >>> r = requests.head('https://httpbin.org/get') # HEAD >>> r = requests.options('https://httpbin.org/get') # OPTIONS
URL傳參
可以使用params
字典參數(shù)為URL提供查詢字符串參數(shù),例如,訪問 https://httpbin.org/get?key1=value1&key2=value2
,可使用以下代碼:
>>> import requests >>> payload = {'key1': 'value1', 'key2': 'value2', 'key3':'', 'key4':None} >>> r = requests.get('https://httpbin.org/get', params=payload) >>> r.url https://httpbin.org/get?key2=value2&key1=value1&key3=
需要注意的是,如果字典參數(shù)中key值(即URL參數(shù)的值為None
),則該參數(shù)不會添加到URL的查詢字符串中。
如果URL查詢字符串中,存在重復(fù)參數(shù)(參數(shù)名稱相同,參數(shù)值不同),則需要將key值設(shè)置為由參數(shù)值組成的列表,如下:
>>> import requests >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']} >>> r = requests.get('https://httpbin.org/get', params=payload) >>> r.url https://httpbin.org/get?key1=value1&key2=value2&key2=value3
響應(yīng)內(nèi)容
讀取服務(wù)器響應(yīng)內(nèi)容
>>> import requests >>> r = requests.get('https://api.github.com/events') >>> r.text <class 'str'> [{"id":"27579847062","type":"PushEvent","actor":{"...
requests 將自動解碼來自服務(wù)器的內(nèi)容。大多數(shù)unicode字符集都是無縫解碼的。
當(dāng)你發(fā)出請求時,requests會根據(jù)HTTP頭對響應(yīng)的編碼進(jìn)行有依據(jù)的猜測。當(dāng)你訪問r.text
時,將使用requests猜測的文本編碼??梢允褂?code>r.encoding屬性查找請求使用的編碼,并對其進(jìn)行更改:
>>> r.encoding # 輸出:utf-8 r.encoding = 'ISO-8859-1'
如果更改編碼,則每當(dāng)調(diào)用r.text
時,requests都將使用新的r.encoding
的值。在任何情況下,你都可以應(yīng)用特殊邏輯來確定內(nèi)容的編碼。例如,HTML和XML可以在其正文中指定其編碼。在這種情況下,你應(yīng)該使用r.content
查找編碼,然后設(shè)置r.encoding
。這將允許你使用具有正確編碼的r.text
。
requests還將在需要時使用自定義編碼。如果你已經(jīng)創(chuàng)建了自己的編碼并將其注冊到codecs
模塊,則可以簡單地使用codec名稱作為r.encoding
的值,而requests將為你處理解碼。
二進(jìn)制響應(yīng)內(nèi)容
對于非文本請求,還可以以字節(jié)的形式訪問響應(yīng)體(當(dāng)然,文本請求也可以):
>>> r.content b'[{"id":"27581220674","type":"IssueCommentEvent","actor":{"id":327807...
requests會自動解碼gzip
和deflate
傳輸編碼。
如果安裝了類似 brotli 或 brotlicffi的Brotil類庫,Requets也會自動界面br
傳輸編碼
如果Brotli庫(如[Brotli])為您自動解碼br
傳輸編碼(https://pypi.org/project/brotli)或brotliffi已安裝。
例如,可以使用以下代碼,從請求返回的二進(jìn)制數(shù)據(jù)創(chuàng)建圖像:
from PIL import Image from io import BytesIO img = Image.open(BytesIO(r.content))
JSON響應(yīng)內(nèi)容
可使用內(nèi)置的JSON解碼器,處理JSON數(shù)據(jù):
>>> import requests >>> r = requests.get('https://api.github.com/events') >>> r.json() # JSON [{'id': '27609416600', 'type': 'PushEvent', ...
如果JSON解碼失敗,r.json()
將拋出異常。例如,如果響應(yīng)得到一個204(無內(nèi)容),或者如果響應(yīng)包含無效的JSON,則r.json()
會拋出requests.exceptions.JSONDecodeError
。此封裝的異??赡軙?yàn)椴煌琾ython版本和JSON序列化庫可能引發(fā)的多個異常提供互操作性。
需要注意的是,調(diào)用r.json()
的成功調(diào)用并不表示響應(yīng)的成功。一些服務(wù)器可能會在失敗的響應(yīng)中返回JSON對象(例如,HTTP 500的錯誤詳細(xì)信息)。這樣的JSON將被解碼并返回。要檢查請求是否成功,請使用r.raise_for_status()
或檢查r.status_code
原始響應(yīng)內(nèi)容
可以通過訪問r.raw
訪問服務(wù)器返回的原始socket響應(yīng)。如果希望這樣做,確保在初始請求中設(shè)置 stream=True
:
>>> import requests >>> r = requests.get('https://api.github.com/events', stream=True) >>> r.raw <urllib3.response.HTTPResponse object at 0x0000018DB1704D30> >>> r.raw.read(10) b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
然而,通常情況下,應(yīng)該使用類似這樣的模式來保存正在流式傳輸?shù)膬?nèi)容到文件中:
with open(filename, 'wb') as fd: for chunk in r.iter_content(chunk_size=128): fd.write(chunk)
使用Response.iter_content
將處理很多你在直接使用Resort.raw
時需要處理的事情。當(dāng)流式傳輸下載時,以上是檢索內(nèi)容的首選和推薦方法。請注意,chunk_size
可以自由調(diào)整為更適合你使用場景的數(shù)字。
注意
關(guān)于使用 Response.iter_content
與Response.raw
的重要注意事項(xiàng)。 Response.iter_content
將自動解碼gzip
和deflate
傳輸編碼。Response.raw
是一個原始字節(jié)流--它不會轉(zhuǎn)換響應(yīng)內(nèi)容。如果確實(shí)需要訪問返回的字節(jié),請使用Response.raw
。
自定義請求頭
如果您想向請求中添加HTTP頭,只需向headers
參數(shù)傳遞一個dict
即可,例如:
>>> url = 'https://api.github.com/some/endpoint' >>> headers = {'user-agent': 'my-app/0.0.1'} >>> r = requests.get(url, headers=headers)
注意:自定義請求頭的優(yōu)先級低于更具體的信息源。例如:
- 如果在
.netrc
中指定了憑據(jù),則使用headers=
設(shè)置的Authorization
請求頭將被覆蓋,而憑據(jù)又將被auth=
參數(shù)覆蓋。請求將在~/.netrc
、~/_netrc
或NETRC
環(huán)境變量指定的路徑處中搜索netrc文件。 - 如果從主機(jī)重定向,將刪除
Authorization
請求頭。 Proxy-Authorization
請求頭將被URL中提供的代理憑據(jù)覆蓋。- 當(dāng)我們可以確定內(nèi)容的長度時,將覆蓋
Content-Length
請求頭。
此外,請求根本不會根據(jù)指定的自定義請求頭更改其行為。請求頭僅是簡單的傳遞到最終請求中。
注意:所有請求頭值必須是字符串、字節(jié)字符串或unicode。雖然允許,但建議避免傳遞unicode請求頭值。
更復(fù)雜的POST請求More complicated POST requests
通常,如果發(fā)送一些表單編碼(form-encoded)的數(shù)據(jù)--就像一個HTML表單。為此,只需將字典傳遞給data
參數(shù)即可。發(fā)送請求時,將自動對字典數(shù)據(jù)進(jìn)行表單編碼:
>>> import requests >>> payload = {'key1': 'value1', 'key2': 'value2'} >>> r = requests.post("https://httpbin.org/post", data=payload) >>> r.text { "args": {}, "data": "", "files": {}, "form": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-6409fe3b-0cb4118319f09ab3187402bc" }, "json": null, "origin": "183.62.127.25", "url": "https://httpbin.org/post" }
data
參數(shù)中,為每個鍵可以具有多個值。這可以通過將data
設(shè)置為元組列表或以列表為值的字典來實(shí)現(xiàn)。當(dāng)表單中有多個元素使用相同的鍵時,這特別有用:
>>> import requests >>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')] >>> r1 = requests.post('https://httpbin.org/post', data=payload_tuples) >>> payload_dict = {'key1': ['value1', 'value2']} >>> r2 = requests.post('https://httpbin.org/post', data=payload_dict) >>> r1.text { "args": {}, "data": "", "files": {}, "form": { "key1": [ "value1", "value2" ] }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-6409ff49-11b8232a7cc81fc0290ec4c4" }, "json": null, "origin": "183.62.127.25", "url": "https://httpbin.org/post" } >>> re.text == r2.text True
有時,你可能想發(fā)送未經(jīng)表單編碼的數(shù)據(jù),則需要傳入string
類型的數(shù)據(jù),而不是dict
,string
數(shù)據(jù)將被直接提交。
例如,GitHub API v3接受JSON編碼的POST/PATCH數(shù)據(jù):
>>> import requests >>> import json >>> url = 'https://api.github.com/some/endpoint' >>> payload = {'some': 'data'} >>> r = requests.post(url, data=json.dumps(payload))
請注意,上述代碼不會添加Content-Type
請求頭(特別是不會將其設(shè)置為application/json
)。如果需要設(shè)置那個請求頭('Content-Type': 'application/json
,發(fā)送json請求體),并且不想自己對dict
進(jìn)行編碼,你也可以直接使用json
參數(shù)傳遞它,它將自動被編碼:
>>> url = 'https://api.github.com/some/endpoint' >>> payload = {'some': 'data'} >>> r = requests.post(url, json=payload)
注意,如果提供了data
,或者file
參數(shù),json
參數(shù)將被自動忽略。
提交Multipart-Encoded文件
Request讓上傳Multipart編碼文件變得簡單:
>>> import requests >>> url = 'https://httpbin.org/post' >>> files = {'file': open('report.xls', 'rb')} >>> r = requests.post(url, files=files) >>> r.text { "args": {}, "data": "", "files": { "file": "#!/usr/bin/env python\r\n# -*- coding:utf-8 -*-\r\n\r\n#!/usr/bin/env python\r\n# -*- coding:utf-8 -*-\r\n\r\nfrom multiprocessing import Pool\r\nfrom threading import Thread\r\nfrom concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor..." }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "3035", "Content-Type": "multipart/form-data; boundary=9ef4437cb1e14427fcba1c42943509cb", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-640a03df-1a0a5ce972ce410378cda7a2" }, "json": null, "origin": "183.62.127.25", "url": "https://httpbin.org/post" }
可以顯示的設(shè)置文件名稱,內(nèi)容類型,請求頭:
>>> url = 'https://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 { "args": {}, "data": "", "files": { "file": "data:application/vnd.ms-excel;base64,UEsDBBQAAAAAAHy8iFMAAAAAAA...==" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "9667", "Content-Type": "multipart/form-data; boundary=ff85e1018eb5232f7dcab2b2bc5ffa50", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-640def51-43cc213e33437a0e60255add" }, "json": null, "origin": "183.62.127.25", "url": "https://httpbin.org/post" }
如果想發(fā)送一些字符串,以文件的方式被接收:
>>> url = 'https://httpbin.org/post' >>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')} >>> r = requests.post(url, files=files) >>> r.text { "args": {}, "data": "", "files": { "file": "some,data,to,send\nanother,row,to,send\n" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "184", "Content-Type": "multipart/form-data; boundary=2bfe430e025860528e29c893a09f1198", "Host": "httpbin.org", "User-Agent": "python-requests/2.27.1", "X-Amzn-Trace-Id": "Root=1-640df132-247947ca699e9da35c588f2d" }, "json": null, "origin": "183.62.127.25", "url": "https://httpbin.org/post" }
如果你將一個非常大的文件作為multipart/form-data
請求提交,你可能需要流式傳輸該請求。默認(rèn)情況下,requests
不支持此功能,但有一個單獨(dú)的包支持此功能——requests toolbelt
。閱讀toolbelt文檔獲取有關(guān)如何使用它的詳細(xì)信息。
要在一個請求中發(fā)送多個文件,請參閱高級章節(jié)。
警告
強(qiáng)烈建議以二進(jìn)制模式打開文件。這是因?yàn)閞equests可能會嘗試為你提供Content-Length
請求頭,如果這樣做,該請求頭值將被設(shè)置為文件中的字節(jié)數(shù)。如果以文本模式打開文件,可能會發(fā)生錯誤。
響應(yīng)狀態(tài)碼
>>> import requests >>> r = requests.get('https://httpbin.org/get') >>> r.status_code 200
以便于參考,requests
還附帶一個內(nèi)置的狀態(tài)代碼查找對象:
>>> r = requests.get('https://httpbin.org/get') >>> r.status_code == requests.codes.ok True
如果請求出錯4XX客戶端錯誤或5XX服務(wù)器錯誤響應(yīng)),我們可以使用response.raise_for_status()
拋出錯誤:
>>> import requests >>> bad_r = requests.get('https://httpbin.org/status/404') >>> bad_r.status_code 404 >>> bad_r.raise_for_status() Traceback (most recent call last): File "D:/codePojects/test.py", line 12, in <module> bad_r.raise_for_status() File "D:\Program Files (x86)\python36\lib\site-packages\requests\models.py", line 960, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: https://httpbin.org/status/404
但是,如果r.status_code
為200
, raise_for_status()
將返回None
>>> r.raise_for_status() None
響應(yīng)頭
>>> r.headers { 'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'connection': 'close', 'server': 'nginx/1.0.4', 'x-runtime': '148ms', 'etag': '"e1ca502697e5c9317743dc078f67693f"', 'content-type': 'application/json' }
根據(jù)RFC 7230, HTTP請求頭大小寫不敏感,所以,我們可以使用任何大寫。因此,我們可以使用任意大小寫來訪問請求頭:
>>> r.headers['Content-Type'] 'application/json' >>> r.headers.get('content-type') 'application/json'
Cookies
如果響應(yīng)包含Cookie,可以快速訪問它們:
>>> url = 'http://example.com/some/cookie/setting/url' >>> r = requests.get(url) >>> r.cookies['example_cookie_name'] # 如果存在名為 example_cookie_name的cookie的話 'example_cookie_value'
可以使用cookies
參數(shù)將cookie發(fā)送給服務(wù)器:
>>> url = 'https://httpbin.org/cookies' >>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies) >>> r.text '{\n "cookies": {\n "cookies_are": "working"\n }\n}\n'
Cookies are returned in a RequestsCookieJar
, which acts like a dict
but also offers a more complete interface, suitable for use over multiple domains or paths. Cookie jars can also be passed in to requests:
返回的Cookie存儲在RequestsCookieJar
中,其作用類似于dict
,同時提供了一個更完整的接口,適合在多個域或路徑上使用。Cookie jar也可以傳遞給請求:
>>> jar = requests.cookies.RequestsCookieJar() >>> jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies') Cookie(version=0, name='tasty_cookie', value='yum', port=None, port_specified=False, domain='httpbin.org', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) >>> jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere') Cookie(version=0, name='gross_cookie', value='blech', port=None, port_specified=False, domain='httpbin.org', domain_specified=True, domain_initial_dot=False, path='/elsewhere', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) >>> url = 'https://httpbin.org/cookies' >>> r = requests.get(url, cookies=jar) >>> r.text '{"cookies": {"tasty_cookie": "yum"}}'
重定向與history
默認(rèn)情況下,requests
將對除HEAD
之外的所有請求執(zhí)行位置重定向(如果需要重定向的話)。
我們可以使用Response對象的history
屬性來跟蹤重定向。
Response.history
列表包含為完成請求而創(chuàng)建的Response
對象。列表按響應(yīng)的先后順序排序。
例如,Gitee將所有HTTP請求重定向到HTTPS:
>>> r = requests.get('http://gitee.com/') >>> r.url 'https://gitee.com/' >>> r.status_code 200 >>> r.history [<Response [302]>]
如果使用HEAD,GET, OPTIONS
, POST
, PUT
, PATCH
或者DELETE
,可以使用 allow_redirects
參數(shù)禁止重定向:
>>> r = requests.get('http://gitee.com/', allow_redirects=False) >>> r.status_code 302 >>> r.history [] >>> r = requests.head('http://gitee.com/', allow_redirects=False) >>> r.url 'http://gitee.com/' >>> r.status_code 302 >>> r.history [] >>> r = requests.head('http://gitee.com/', allow_redirects=True) >>> r.status_code 200 >>> r.url 'https://gitee.com/' >>> r.history [<Response [302]>]
請求超時
可以使用timeout
參數(shù)告訴requests在給定的秒數(shù)后停止等待響應(yīng)。幾乎所有的生產(chǎn)代碼都應(yīng)該在幾乎所有的請求中使用此參數(shù)。否則會導(dǎo)致程序無限期掛起:
>>> requests.get('https://gitee.com/', timeout=0.1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ... urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='gitee.com', port=443): Read timed out. (read timeout=0.1)
注意:
timeout
不是整個響應(yīng)的下載時間限制;相反,如果服務(wù)器在timeout
秒內(nèi)沒有發(fā)出響應(yīng)(更準(zhǔn)確地說,如果在timeout
秒內(nèi)底層socket沒有接收到任何字節(jié)數(shù)據(jù)),則會引發(fā)異常。如果未明確指定timeout
,則請求不會超時。
錯誤和異常
如果出現(xiàn)網(wǎng)絡(luò)問題(例如DNS故障、拒絕連接等),requests將拋出ConnectionError
異常。
如果HTTP請求返回了失敗的狀態(tài)代碼,Response.raise_for_statu()
將拋出HTTPError
如果請求超時,則會拋出Timeout
異常。
如果請求超過了配置的最大重定向次數(shù),則會拋出TooManyRedirects
異常。
requests顯式拋出的所有異常都繼承自requests.exceptions.RequestException
。
高級用法
Session對象
Session對象允許你跨請求保持某些參數(shù),以及Session實(shí)例發(fā)出的所有請求的cookie,并將使用urllib3
的[連接池](https://urllib3.readthedocs.io/en/latest/reference/index.html#module-urllib3.connectionpool)。因此,如果你向同一主機(jī)發(fā)出多個請求,將復(fù)用底層TCP連接,這可能會顯著提高性能(請參見HTTP持久連接)。
Session對象具有主要 requests API的所有方法。
讓我們在請求之間保持一些cookie:
>>> s = requests.Session() >>> s.get('https://httpbin.org/cookies/set/sessioncookie/123456789') <Response [200]> >>> r = s.get('https://httpbin.org/cookies') >>> r.text '{\n "cookies": {\n "sessioncookie": "123456789"\n }\n}\n' >>>
Seesion對象還可以用于向請求方法提供默認(rèn)數(shù)據(jù)。這是通過向Session對象屬性提供數(shù)據(jù)來實(shí)現(xiàn)的:
>>> s = requests.Session() >>> s.auth = ('user', 'pass') >>> s.headers.update({'x-test': 'true'}) # 'x-test'和'x-test2'請求頭隨請求發(fā)送了 >>> s.headers.update({'x-test': 'true'}) >>> s.get('https://httpbin.org/headers', headers={'x-test2': 'true'}) <Response [200]>
傳遞給請求方法的任何字典都將與會話級別設(shè)置的值合并。方法級別的參數(shù)會覆蓋會話級別的參數(shù)。
然而,請注意,即使使用會話,方法級參數(shù)也不能跨請求保持。本示例將只在發(fā)送第一個請求發(fā)送cookie,而不發(fā)送第二個請求
>>> s = requests.Session() >>> r = s.get('https://httpbin.org/cookies', cookies={'from-my': 'browser'}) >>> r.text '{\n "cookies": {\n "from-my": "browser"\n }\n}\n' >>> r = s.get('https://httpbin.org/cookies') >>> r.text '{\n "cookies": {}\n}\n'
Cookie utility functions to manipulate Session.cookies
如果想手動向Session添加Cookie,那么使用 Cookie utility functions來操作Session.cookies
。
Session對象也可以用作上下文管理器
>>> with requests.Session() as s: ... s.get('https://httpbin.org/cookies/set/sessioncookie/123456789') ... <Response [200]> >>>
這將確保在退出with
塊后立即關(guān)閉會話,即使發(fā)生未處理的異常。
Remove a Value From a Dict Parameter Sometimes you'll want to omit session-level keys from a dict parameter. To do this, you simply set that key's value to `None` in the method-level parameter. It will automatically be omitted. 從字典參數(shù)中刪除值 有時,你需要從dict參數(shù)中忽略會話級別的鍵。為此,只需在方法級參數(shù)中將該鍵的值設(shè)置為“None”即可。它將被自動忽略。
Session中包含的所有值都可以直接使用。參見Session API Docs了解更多信息。
請求和響應(yīng)對象
示例:獲取響應(yīng)頭和請求頭
>>> r = s.get('https://httpbin.org') >>> r.headers # 獲取響應(yīng)頭 {'Date': 'Mon, 13 Mar 2023 15:43:41 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '9593', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'} >>> r.request.headers {'User-Agent': 'python-requests/2.27.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'sessioncookie=123456789'} >>>
Prepared requests
每當(dāng)收到來自某個API調(diào)用或者Session調(diào)用的Response
對象,request
屬性實(shí)際上是所使用的PreparedRequest
。在某些情況下,你可能希望在發(fā)送請求之前對請求體或請求頭(或其他任何內(nèi)容)做一些額外的工作。簡單的做法如下:
from requests import Request, Session s = Session() req = Request('POST', url, data=data, headers=headers) prepped = req.prepare() # do something with prepped.body prepped.body = 'No, I want exactly this as the body.' # do something with prepped.headers del prepped.headers['Content-Type'] resp = s.send(prepped, stream=stream, verify=verify, proxies=proxies, cert=cert, timeout=timeout ) print(resp.status_code)
However, the above code will lose some of the advantages of having a requests S
essio
n
object. In particular, Sessio
n
-level state such as cookies will not get applied to your request. To get a PreparedRequest
with that state applied, replace the call to Request.prepare()
with a call to Session.prepare_request()
, like this:
由于你沒有對Request
對象執(zhí)行任何特殊操作,因此您可以立即prepare它并修改PreparedRequest
對象。然后將其與發(fā)送給requests.*
或Session.*
的其它參數(shù)一起發(fā)送。
然而,上述代碼將失去使用requests Session
對象的一些優(yōu)點(diǎn)。特別是Session
級別的狀態(tài),比如cookie將不會應(yīng)用于你的請求。如果需要獲取應(yīng)用了那些狀態(tài)的 Prep
aredRequ
est
,替換 Request.prepare()
調(diào)用為Session.prepare_request()
,像這樣:
from requests import Request, Session s = Session() req = Request('GET', url, data=data, headers=headers) prepped = s.prepare_request(req) # do something with prepped.body prepped.body = 'Seriously, send exactly these bytes.' # do something with prepped.headers prepped.headers['Keep-Dead'] = 'parrot' resp = s.send(prepped, stream=stream, verify=verify, proxies=proxies, cert=cert, timeout=timeout ) print(resp.status_code)
When you are using the prepared request flow, keep in mind that it does not take into account the environment. This can cause problems if you are using environment variables to change the behaviour of requests. For example: Self-signed SSL certificates specified in REQUESTS_CA_BUNDLE
will not be taken into account. As a result an SSL: CERTIFICATE_VERIFY_FAILED
is thrown. You can get around this behaviour by explicitly merging the environment settings into your session:
當(dāng)你使用prepared request請求時,請記住它沒有考慮環(huán)境。如果你正使用環(huán)境變量來更改請求的行為,這可能會導(dǎo)致問題。例如:在REQUESTS_CA_BUNDLE
中指定的自簽名SSL證書將不起作用,結(jié)果引發(fā)了SSL:CERTIFICATE_VERIFY_FAILED
。你可以通過將環(huán)境設(shè)置顯式合并到Session中來避免這種行為:
from requests import Request, Session s = Session() req = Request('GET', url) prepped = s.prepare_request(req) # Merge environment settings into session settings = s.merge_environment_settings(prepped.url, {}, None, None, None) resp = s.send(prepped, **settings) print(resp.status_code)
HTTP Basic 驗(yàn)證
>>> from requests.auth import HTTPBasicAuth >>> auth = HTTPBasicAuth('your_username', 'your_password') >>> r = requests.post(url='you_target_url', data=body, auth=auth)
SSL證書驗(yàn)證
requests驗(yàn)證HTTPS請求的SSL證書,就像web瀏覽器一樣。默認(rèn)情況下,SSL驗(yàn)證已啟用,如果無法驗(yàn)證證書,請求將拋出SSLError:
>>> requests.get('https://requestb.in') requests.exceptions.SSLError: hostname 'requestb.in' doesn't match either of '*.herokuapp.com', 'herokuapp.com'
你可以使用verify
參數(shù)傳遞擁有受信任CA的證書的CA_BUNDLE文件的路徑或者目錄:
>>> requests.get('https://github.com', verify='/path/to/certfile')
或者
s = requests.Session() s.verify = '/path/to/certfile'
注意:
如果verify
設(shè)置為目錄的路徑,則必須使用OpenSSL提供的c_rehash
實(shí)用程序處理該目錄。
還可以通過REQUESTS_CA_BUNDLE
環(huán)境變量指定此受信任CA列表。如果未設(shè)置REQUESTS_CA_BUNDLE
,將使用CURL_CA_BUNDLE
。
如果將verify
設(shè)置為False
,則requests也可以忽略SSL證書驗(yàn)證:
>>> requests.get('https://kennethreitz.org', verify=False) <Response [200]>
請注意,當(dāng)verify
設(shè)置為False
時,Requests將接受服務(wù)器提供的任何TLS證書,并將忽略主機(jī)名不匹配,或過期的證書,這將使你的應(yīng)用程序容易受到中間人(MitM)攻擊。在本地開發(fā)或測試期間,將verify
設(shè)置為False
可能很有用。
默認(rèn)情況下,verify
設(shè)置為True
。選項(xiàng)verify
僅適用于主機(jī)證書。
客戶端證書
你還可以將本地證書指定為客戶端證書、單個文件(包含私鑰和證書)或兩個文件路徑的元組
>>> requests.get('https://kennethreitz.org', cert=('/path/client.cert', '/path/client.key')) <Response [200]>
或者:
s = requests.Session() s.cert = '/path/client.cert'
警告
本地證書的私鑰必須為未加密的。當(dāng)前,Requests不支持加密的私鑰
CA證書
Reuests使用來自certific包中的證書. 這允許用戶在不更改Requests版本的情況下更新其受信任的證書。
在2.16版本之前,Requests捆綁了一組其信任的根CA證書,證書來源于Mzillatruststore。每個Request版本只更新一次證書。當(dāng)未安裝certific
時,當(dāng)使用較舊版本的requests時,這會導(dǎo)致證書包非常過時。
為了安全起見,我們建議經(jīng)常升級certific
!
Body內(nèi)容工作流
默認(rèn)情況下,當(dāng)你發(fā)出一個請求時,將立即下載響應(yīng)的正文。你可以使用stream
參數(shù)覆蓋此行為并延遲下載響應(yīng)主體直到訪問response.content
屬性
tarball_url = 'https://github.com/psf/requests/tarball/main' r = requests.get(tarball_url, stream=True)
此時,僅僅響應(yīng)頭被下載,且連接保持打開狀態(tài),因此,允許我們有條件的檢索內(nèi)容:
if int(r.headers.get('content-length')) < TOO_LONG: content = r.content ...
您可以使用 Response.iter_content()
和Response.iter_lines()
方法進(jìn)一步控制工作流?;蛘?,可以從位于Response.raw
的底層的urllib3.HTTPResponse
中讀取未編碼的主體.
如果在發(fā)出請求時將stream
設(shè)置為True
,則requests無法釋放連接回連接池,除非讀取完所有數(shù)據(jù)或調(diào)用Response.close
。這可能導(dǎo)致連接效率低下。如果你發(fā)現(xiàn)自己在使用stream=True
時部分讀取請求體(或根本沒有讀取它們),則應(yīng)在with
語句中發(fā)出請求,以確保連接最終處于關(guān)閉狀態(tài):
with requests.get('https://httpbin.org/get', stream=True) as r: # Do things with the response here.
Keep-Alive
多虧了urllib3
,keep-alive
在Session中是100%自動的!你在Session發(fā)出的任何請求都將自動重用合適的連接!
注意,只有在讀取了所有響應(yīng)體數(shù)據(jù)后,才會將連接釋放回連接池以供重用;請確保將stream
設(shè)置為False
或讀取Response
對象的content
屬性。
流式上傳
requests支持流式上傳,允許發(fā)送大型流或文件,而無需將其讀入內(nèi)存。要流式傳輸和上傳,只需為請求體提供一個類似文件的對象:
with open('massive-body', 'rb') as f: requests.post('http://some.url/streamed', data=f)
警告
強(qiáng)烈建議以二進(jìn)制模式打開文件。這是因?yàn)閞equests可能會嘗試為你提供Content-Length
請求頭,如果這樣做,該請求頭值將被設(shè)置為文件中的字節(jié)數(shù)。如果以文本模式打開文件,可能會發(fā)生錯誤。
分塊編碼(Chunk-Encoded)請求
requests 還支持傳出和傳入請求的分塊傳輸編碼。要發(fā)送塊編碼請求,只需簡單的為請求體提供一個生成器(或任何沒有長度的迭代器)
def gen(): yield 'hi' yield 'there' requests.post('http://some.url/chunked', data=gen())
對于分塊編碼請求的響應(yīng),最好使用Response.iter_content()
對數(shù)據(jù)進(jìn)行迭代。在理想情況下,將在請求上設(shè)置stream=True
,在這種情況下,可以通過使用值為None
的chunk_size
參數(shù)調(diào)用iter_content
來逐塊迭代。如果要設(shè)置塊的最大大小,可以將chunk_size
參數(shù)設(shè)置為任意目標(biāo)大小整數(shù)。
POST 多個分部編碼(Multipart-Encoded)文件
你可以在一個請求中發(fā)送多個文件。例如,假設(shè)你要將圖像文件上載到具有多個文件字段“images”的HTML表單:
<input type="file" name="images" multiple="true" required="true"/>
為此,只需將files
設(shè)置為(form_field_name,file_info)
的元組列表:
>>> url = 'https://httpbin.org/post' >>> multiple_files = [ ... ('images', ('foo.png', open('foo.png', 'rb'), 'image/png')), ... ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))] >>> r = requests.post(url, files=multiple_files) >>> r.text >>> r.text '{\n "args": {}, \n "data": "", \n "files": {\n "images": "data:image/png;base64,iVBORw0KGgoAAAAN...=="\n }, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "1800", \n "Content-Type": "multipart/form-data; boundary=771ef90459071106c5f47075cbca2659", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.27.1", \n "X-Amzn-Trace-Id": "Root=1-641122ea-10a6271f0fdf488c70cf90e9"\n }, \n "json": null, \n "origin": "183.62.127.25", \n "url": "https://httpbin.org/post"\n}\n'
事件鉤子(Event Hooks)
requests擁有一個hook系統(tǒng),可用于控制請求過程的部分,或者信號事件處理。
可用的hooks:
response
:請求生成的響應(yīng)
通過將{hook_name:callback_function}
字典傳遞給hooks
請求參數(shù),可以按每個請求分配一個鉤子函數(shù):
hooks={'response': print_url}
callback_function
將接收一數(shù)據(jù)塊(a chunk of data)作為其第一個參數(shù)。
def print_url(r, *args, **kwargs): print(r.url)
回調(diào)函數(shù)必須處理其自己的異常。任何為處理的異常,都不會以靜默方式傳遞,因此應(yīng)該由代碼調(diào)用請求來處理。
如果回調(diào)函數(shù)返回某個值,則假定它將替換傳入的數(shù)據(jù)。如果函數(shù)不返回任何內(nèi)容,則不產(chǎn)生任何影響
def record_hook(r, *args, **kwargs): r.hook_called = True return r
讓我們在運(yùn)行時打印一些請求方法參數(shù):
>>> requests.get('https://httpbin.org/', hooks={'response': print_url}) https://httpbin.org/ <Response [200]>
可以添加多個鉤子到單個請求中,如下,一次調(diào)用兩個鉤子函數(shù):
>>> r = requests.get('https://httpbin.org/', hooks={'response': [print_url, record_hook]}) >>> r.hook_called True
還可以為Session
實(shí)例添加鉤子,這樣添加的任何鉤子都將在向會話發(fā)出的每個請求中被調(diào)用。例如:
>>> s = requests.Session() >>> s.hooks['response'].append(print_url) >>> s.get('https://httpbin.org/') https://httpbin.org/ <Response [200]>
如果Session
實(shí)例可個鉤子函數(shù),那么將按鉤子的添加順序調(diào)用這些鉤子。
自定義身份驗(yàn)證
requests 請求支持自定義身份驗(yàn)證機(jī)制。
作為auth
參數(shù)傳遞給請求方法的任何可調(diào)用對象都有機(jī)會在發(fā)送請求之前修改請求。
身份驗(yàn)證實(shí)現(xiàn)為AuthBase
的子類,并且易于定義。requests在requests.auth
中提供了兩種常見的身份驗(yàn)證方案實(shí)現(xiàn):HTTPBasicAuth
和HTTPDigestAuth
.
假設(shè)我們有一個web服務(wù),它只有在X-Pizza
請求頭設(shè)置為密碼值時才會響應(yīng)。這不太可能,暫且還是順著它:
from requests.auth import AuthBase class PizzaAuth(AuthBase): """Attaches HTTP Pizza Authentication to the given Request object.""" def __init__(self, username): # setup any auth-related data here self.username = username def __call__(self, r): # modify and return the request r.headers['X-Pizza'] = self.username return r
然后,發(fā)送請求
>>> requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth')) <Response [200]>
流式請求
使用Response.iter_lines()
,可以很輕易的迭代流式API,比如 Twitter Streaming API。簡單的設(shè)置 stream
為 True
并且使用iter_lines
對響應(yīng)進(jìn)行迭代:
import json import requests r = requests.get('https://httpbin.org/stream/20', stream=True) for line in r.iter_lines(): # filter out keep-alive new lines if line: decoded_line = line.decode('utf-8') print(json.loads(decoded_line))
將decode_unicode=True
與 Response.iter_lines()
或者Response.iter_content()
配合使用時,如果服務(wù)器未提供編碼,則需要提供編碼:
r = requests.get('https://httpbin.org/stream/20', stream=True) if r.encoding is None: r.encoding = 'utf-8' for line in r.iter_lines(decode_unicode=True): if line: print(json.loads(line))
警告
iter_lines
不是可重入安全的。多次調(diào)用此方法會導(dǎo)致一些接收到的數(shù)據(jù)丟失。如果需要從多個地方調(diào)用它,請使用生成的迭代器對象:
lines = r.iter_lines() # Save the first line for later or just skip it first_line = next(lines) for line in lines: print(line)
代理
如果你需要使用代理,可在任何請求方法的proxys
參數(shù)中為單個請求配置代理
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080', } requests.get('http://example.org', proxies=proxies)
可選的,可以一次性為整個Session配置代理。
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080', } session = requests.Session() session.proxies.update(proxies) session.get('http://example.org')
警告
為session.proxies
提供的值可能被環(huán)境代理(由urllib.request.getproxys返回的值)覆蓋,所以為了確保在環(huán)境代理存在的情況下,也使用給定代理,顯示為所有單個請求指定proxies
參數(shù),如上述一開始所述。
如果沒有為請求設(shè)置proxies
請求參數(shù)的情況下,requests會嘗試讀取由標(biāo)準(zhǔn)環(huán)境變量 http_proxy
, https_proxy
, no_proxy
和all_proxy
定義的代理配置。這些變量名稱可大寫。所以,可以通過這些變量配置為請求設(shè)置代理(請根據(jù)實(shí)際需要配置):
linux:
$ export HTTP_PROXY="http://10.10.1.10:3128" $ export HTTPS_PROXY="http://10.10.1.10:1080" $ export ALL_PROXY="socks5://10.10.1.10:3434" $ python >>> import requests >>> requests.get('http://example.org')
win:
set HTTP_PROXY=http://10.10.1.10:3128 >>> import requests >>> requests.get('http://example.org')
要對代理使用HTTP基本身份驗(yàn)證,請?jiān)谏鲜鋈我獯砼渲萌肟谥惺褂?code>http://user:password@host/語法:
$ export HTTPS_PROXY="http://user:pass@10.10.1.10:1080" $ python >>> proxies = {'http': 'http://user:pass@10.10.1.10:3128/'}
警告
將敏感的用戶名和密碼信息存儲在環(huán)境變量或版本控制的文件中會帶來安全風(fēng)險(xiǎn),強(qiáng)烈建議不要這樣做。
如果要為特定shema和主機(jī)提供代理,請使用scheme://hostname
作proxies
字典參數(shù)的鍵來設(shè)置代理。這將匹配給定scheme和確切主機(jī)名的任何請求。
proxies = {'http://10.20.1.128': 'http://10.10.1.10:5323'}
注意,代理URL必須包含schema。
最后需要注意的,為https
連接設(shè)置代理,通常需要所在本機(jī)機(jī)器信任代理根證書。默認(rèn)的,可以通過以下代碼查找requests信任的證書列表:
from requests.utils import DEFAULT_CA_BUNDLE_PATH print(DEFAULT_CA_BUNDLE_PATH)
通過將 REQUESTS_CA_BUNDLE
(or CURL_CA_BUNDLE
) 環(huán)境變量設(shè)置為另一個文件路徑,可以覆蓋此證書路徑:
$ export REQUESTS_CA_BUNDLE="/usr/local/myproxy_info/cacert.pem" $ export https_proxy="http://10.10.1.10:1080" $ python >>> import requests >>> requests.get('https://example.org')
SOCKS
版本2.10.0中新增
除了基本的HTTP代理之外,requests還支持使用SOCKS協(xié)議的代理。這是一項(xiàng)可選功能,要求在使用前安裝其他第三方庫
可通過pip
獲取該功能需要的依賴:
$ python -m pip install requests[socks]
安裝依賴后,使用SOCKS代理就同使用HTTP代理一樣簡單:
proxies = { 'http': 'socks5://user:pass@host:port', 'https': 'socks5://user:pass@host:port' }
使用 socks5
會導(dǎo)致DNS解析發(fā)生在客戶端上,而不是代理服務(wù)器上。這與curl
保持一致,curl使用scheme來決定是在客戶端還是代理服務(wù)器上進(jìn)行DNS解析。如果要解析代理服務(wù)器上的域,請使用socks5h
作為scheme
編碼
當(dāng)收到響應(yīng)時,并訪問 Response.text
屬性時,requests會猜測用于解碼響應(yīng)體的編碼。requests將首先檢查HTTP請求頭中的編碼,如果不存在,則使用charset_normalizer或chardet嘗試猜測編碼。
如果安裝了chardet
,requests
將使用它,但對于python3來說,chardet
不再是強(qiáng)制依賴項(xiàng)。
當(dāng)安裝requests
時,沒有指定 [use_chardet_on_py3]
,并且chardet
尚未安裝時,requests將使用charset normalizer
來猜測編碼。
requests不會猜測編碼的唯一情況是HTTP請求頭中不存在顯示字符集且Content-Type
請求頭包含text
。在這種情況下,RFC 2616指定默認(rèn)字符集必須是ISO-8859-1
。requests遵循該規(guī)范。如果需要不同的編碼,您可以手動設(shè)置Response.conding
屬性,或使用原始Response.content
。
Link請求頭
許多HTTP API具有l(wèi)ink請求頭。它們使API更加自我描述和可發(fā)現(xiàn)。
GitHub 在API中將這些用于分頁
>>> url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10' >>> r = requests.head(url=url) >>> r.headers['link'] '<https://api.github.com/user/119893/repos?page=2&per_page=10>; rel="next", <https://api.github.com/user/119893/repos?page=5&per_page=10>; rel="last"'
requests 將自動解析這link請求頭并且讓它們更容易被使用:
>>> r.links["next"] {'url': 'https://api.github.com/user/119893/repos?page=2&per_page=10', 'rel': 'next'} >>> r.links["last"] {'url': 'https://api.github.com/user/119893/repos?page=5&per_page=10', 'rel': 'last'}
傳輸適配器(Transport Adapters)
從v1.0.0開始,requests 已模塊化內(nèi)部設(shè)計(jì)。這樣做的部分原因是為了實(shí)現(xiàn)傳輸適配器,最初在此處描述. 傳輸適配器提供了一種機(jī)制來定義某個HTTP服務(wù)的交互方法。特別是,它們允許你應(yīng)用每個服務(wù)的配置。
requests附帶單個傳輸適配器HTTPAdapter
. 此適配器使用功能強(qiáng)大的urllib3
提供與HTTP和HTTPS的默認(rèn)請求交互。當(dāng)初始化 requests Session
時,其中一個附加到Session
對象表示HTTP,一個表示HTTPS。
戶能夠創(chuàng)建和使用自己的具備特定功能的傳輸適配器。一旦創(chuàng)建,傳輸適配器就可以加載到會話對象,并指示它應(yīng)該應(yīng)用于哪些web服務(wù)。
>>> s = requests.Session() >>> s.mount('https://github.com/', MyAdapter())
上述mount
調(diào)用將傳輸適配器的指定實(shí)例注冊到URL前綴中。一旦掛載,使用該session發(fā)起的,URL以給定前綴開頭的任何HTTP請求都將使用給定的傳輸適配器。
實(shí)現(xiàn)傳輸適配器的許多細(xì)節(jié)超出了本文檔的范圍,但是可以看下一個簡單SSL使用示例。除此之外,您還可以考慮繼承BaseAdapter
實(shí)現(xiàn)子類適配器。
示例: 指定SSL版本
The requests team has made a specific choice to use whatever SSL version is default in the underlying library (urllib3). Normally this is fine, but from time to time, you might find yourself needing to connect to a service-endpoint that uses a version that isn’t compatible with the default.
You can use Transport Adapters for this by taking most of the existing implementation of HTTPAdapter, and adding a parameter ssl_version that gets passed-through to urllib3. We’ll make a Transport Adapter that instructs the library to use SSLv3:
默認(rèn)情況下,requests選擇使用底層urllib3
庫中默認(rèn)的SSL版本。 通常情況下,這是可以的,但有時,您可能會發(fā)現(xiàn)自己需要連接到使用與默認(rèn)版本不兼容的SSL版本的服務(wù)端。
為此,可以通過繼承HTTPAdapter
實(shí)現(xiàn)自定義傳輸適配器,
示例:編寫一個適配器,指示庫使用SSLv3:
import ssl from urllib3.poolmanager import PoolManager from requests.adapters import HTTPAdapter class Ssl3HttpAdapter(HTTPAdapter): """"Transport adapter" that allows us to use SSLv3.""" def init_poolmanager(self, connections, maxsize, block=False): self.poolmanager = PoolManager( num_pools=connections, maxsize=maxsize, block=block, ssl_version=ssl.PROTOCOL_SSLv3)
阻塞或非阻塞
有了默認(rèn)的傳輸適配器,requests就不會提供任何類型的非阻塞IO。Response.content
屬性將阻塞,直到下載完整個響應(yīng)為止。如果你需要更大的粒度,則庫的流式傳輸功能(請參閱流式傳輸請求)允許單次接收較小數(shù)量的響應(yīng)那日。然而,這些調(diào)用仍然是阻塞。
如果您關(guān)心阻塞IO的使用,那么有很多項(xiàng)目將請求與Python的異步框架結(jié)合在一起。一些很好的例子是 requests-threads, grequests, requests-futures, 和httpx.
超時
大多數(shù)對外部服務(wù)器的請求都應(yīng)該附加超時,以防服務(wù)器沒有及時響應(yīng)。默認(rèn)情況下,除非顯式設(shè)置了超時時間,否則requests不會超時。如果沒有超時,你的代碼可能會掛起幾分鐘或更長時間。
連接超時是requests等待客戶端建立與遠(yuǎn)程計(jì)算機(jī)的socke連接的秒數(shù)。將連接超時設(shè)置為略大于3的倍數(shù)是一種很好的做法,因?yàn)?秒是默認(rèn)的TCP數(shù)據(jù)包重傳窗口.
一旦客戶端連接到服務(wù)器并發(fā)送HTTP請求后,讀取超時是客戶端等待服務(wù)器返回響應(yīng)的秒數(shù)(具體來說,這是客戶端等待服務(wù)器返回字節(jié)數(shù)據(jù)的秒數(shù)。在99.9%的情況下,這是服務(wù)器返回第一個字節(jié)之前的等待時間)。
如果需要為請求設(shè)置一個超時時間,可以為timeout
參數(shù)指定一個具體的時間值:
r = requests.get('https://github.com', timeout=5)
該超時時間將同時應(yīng)用于連接超時和讀取超時。如果想為連接超時和讀取超時分別設(shè)置不同的等待時間,可以指定一個元組:
r = requests.get('https://github.com', timeout=(3.05, 27))
如果服務(wù)很慢,想讓requests一直等待響應(yīng)直到獲取響應(yīng),可以指定timeout
參數(shù)值為None
r = requests.get('https://github.com', timeout=None)
到此這篇關(guān)于Python Requests庫學(xué)習(xí)總結(jié)的文章就介紹到這了,更多相關(guān)Python Requests庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python pymysql peewee關(guān)于時區(qū)問題分析
這篇文章主要為大家介紹了python pymysql peewee關(guān)于時區(qū)問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Python?Traceback(most?recent?call?last)報(bào)錯信息:示例解讀
這篇文章主要介紹了Python?Traceback(most?recent?call?last)報(bào)錯信息:示例解讀,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12python如何實(shí)現(xiàn)一個刷網(wǎng)頁小程序
這篇文章主要給大家介紹了關(guān)于利用python如何實(shí)現(xiàn)一個刷網(wǎng)頁小程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11學(xué)會迭代器設(shè)計(jì)模式,幫你大幅提升python性能
這篇文章主要介紹了python 迭代器設(shè)計(jì)模式的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01tensorflow中tf.reduce_mean函數(shù)的使用
這篇文章主要介紹了tensorflow中tf.reduce_mean函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04pandas對齊運(yùn)算的實(shí)現(xiàn)示例
本文主要介紹了pandas對齊運(yùn)算的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10ubuntu17.4下為python和python3裝上pip的方法
今天小編就為大家分享一篇ubuntu17.4下為python和python3裝上pip的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06使用Python實(shí)現(xiàn)操作控制鼠標(biāo)和鍵盤
Python 有很多的庫可以實(shí)現(xiàn)各種各樣的功能,比如使用 pynput 操作,下面小編就來和大家詳細(xì)介紹一下如何使用pynput進(jìn)行操作控制鼠標(biāo)和鍵盤吧2024-02-02Python 圖形界面框架TkInter之在源碼中找pack方法
這篇文章主要介紹了Python 圖形界面框架TkInter之在源碼中找pack方法,根據(jù)不同的需求,選擇對應(yīng)的布局方式,每個控件都可以使用pack作為布局管理,下面我們就來看看從源碼中找pack()在哪里,需要的朋友可以參考下2022-03-03python代碼 FTP備份交換機(jī)配置腳本實(shí)例解析
這篇文章主要介紹了python代碼 FTP備份交換機(jī)配置腳本實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08