国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內(nèi)容

詳解Python之urllib爬蟲、request模塊和parse模塊

極客小將2021-03-23-
簡介文章目錄urllibrequest模塊訪問URLRequest類其他類parse模塊解析URL轉(zhuǎn)義URLrobots.txt文件(免費(fèi)學(xué)習(xí)推薦:python視頻教程)urlliburllib是Python中用來處理URL的工具包,源碼位于/Lib/下。它包含了幾個(gè)模塊:用于打開及讀寫的urls的re
TWM少兒編程網(wǎng)-https://www.pxcodes.com

文章目錄TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllibrequest模塊訪問URLRequest類其他類parse模塊解析URL轉(zhuǎn)義URLrobots.txt文件

(免費(fèi)學(xué)習(xí)推薦:python視頻教程)TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllibTWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib是Python中用來處理URL的工具包,源碼位于/Lib/下。它包含了幾個(gè)模塊:用于打開及讀寫的urls的request模塊、由request模塊引起異常的error模塊、用于解析urls的parse模塊、用于響應(yīng)處理的response模塊、分析robots.txt文件的robotparser模塊。TWM少兒編程網(wǎng)-https://www.pxcodes.com

注意版本差異。urllib有3個(gè)版本:Python2.X包含urllib、urllib2模塊,Python3.X把urllib、urllib2以及urlparse合成到urllib包中,而urllib3是新增的第三方工具包。若遇到"No module named urllib2"等問題幾乎都是Python版本不同導(dǎo)致的。TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib3是一個(gè)功能強(qiáng)大、條例清晰、用于HTTP客戶端的Python庫。它提供了許多Python標(biāo)準(zhǔn)庫里所沒有的特性:壓縮編碼、連接池、線程安全、SSL/TLS驗(yàn)證、HTTP和SCOCKS代理等。可以通過pip進(jìn)行安裝:pip install urllib3

也可以通過GitHub下載**新代碼:TWM少兒編程網(wǎng)-https://www.pxcodes.com

git clone git://github.com/shazow/urllib3.git python setup.py install

urllib3參考文檔:https://urllib3.readthedocs.io/en/latest/TWM少兒編程網(wǎng)-https://www.pxcodes.com

request模塊TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib.request模塊定義了身份認(rèn)證、重定向、cookies等應(yīng)用中打開url的函數(shù)和類。TWM少兒編程網(wǎng)-https://www.pxcodes.com

再簡單介紹下request包,該包用于高級的非底層的HTTP客戶端接口,容錯(cuò)能力比request模塊強(qiáng)大。request使用的是urllib3,它繼承了urllib2的特性,支持HTTP連接保持和連接池,支持使用cookie保持會(huì)話、文件上傳、自動(dòng)解壓縮、Unicode響應(yīng)、HTTP(S)代理等。更多具體可參考文檔http://requests.readthedocs.io。TWM少兒編程網(wǎng)-https://www.pxcodes.com

下面將介紹urllib.request模塊常用函數(shù)和類。TWM少兒編程網(wǎng)-https://www.pxcodes.com

訪問URLTWM少兒編程網(wǎng)-https://www.pxcodes.com

一、urlopen()TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=false,context=None)

該函數(shù)用于抓取URL數(shù)據(jù),十分重要。帶有如上所示的參數(shù),除了URL參數(shù)外(字符串或Request對象)其余參數(shù)都有默認(rèn)值。TWM少兒編程網(wǎng)-https://www.pxcodes.com

