資料分析訓練營-urllib實戰與反爬策略-request物件之get請求與URL編碼
阿新 • • 發佈:2020-12-10
req1
import urllib.request as ur
request = ur.Request('https://edu.csdn.net/')
response = ur.urlopen(request).read()
print(response)
req2
import urllib.parse as up
import urllib.request as ur
kw = '動漫'
data = {
'kw':kw,
'ie':'utf-8',
'pn':'100'
}
data_url = up.urlencode(
data
) #編碼
print(data_url)
ret = up.unquote(data_url)#解碼
print(ret)
request = ur.Request('https://tieba.baidu.com/f?'+data_url)#構造物件
response = ur.urlopen(request).read()#得到網頁資料
with open('%s.html' % kw,'wb') as f:
f.write(response)#寫入到本地