摘要:百度云搜索搜網(wǎng)盤利用系統(tǒng)自帶的庫寫簡單爬蟲獲取一個的源碼讀出源碼內(nèi)容將字節(jié)轉(zhuǎn)化成字符串正則獲取頁面指定內(nèi)容獲取源碼學(xué)院實(shí)戰(zhàn)群正則規(guī)則,獲取到號將網(wǎng)絡(luò)文件下載保存到本地,參數(shù)網(wǎng)絡(luò)文件,參數(shù)保存路徑
【百度云搜索:http://www.lqkweb.com】 【搜網(wǎng)盤:http://www.swpan.cn】
利用python系統(tǒng)自帶的urllib庫寫簡單爬蟲
urlopen()獲取一個URL的html源碼
read()讀出html源碼內(nèi)容
decode("utf-8")將字節(jié)轉(zhuǎn)化成字符串
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html").read().decode("utf-8") print(html)
正則獲取頁面指定內(nèi)容
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request import re html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html").read().decode("utf-8") #獲取html源碼 pat = "51CTO學(xué)院Python實(shí)戰(zhàn)群((d*?))" #正則規(guī)則,獲取到QQ號 rst = re.compile(pat).findall(html) print(rst) #["325935753"]
urlretrieve()將網(wǎng)絡(luò)文件下載保存到本地,參數(shù)1網(wǎng)絡(luò)文件URL,參數(shù)2保存路徑
#!/usr/bin/env python # -*- coding:utf-8 -*- from urllib import request import re import os file_path = os.path.join(os.getcwd() + "/222.html") #拼接文件保存路徑 # print(file_path) request.urlretrieve("http://edu.51cto.com/course/8360.html", file_path) #下載這個文件保存到指定路徑
urlcleanup()清除爬蟲產(chǎn)生的內(nèi)存
#!/usr/bin/env python # -*- coding:utf-8 -*- from urllib import request import re import os file_path = os.path.join(os.getcwd() + "/222.html") #拼接文件保存路徑 # print(file_path) request.urlretrieve("http://edu.51cto.com/course/8360.html", file_path) #下載這個文件保存到指定路徑 request.urlcleanup()
info()查看抓取頁面的簡介
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request import re html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html") #獲取html源碼 a = html.info() print(a) # C:UsersadminAppDataLocalProgramsPythonPython35python.exe H:/py/15/chshi.py # Date: Tue, 25 Jul 2017 16:08:17 GMT # Content-Type: text/html; charset=UTF-8 # Transfer-Encoding: chunked # Connection: close # Set-Cookie: aliyungf_tc=AQAAALB8CzAikwwA9aReq63oa31pNIez; Path=/; HttpOnly # Server: Tengine # Vary: Accept-Encoding # Vary: Accept-Encoding # Vary: Accept-Encoding
getcode()獲取狀態(tài)碼
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request import re html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html") #獲取html源碼 a = html.getcode() #獲取狀態(tài)碼 print(a) #200
geturl()獲取當(dāng)前抓取頁面的URL
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request import re html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html") #獲取html源碼 a = html.geturl() #獲取當(dāng)前抓取頁面的URL print(a) #http://edu.51cto.com/course/8360.html
timeout抓取超時設(shè)置,單位為秒
是指抓取一個頁面時對方服務(wù)器響應(yīng)太慢,或者很久沒響應(yīng),設(shè)置一個超時時間,超過超時時間就不抓取了
#!/usr/bin/env python # -*- coding:utf-8 -*- import urllib.request import re html = urllib.request.urlopen("http://edu.51cto.com/course/8360.html",timeout=30) #獲取html源碼 a = html.geturl() #獲取當(dāng)前抓取頁面的URL print(a) #http://edu.51cto.com/course/8360.html
自動模擬http請求
http請求一般常用的就是get請求和post請求
get請求
比如360搜索,就是通過get請求并且將用戶的搜索關(guān)鍵詞傳入到服務(wù)器獲取數(shù)據(jù)的
所以我們可以模擬百度http請求,構(gòu)造關(guān)鍵詞自動請求
quote()將關(guān)鍵詞轉(zhuǎn)碼成瀏覽器認(rèn)識的字符,默認(rèn)網(wǎng)站不能是中文
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import re gjc = "手機(jī)" #設(shè)置關(guān)鍵詞 gjc = urllib.request.quote(gjc) #將關(guān)鍵詞轉(zhuǎn)碼成瀏覽器認(rèn)識的字符,默認(rèn)網(wǎng)站不能是中文 url = "https://www.so.com/s?q="+gjc #構(gòu)造url地址 # print(url) html = urllib.request.urlopen(url).read().decode("utf-8") #獲取html源碼 pat = "(w*w*w*)" #正則獲取相關(guān)標(biāo)題 rst = re.compile(pat).findall(html) # print(rst) for i in rst: print(i) #循環(huán)出獲取的標(biāo)題 # 官網(wǎng) < em > 手機(jī) < / em > # 官網(wǎng) < em > 手機(jī) < / em > # 官網(wǎng) < em > 手機(jī) < / em > 這么低的價格 # 大牌 < em > 手機(jī) < / em > 低價搶 # < em > 手機(jī) < / em > # 淘寶網(wǎng)推薦 < em > 手機(jī) < / em > # < em > 手機(jī) < / em > # < em > 手機(jī) < / em > # < em > 手機(jī) < / em > # < em > 手機(jī) < / em > # 蘇寧易購買 < em > 手機(jī) < / em > # 買 < em > 手機(jī) < / em > # 買 < em > 手機(jī) < / em >
post請求
urlencode()封裝post請求提交的表單數(shù)據(jù),參數(shù)是字典形式的鍵值對表單數(shù)據(jù)
Request()提交post請求,參數(shù)1是url地址,參數(shù)2是封裝的表單數(shù)據(jù)
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import urllib.parse posturl = "http://www.iqianyue.com/mypost/" shuju = urllib.parse.urlencode({ #urlencode()封裝post請求提交的表單數(shù)據(jù),參數(shù)是字典形式的鍵值對表單數(shù)據(jù) "name": "123", "pass": "456" }).encode("utf-8") req = urllib.request.Request(posturl,shuju) #Request()提交post請求,參數(shù)1是url地址,參數(shù)2是封裝的表單數(shù)據(jù) html = urllib.request.urlopen(req).read().decode("utf-8") #獲取post請求返回的頁面 print(html)
【轉(zhuǎn)載自:http://www.leiqiankun.com/?id=49】
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/44039.html
摘要:百度云搜索搜網(wǎng)盤如果爬蟲沒有異常處理,那么爬行中一旦出現(xiàn)錯誤,程序?qū)⒈罎⑼V构ぷ鳎挟惓L幚砑词钩霈F(xiàn)錯誤也能繼續(xù)執(zhí)行下去常見狀態(tài)碼重定向到新的,永久性重定向到臨時,非永久性請求的資源未更新非法請求請求未經(jīng)授權(quán)禁止訪問沒找到對應(yīng)頁面服務(wù)器內(nèi)部 【百度云搜索:http://www.lqkweb.com】 【搜網(wǎng)盤:http://www.swpan.cn】 如果爬蟲沒有異常處理,那么爬行中一...
摘要:下面我們傳入多個參數(shù)構(gòu)建一個來感受一下在這里我們通過四個參數(shù)構(gòu)造了一個,即請求,在中指定了和,傳遞的參數(shù)用了和方法來轉(zhuǎn)成字節(jié)流,另外指定了請求方式為。運(yùn)行結(jié)果如下通過觀察結(jié)果可以發(fā)現(xiàn),我們成功設(shè)置了,以及。用于處理重定向。 上一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---19、代理基本原理下一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---21、使用Urllib:處理異常 學(xué)習(xí)爬蟲,最初的操作便...
摘要:隨后,為了保險,重啟,火狐瀏覽器也重啟一下,然后開始抓的包,此時你會發(fā)現(xiàn)你的連接并不安全等類似提示已經(jīng)消失,并且已經(jīng)能夠抓包了。 【百度云搜索,搜各種資料:http://www.bdyss.com】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 封裝模塊 #!/usr/bin/env?python #?-*-?coding:?utf-8?-*- import?urll...
摘要:百度云搜索搜網(wǎng)盤淘寶券使用代理格式化,第一個參數(shù),請求目標(biāo)可能是或者對應(yīng)設(shè)置初始化將代理設(shè)置成全局當(dāng)使用請求時自動使用代理引入隨機(jī)模塊文件格式化注意第一個參數(shù)可能是或者,對應(yīng)設(shè)置初始化將代理設(shè)置成全局當(dāng)使用請求時自動使用代理請求 【百度云搜索:http://bdy.lqkweb.com】 【搜網(wǎng)盤:http://www.swpan.cn】 【淘寶券:http://www.tbquan....
摘要:比如我們以知乎為例,直接利用來維持登錄狀態(tài)。測試后,發(fā)現(xiàn)同樣可以正常登錄知乎。上一篇文章網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)基本使用下一篇文章網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)正則表達(dá)式 上一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---24、requests:基本使用下一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---26、正則表達(dá)式 在前面一節(jié)我們了解了 Requests 的基本用法,如基本的 GET、POST 請求以及 Response...
閱讀 3385·2023-04-26 02:42
閱讀 865·2021-10-09 09:41
閱讀 3494·2021-09-06 15:02
閱讀 810·2019-08-26 10:45
閱讀 549·2019-08-23 15:53
閱讀 816·2019-08-22 18:10
閱讀 618·2019-08-22 18:01
閱讀 3582·2019-08-22 17:34