①URL參數(shù)TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib import requestwith request.urlopen("http://www.baidu.com") as f: print(f.status) print(f.getheaders())#運(yùn)行結(jié)果如下200[('Bdpagetype', '1'), ('Bdqid', '0x8583c98f0000787e'), ('Cache-Control', 'private'), ('Content-Type', 'text/html;charset=utf-8'), ('Date', 'Fri, 19 Mar 2021 08:26:03 GMT'), ('Expires', 'Fri, 19 Mar 2021 08:25:27 GMT'), ('P3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('P3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('Server', 'BWS/1.1'), ('Set-Cookie', 'BAIDUID=B050D0981EE3A706D726852655C9FA21:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'BIDUPSID=B050D0981EE3A706D726852655C9FA21; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'PSTM=1616142363; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'BAIDUID=B050D0981EE3A706FA20DF440C89F27F:FG=1; max-age=31536000; expires=Sat, 19-Mar-22 08:26:03 GMT; domain=.baidu.com; path=/; version=1; comment=bd'), ('Set-Cookie', 'BDSVRTM=0; path=/'), ('Set-Cookie', 'BD_HOME=1; path=/'), ('Set-Cookie', 'H_PS_PSSID=33272_33710_33690_33594_33600_33624_33714_33265; path=/; domain=.baidu.com'), ('Traceid', '161614236308368819309620754845011048574'), ('Vary', 'Accept-Encoding'), ('Vary', 'Accept-Encoding'), ('X-Ua-Compatible', 'IE=Edge,chrome=1'), ('Connection', 'close'), ('Transfer-Encoding', 'chunked')]

②data參數(shù)
如果向服務(wù)器發(fā)送數(shù)據(jù),那么data參數(shù)必須是一個(gè)有數(shù)據(jù)的 byes對象,否則為None。在Python3.2之后可以是一個(gè)iterable對象。若是,則headers中必須帶有Content-Length參數(shù)。HTTP請求使用POST方法時(shí),data必須有數(shù)據(jù);使用GET方法時(shí),data寫None即可。TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib import parsefrom urllib import request data = bytes(parse.urlencode({"pro":"value"}),encoding="utf8")response = request.urlopen("http://httpbin.org/post",data=data)print(response.read())#運(yùn)行結(jié)果如下b'{ "args": {}, "data": "", "files": {}, "form": { "pro": "value" }, "headers": { "Accept-Encoding": "identity", "Content-Length": "9", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.9", "X-Amzn-Trace-Id": "Root=1-60545f5e-7428b29435ce744004d98afa" }, "json": null, "origin": "112.48.80.243", "url": "http://httpbin.org/post" } '

對數(shù)據(jù)進(jìn)行POST請求,需要轉(zhuǎn)碼bytes類型或者iterable類型。這里通過bytes()進(jìn)行字節(jié)轉(zhuǎn)換,考慮到第一個(gè)參數(shù)為字符串,所以需要利用parse模塊(下面會(huì)講)的urlencode()方法對上傳的數(shù)據(jù)進(jìn)行字符串轉(zhuǎn)換,同時(shí)指定編碼格式為utf8。測試網(wǎng)址httpbin.org可以提供HTTP測試,從返回的內(nèi)容可以看出提交以表單form作為屬性、以字典作為屬性值。TWM少兒編程網(wǎng)-https://www.pxcodes.com

③timeout參數(shù)
該參數(shù)是可選的,以秒為單位指定一個(gè)超時(shí)時(shí)間,若超過該時(shí)間則任何操作都會(huì)被阻止,如果沒有指定,那么默認(rèn)會(huì)取sock.GLOBAL_DEFAULT_TIMEOUT對應(yīng)的值。該參數(shù)僅對http、https、ftp連接有效。TWM少兒編程網(wǎng)-https://www.pxcodes.com

超時(shí)后會(huì)拋出urllib.error.URLError:<urlopen error time out>異常,可以用try…except來處理異常。TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib import request response = request.urlopen("http://httpbin.org/get",timeout=1)print(response.read())#運(yùn)行結(jié)果如下b'{ "args": {}, "headers": { "Accept-Encoding": "identity", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.9", "X-Amzn-Trace-Id": "Root=1-605469dd-76a6d963171127c213d9a9ab" }, "origin": "112.48.80.243", "url": "http://httpbin.org/get" } '

④返回對象的常用方法和屬性
除了前三個(gè)urlopen()常用參數(shù)外,該函數(shù)返回用作context manager(上下文管理器)的類文件對象,并包含如下方法:TWM少兒編程網(wǎng)-https://www.pxcodes.com

geturl():返回請求的URL,通常重定向后的URL照樣能獲取到info():返回httplib.HTTPMessage對象,表示遠(yuǎn)程服務(wù)器返回的頭信息getcode():返回響應(yīng)后的HTTP狀態(tài)碼status屬性:返回響應(yīng)后的HTTP狀態(tài)碼msg屬性:請求結(jié)果from urllib import request response = request.urlopen("http://httpbin.org/get")print(response.geturl())print("===========")print(response.info())print("===========")print(response.getcode())print("===========")print(response.status)print("===========")print(response.msg)

