課程
/后端開(kāi)發(fā)
/Python
/Python開(kāi)發(fā)簡(jiǎn)單爬蟲(chóng)
大佬們知道咋回事么?
2018-03-16
源自:Python開(kāi)發(fā)簡(jiǎn)單爬蟲(chóng) 6-1
正在回答
print('第三種方法') #通過(guò)cookieJar()類構(gòu)建一個(gè)cookieJar()對(duì)象,用來(lái)保存cookie的值 print('第三種方法') cj?=?cookiejar.CookieJar() #通過(guò)HTTPCookieProcessor()處理器類構(gòu)建一個(gè)處理器對(duì)象,用來(lái)處理cookie cookie_handler?=?urllib.request.HTTPCookieProcessor(cj) #構(gòu)建一個(gè)自定義的opener opener?=?urllib.request.build_opener(cookie_handler) response3?=?urllib.request.urlopen(url) print(response3.getcode()) print(response3.read()) print(cj)
python版本差異導(dǎo)致的,python3需要用urllib.request
import?urllib.request import?http.cookiejar url?=?'http://www.baidu.com' print('第一種方法') response1?=?urllib.request.urlopen(url) print(response1.getcode()) print(len(response1.read())) print("第二種方法") request?=?urllib.request.Request(url) request.add_header("user-agent","Mozilla/5.0") response2?=?urllib.request.urlopen(url) print(response2.getcode()) print(len(response2.read())) print("第三種方法") cj?=?http.cookiejar.CookieJar() opener?=?urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) urllib.request.install_opener(opener) response3?=?urllib.request.urlopen(url) print(response3.getcode()) print(cj) print("網(wǎng)頁(yè)內(nèi)容如下:") print(response3.read())
如果你使用的是python3的話,應(yīng)該輸入urllib3里面urllib與urllib2合并并了例:from urllib import request
舉報(bào)
本教程帶您解開(kāi)python爬蟲(chóng)這門(mén)神奇技術(shù)的面紗
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2019-10-26
2019-10-11
python版本差異導(dǎo)致的,python3需要用urllib.request
2018-06-08
2018-03-16
如果你使用的是python3的話,應(yīng)該輸入urllib
3里面urllib與urllib2合并并了
例:from urllib import request