Python爬蟲 反爬蟲第一步

2021-08-25 17:01:06 字數 435 閱讀 7235

request=urllib2.request("",headers=headers)

response=urllib2.urlopen(request)

html=response.read().decode('utf-8')

#print(html)

print(response.getcode())

response 是伺服器響應的類檔案,除了支援檔案操作的方法外,還支援以下常用的方法:

# 返回 http的響應碼,成功返回200,4伺服器頁面出錯,5伺服器問題

print response.getcode()

# 返回 返回實際資料的實際url,防止重定向問題

print response.geturl()

# 返回 伺服器響應的http報頭

print response.info()

爬蟲第一步

注意正規表示式的書寫注意正規表示式的書寫 import re import requests url headers html requests.get url,headers,timeout 10 text print html redata re.compile r for i in re.fi...

爬蟲第一步 獲取資料

在python中,可通過requests庫來獲取資料。windows系統 在cmd命令視窗中輸入 pip install requests mac系統 在terminal終端軟體中輸入 pip3 install requests requests.get 用法如下 引入requests庫 impor...

python爬蟲第一步 獲取頁面資料

第一步html頁面資料 最好是設定下頭部 資訊,不然伺服器檢查到你是用python請求的可能會把你 遮蔽掉 import urllib.request class gethtml object def init self url head self.url url self.head head de...