運(yùn)行結(jié)果:

附:狀態(tài)碼對應(yīng)狀態(tài)TWM少兒編程網(wǎng)-https://www.pxcodes.com

1xx(informational):請求已經(jīng)收到,正在進(jìn)行中。2xx(successful):請求成功接收,解析,完成。3xx(Redirection):需要重定向。4xx(Client Error):客戶端問題,請求存在語法錯(cuò)誤,網(wǎng)址未找到。5xx(Server Error):服務(wù)器問題。

二、build_opener()TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib.request.build_opener([handler1 [handler2, ...]])

該函數(shù)不支持驗(yàn)證、cookie及其他HTTP高級功能。要支持這些功能必須使用build_opener()函數(shù)自定義OpenerDirector對象,稱之為Opener。TWM少兒編程網(wǎng)-https://www.pxcodes.com

build_opener()函數(shù)返回的是OpenerDirector實(shí)例,而且是按給定的順序鏈接處理程序的。作為OpenerDirector實(shí)例,可以從OpenerDirector類的定義看出他具有addheaders、handlers、handle_open、add_handler()、open()、close()等屬性或方法。open()方法與urlopen()函數(shù)的功能相同。

上述代碼通過修改http報(bào)頭進(jìn)行HTTP高級功能操作,然后利用返回對象open()進(jìn)行請求,返回結(jié)果與urlopen()一樣,只是內(nèi)存位置不同而已。TWM少兒編程網(wǎng)-https://www.pxcodes.com

實(shí)際上urllib.request.urlopen()方法只是一個(gè)Opener,如果安裝啟動(dòng)器沒有使用urlopen啟動(dòng),調(diào)用的就是OpenerDirector.open()方法。那么如何設(shè)置默認(rèn)全局啟動(dòng)器呢?就涉及下面的install_opener函數(shù)。TWM少兒編程網(wǎng)-https://www.pxcodes.com

三、install_opener()TWM少兒編程網(wǎng)-https://www.pxcodes.com

urllib.request.install_opener(opener)

安裝OpenerDirector實(shí)例作為默認(rèn)全局啟動(dòng)器。
首先導(dǎo)入request模塊,實(shí)例化一個(gè)HTTPBasicAuthHandler對象,然后通過利用add_password()添加用戶名和密碼來創(chuàng)建一個(gè)認(rèn)證處理器,利用urllib.request.build_opener()方法來調(diào)用該處理器以構(gòu)建Opener,并使其作為默認(rèn)全局啟動(dòng)器,這樣Opener在發(fā)生請求時(shí)具備了認(rèn)證功能。通過Opener的open()方法打開鏈接完成認(rèn)證。TWM少兒編程網(wǎng)-https://www.pxcodes.com


當(dāng)然了,CSDN不需要賬號密碼也可以訪問,讀者還可以在其他網(wǎng)站上用自己的賬號進(jìn)行測試。TWM少兒編程網(wǎng)-https://www.pxcodes.com

除了上述方法外,還有將路徑轉(zhuǎn)換為URL的pathname2url(path)、將URL轉(zhuǎn)換為路徑的url2pathname(path),以及返回方案至代理服務(wù)器URL映射字典的getproxies()等方法。TWM少兒編程網(wǎng)-https://www.pxcodes.com

Request類TWM少兒編程網(wǎng)-https://www.pxcodes.com

前面介紹的urlopen()方法可以滿足一般基本URL請求,如果需要添加headers信息,就要考慮更為強(qiáng)大的Request類了。Request類是URL請求的抽象,包含了許多參數(shù),并定義了一系列屬性和方法。TWM少兒編程網(wǎng)-https://www.pxcodes.com

一、定義TWM少兒編程網(wǎng)-https://www.pxcodes.com

