簡單的請求
from urllib.request import urlopen
#發起網路請求
response = urlopen(
'')assert response.code ==
200print
("請求成功"
)#儲存請求的網頁
#file變數接受open()函式返回的物件的__enter__()返回結果
with
open
('%s.html'
% wd,
'wb')as
file
:file
.write(response.read(
))
urlopen(url,data=none)可以直接發起url的請求,如果data不為空時,則預設時post請求,反之為get請求。
response是http.client.httpresponse類物件。
帶請求頭的請求
from urllib.request import urlopen, request
:#網路資源的介面(url)
url =
''#生成請求物件,封裝請求的url和頭hearder
request = request(url % quote(wd)
, headers=
) response = urlopen(request)
#發起請求
assert response.code ==
200print
("請求成功"
)#讀取響應的資料
bytes_ = response.read(
)with
open
('%s.html'
% wd,
'wb')as
file
:file
.write(bytes_)
此模組又兩個核心函式: python網路請求 爬蟲前奏
惠惠購物助手。資料分析與研究 資料冰山知乎專欄 搶票軟體等。php php是世界是最好的語言,但他天生不是做這個的,而且對多執行緒 非同步支援不是很好,併發處理能力弱。爬蟲是工具性程式,對速度和效率要求比較高。j a 生態圈很完善,是python爬蟲最大的競爭對手。但是j a語言本身很笨重,量很大。...
Python爬蟲基礎 一 簡單的url請求
encoding utf 8 import urllib import urllib.request data是乙個字典,然後通過urllib.parse.urlencode 將data轉換為 wd 904727147 的字串 最後和url合併為full url urllib.request是乙個庫...
Python 爬蟲 請求資料 url編碼和解碼
瀏覽器中包含漢字,需要編碼 錯誤原始碼 查詢語句有中文成功,在瀏覽器中ascii不支援 from urllib import request resp request.urlopen 成功 from home ie utf 8 print resp.read 結果報錯 編碼錯誤 正確原始碼 對電箱的...