Python爬虫類----Webダウンロードとurllib 2モジュールと対応する例
2914 ワード
Webダウンロード:インターネット上のURLに対応するWebページをローカルにダウンロードするツールで、爬虫類のコアコンポーネントです.
urllib 2のWebページのダウンロードの3つの方法
対応するインスタンスコードは次のとおりです.
第1の方法-->ダイレクトリクエスト200 4305
2つ目の方法:200 4305
3つ目の方法:200......


urllib 2のWebページのダウンロードの3つの方法

対応するインスタンスコードは次のとおりです.
#coding:utf8
import urllib2
url = 'http://www.baidu.com'
print ' --> '
response1 = urllib2.urlopen(url)
# , 200
print response1.getcode()
#
print len(response1.read() )
第1の方法-->ダイレクトリクエスト200 4305

#coding:utf8
import urllib2
url = 'http://www.baidu.com'
print ' :'
# Request
request= urllib2.Request(url)
# http header
request.add_header('User-Agent' , 'Mozilla/5.0')
#
response2 = urllib2.urlopen(request)
print response2.getcode()
print len(response2.read())
2つ目の方法:200 4305

#coding:utf8
import urllib
import urllib2
import cookielib
url = 'http://www.baidu.com'
print ' :'
# cookie
cj = cookielib.CookieJar()
# 1 opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# urllib2 opener
urllib2.install_opener(opener)
# cookie urllib2
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()
3つ目の方法:200......