class urllib.request.Request(url,data=None,headers={},origin_req_host=None,unverifiable=False,method=None)參數(shù)url是有效網(wǎng)址的字符串,同urlopen()方法中一樣,data參數(shù)也是。headers是一個(gè)字典,可以通過add_header()以鍵值進(jìn)行調(diào)用。通常用于爬蟲爬取數(shù)據(jù)時(shí)或者Web請求時(shí)更改User-Agent標(biāo)頭值參數(shù)來進(jìn)行請求。origin_req_host是原始請求主機(jī),比如請求的是針對HTML文檔中的圖像的,則該請求主機(jī)是包含圖像頁面所在的主機(jī)。Unverifiable指示請求是否是無法驗(yàn)證的。method指示使用的是HTTP請求方法。常用的有GET、POST、PUT、DELETE等,

代碼示例:TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib import requestfrom urllib import parse data = parse.urlencode({"name":"baidu"}).encode('utf-8')headers = {'User-Agent':'wzlodq'}req = request.Request(url="http://httpbin.org/post",data=data,headers=headers,method="POST")response = request.urlopen(req)print(response.read())#運(yùn)行結(jié)果如下b'{ "args": {}, "data": "", "files": {}, "form": { "name": "baidu" }, "headers": { "Accept-Encoding": "identity", "Content-Length": "10", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "wzlodq", "X-Amzn-Trace-Id": "Root=1-605491a4-1fcf3df01a8b3c3e22b5edce" }, "json": null, "origin": "112.48.80.34", "url": "http://httpbin.org/post" } '

注意data參數(shù)和前面一樣需是字節(jié)流類型的,不同的是調(diào)用Request類進(jìn)行請求。TWM少兒編程網(wǎng)-https://www.pxcodes.com

二、屬性方法TWM少兒編程網(wǎng)-https://www.pxcodes.com

①Request.full_url
full_url屬性包含setter、getter和deleter。如果原始請求URL片段存在,那么得到的full_url將返回原始請求的URL片段,通過添加修飾器@property將原始URL傳遞給構(gòu)造函數(shù)。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

②Request.type:獲取請求對象的協(xié)議類型。
③Request.host:獲取URL主機(jī),可能含有端口的主機(jī)。
④Request.origin_req_host:發(fā)出請求的原始主機(jī),沒有端口。
⑤Request.get_method():返回顯示HTTP請求方法的字符串。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

(插播反爬信息 )博主CSDN地址:https://wzlodq.blog.csdn.net/TWM少兒編程網(wǎng)-https://www.pxcodes.com

⑥Request.add_header(key,val):向請求中添加標(biāo)頭。 TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib import requestfrom urllib import parse data = bytes(parse.urlencode({'name':'baidu'}),encoding='utf-8')req = request.Request('http://httpbin.org/post',data,method='POST')req.add_header('User-agent','test')response = request.urlopen(req)print(response.read().decode('utf-8'))


上述代碼中,通過add_header()傳入了User-Agent,在爬蟲過程中,常常通過循環(huán)調(diào)用該方法來添加不同的User-Agent進(jìn)行請求,避免服務(wù)器針對某一User-Agent的禁用。TWM少兒編程網(wǎng)-https://www.pxcodes.com

其他類TWM少兒編程網(wǎng)-https://www.pxcodes.com

BaseHandler為所有注冊處理程序的基類,并且只處理注冊的簡單機(jī)制,從定義上看,BaseHandler提供了一個(gè)添加基類的add_parent()方法,后面介紹的類都是繼承該類操作的。TWM少兒編程網(wǎng)-https://www.pxcodes.com

HTTPErrorProcessor:用于HTTP錯(cuò)誤響應(yīng)過程。HTTPDefaultErrorHandler:用于處理HTTP響應(yīng)錯(cuò)誤。ProxyHandler:用于設(shè)置代理。HTTPRedirectHandler:用于設(shè)置重定向。HTTPCookieProcessor:用于處理cookie。HEEPBasicAuthHandler:用于管理認(rèn)證。

parse模塊TWM少兒編程網(wǎng)-https://www.pxcodes.com

parse模塊用于分解URL字符串為各個(gè)組成部分,包括尋址方案、網(wǎng)絡(luò)位置、路徑等,也可將這些部分組成URL字符串,同時(shí)可以對“相對URL"進(jìn)行轉(zhuǎn)換等。TWM少兒編程網(wǎng)-https://www.pxcodes.com

