學習爬蟲中,從最近自己寫的爬蟲小程式中抓截一點代碼,加深下記憶,
1.因為我已經安裝了Python3,所以使用了urllib3庫,
2.要根據對應網頁的資料格式進行解碼,有的是utf-8,有的是GB2312,當然可能還有其它,否則會報錯,
import urllib3 import time#因為我使用了Python3,所以使用urllib3庫 http = urllib3.PoolManager() response=http.request('GET','https://www.cnblogs.com/') http_data = response.data.decode('utf-8') #GB2312
#在抓取的網頁資料中查找匹配的資料 User = "齊妙非凡" if (http_data.find(User,0,len(http_data)) != -1): print("找到【%s】的隨筆!"%User) else: print("找不到【%s】的隨筆!"%User)
#建立檔案,保存每次抓取到的網頁的內容 data_filename=r'LOG\%s_data.log'%(time.strftime('%Y%m%d%H%M%S')) web_data = open(data_filename,'w',encoding='utf-8') web_data.write(http_data) web_data.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/83312.html
標籤:Python
上一篇:os模塊
