課程
/后端開(kāi)發(fā)
/Python
/Python開(kāi)發(fā)簡(jiǎn)單爬蟲(chóng)
運(yùn)行結(jié)果剛開(kāi)始的時(shí)候還跟老師的一樣,幾秒后前面幾行就消失了?
2018-06-21
源自:Python開(kāi)發(fā)簡(jiǎn)單爬蟲(chóng) 5-3
正在回答
我的也是這樣,哥們你怎么解決的??
# coding:utf8
import cookielib
print '第一種方法'
import urllib2
url= "http://www.baidu.com"
response = urllib2.urlopen(url)
print response.getcode()
cont = len(response.read())
print cont
print '第二種方法'
request = urllib2.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib2.urlopen(request)
print response2.getcode()
cont = len(response2.read())
print "第三種方法"
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print response3.read()
print cj
舉報(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)系客服咨詢(xún)優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2019-07-24
我的也是這樣,哥們你怎么解決的??
2018-06-22
# coding:utf8
import cookielib
print '第一種方法'
import urllib2
url= "http://www.baidu.com"
response = urllib2.urlopen(url)
print response.getcode()
cont = len(response.read())
print cont
print '第二種方法'
request = urllib2.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib2.urlopen(request)
print response2.getcode()
cont = len(response2.read())
print cont
print "第三種方法"
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print response3.read()
print cj