解析URLTWM少兒編程網(wǎng)-https://www.pxcodes.com

一、urllib.parse.urlparse(urlstring,scheme=’’,allow_fragments=True)
解析URL為6個(gè)部分,即返回一個(gè)6元組(tuple子類的實(shí)例),tuple類具有下標(biāo)所示的屬性:TWM少兒編程網(wǎng)-https://www.pxcodes.com

屬性說明對應(yīng)下標(biāo)指數(shù)不存在時(shí)的取值schemeURL方案說明符 0scheme參數(shù)
netloc網(wǎng)絡(luò)位置部分1空字符串path分層路徑2空字符串params**后路徑元素的參數(shù)3空字符串query查詢組件4空字符串fragment片段標(biāo)識符5空字符串username用戶名
Nonepassword密碼
Nonehostname主機(jī)名
Noneport端口號
None

**后組成的URL結(jié)構(gòu)為scheme://netloc/path;parameters?query#fragment
舉個(gè)栗子:TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib.parse import *res = urlparse('https://wzlodq.blog.csdn.net/article/details/113597816')print(res)print(res.scheme)print(res.netloc)print(res.path)print(res.params)print(res.query)print(res.username)print(res.password)print(res.hostname)print(res.port)print(res.geturl())print(tuple(res))print(res[0])print(res[1])print(res[2])


需要注意的是urlparse有時(shí)并不能很好地識別netloc,它會(huì)假定相對URL以路徑分量開始,將其取值放在path中。TWM少兒編程網(wǎng)-https://www.pxcodes.com

二、urllib.parse.urlunparse(parts)
是urlparse()的逆操作,即將urlparse()返回的原則構(gòu)建一個(gè)URL。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

三、urllib.parse.urlsplit(urlstring,scheme=’’.allow_fragments=True)
類似urlparse(),但不會(huì)分離參數(shù),即返回的元組對象沒有params元素,是一個(gè)五元組,對應(yīng)下標(biāo)指數(shù)也發(fā)生了改變。TWM少兒編程網(wǎng)-https://www.pxcodes.com

from urllib.parse import *sp = urlsplit('https://wzlodq.blog.csdn.net/article/details/113597816')print(sp)#運(yùn)行結(jié)果如下SplitResult(scheme='https', netloc='wzlodq.blog.csdn.net', path='/article/details/113597816', query='', fragment='')

四、urllib.parse.urlunsplit(parts)
類似urlunparse(),是urlsplit()的逆操作,不再贅述。TWM少兒編程網(wǎng)-https://www.pxcodes.com

五、urllib.parse.urljoin(base,url,allow_fragments=True)
該函數(shù)主要組合基本網(wǎng)址(base)與另一個(gè)網(wǎng)址(url)以構(gòu)建新的完整網(wǎng)址。
相對路徑和絕對路徑的url組合是不同的,而且相對路徑是以**后部分路徑進(jìn)行替換處理的:
TWM少兒編程網(wǎng)-https://www.pxcodes.com

六、urllib.parse.urldefrag(url)
根據(jù)url進(jìn)行分割,如果url包含片段標(biāo)識符,就返回url對應(yīng)片段標(biāo)識符前的網(wǎng)址,fragment取片段標(biāo)識符后的值。如果url沒有片段標(biāo)識符,那么fragment為空字符串。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

轉(zhuǎn)義URLTWM少兒編程網(wǎng)-https://www.pxcodes.com

URL轉(zhuǎn)義可以避免某些字符引起歧義,通過引用特殊字符并適當(dāng)編排非ASCII文本使其作為URL組件安全使用。同時(shí)也支持反轉(zhuǎn)這些操作,以便從URL組件內(nèi)容重新創(chuàng)建原始數(shù)據(jù)。TWM少兒編程網(wǎng)-https://www.pxcodes.com

一、urllib.parse.quote(string,safe=’/’,encoding=None,errors=None)
使用%xx轉(zhuǎn)義替換string中的特殊字符,其中字母、數(shù)字和字符’_.-‘不會(huì)進(jìn)行轉(zhuǎn)義。默認(rèn)情況下,此函數(shù)用于轉(zhuǎn)義URL的路徑部分,可選的safe參數(shù)指定不應(yīng)轉(zhuǎn)義的其他ASCII字符——其默認(rèn)值為’/’。

特別注意的是若string是bytes,encoding和errors就無法指定,否則報(bào)錯(cuò)TypeError。TWM少兒編程網(wǎng)-https://www.pxcodes.com

二、urllib.parse.unquote(string,encoding=‘utf-8’,errors=‘replace’)
該函數(shù)時(shí)quote()的逆操作,即將%xx轉(zhuǎn)義為等效的單字符。參數(shù)encoding和errors用來指定%xx編碼序列解碼為Unicode字符,同bytes.decode()方法。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

三、urllib.parse.quote_plus(string,safe=’’,encoding=None,errors=None)
該函數(shù)時(shí)quote()的增強(qiáng)版,與之不同的是用+替換空格,而且如果原始URL有字符,那么+將被轉(zhuǎn)義。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

四、urllib.parse.unquote_plus(string,encoding=‘utf-8’,errors=‘replace’)
類似unquote(),不再贅述。TWM少兒編程網(wǎng)-https://www.pxcodes.com

五、urllib.parse.urlencode(query,doseq=False,safe=’’,encoding=None,errors=None,quote_via=quote_plus)
該函數(shù)前面提到過,通常在使用HTTP進(jìn)行POST請求傳遞的數(shù)據(jù)進(jìn)行編碼時(shí)使用。
TWM少兒編程網(wǎng)-https://www.pxcodes.com

robots.txt文件TWM少兒編程網(wǎng)-https://www.pxcodes.com

robotparser模塊很簡單,僅定義了3個(gè)類(RobotFileParser、RuleLine、Entry)。從__all__屬性來看也就RobotFileParser一個(gè)類(用于處理有關(guān)特定用戶代理是否可以發(fā)布robots.txt文件的網(wǎng)站上提前網(wǎng)址內(nèi)容)。TWM少兒編程網(wǎng)-https://www.pxcodes.com

robots文件類似一個(gè)協(xié)議文件,搜索引擎訪問網(wǎng)站時(shí)查看的第一個(gè)文件,會(huì)告訴爬蟲或者蜘蛛程序在服務(wù)器上可以查看什么文件。TWM少兒編程網(wǎng)-https://www.pxcodes.com

RobotFileParser類有一個(gè)url參數(shù),常用以下方法:TWM少兒編程網(wǎng)-https://www.pxcodes.com

set_url():用來設(shè)置指向robots.txt文件的網(wǎng)址。read():讀取robots.txt網(wǎng)址,并將其提供給解析器。parse():用來解析robots.txt文件。can_fetch():用來判斷是否可提前url。mtime():返回上次抓取robots.txt文件的時(shí)間。modified():將上次抓取robots.txt文件的時(shí)間設(shè)置為當(dāng)前時(shí)間。

TWM少兒編程網(wǎng)-https://www.pxcodes.com

大量免費(fèi)學(xué)習(xí)推薦,敬請?jiān)L問python教程(視頻)TWM少兒編程網(wǎng)-https://www.pxcodes.com

以上就是詳解Python之urllib爬蟲、request模塊和parse模塊的詳細(xì)內(nèi)容,更多請關(guān)注少兒編程網(wǎng)其它相關(guān)文章!TWM少兒編程網(wǎng)-https://www.pxcodes.com

預(yù)約試聽課

已有385人預(yù)約都是免費(fèi)的,你也試試吧...

主站蜘蛛池模板: 高平市| 甘肃省| 昆山市| 柳江县| 山阴县| 昌宁县| 抚松县| 海原县| 土默特右旗| 盐山县| 丰县| 云浮市| 洛扎县| 旺苍县| 渝中区| 廉江市| 韶山市| 龙井市| 九寨沟县| 金阳县| 静海县| 谢通门县| 宿迁市| 临清市| 桂东县| 大邑县| 会泽县| 策勒县| 滦平县| 临城县| 南召县| 湘潭市| 颍上县| 新干县| 盐边县| 科技| 含山县| 台南县| 东安县| 吉木萨尔县| 潍坊市|