python3 urllib 爬蟲基本使用

urllib提供了一系列用于操作URL的功能。
urllib的request模塊可以非常方便地抓取URL內(nèi)容,也就是發(fā)送一個GET請求到指定的頁面,然后返回HTTP的響應(yīng)

01 簡單使用

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib.request

request_url = 'http://www.baidu.com'           # 需要請求的URL地址
response = urllib.request.urlopen(request_url) # 發(fā)起請求
print(response.read().decode('utf-8'))         # 打印響應(yīng)的文本,并進(jìn)行UTF-8解碼

  • read(), readline(), readlines(), fileno(), close():對HTTPResponse類型數(shù)據(jù)進(jìn)行操作
  • info():返回HTTPMessage對象,表示遠(yuǎn)程服務(wù)器返回的頭信息
  • getcode():返回Http狀態(tài)碼。如果是http請求,200請求成功完成、404網(wǎng)址未找到等等
  • geturl():返回請求的url

02 GET 方法

 #!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib.request
import urllib.parse

get_data = {'username': 'aaa', 'password': 'bbb'}          # 此處將GET的數(shù)據(jù)定義為一個字典
get_data_encode = urllib.parse.urlencode(get_data)         # 將GET的數(shù)據(jù)進(jìn)行編碼

request_url = 'http://www.baidu.com'              # 需要請求的URL地址
request_url += '?' + get_data_encode                       # 追加GET參數(shù)到URL后面

# https://www.zhihu.com/#signin?username=aaa&password=bbb
print(request_url)

# 發(fā)起請求
response = urllib.request.urlopen(request_url)
print(response.read().decode('utf-8'))         # 打印響應(yīng)的文本,并進(jìn)行UTF-8解碼

03 GET并獲取header信息

from urllib import request

with request.urlopen('http://www.baidu.com') as f:
    data = f.read()
    print('Status:', f.status, f.reason)
    for k, v in f.getheaders():
        print('%s: %s' % (k, v))
    print('Data:', data.decode('utf-8'))

04 POST 方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 

import urllib.request
import urllib.parse

post_data = {'first': 'true', 'pn': 1, 'kd': 'Python'}      # 此處將POST的數(shù)據(jù)定義為一個字典
post_data_encode = urllib.parse.urlencode(post_data)        # 將POST的數(shù)據(jù)進(jìn)行編碼

# UTF-8編碼
# 否則會報錯:POST data should be bytes or an iterable of bytes. It cannot be of type str.
post_data_encode = post_data_encode.encode(encoding='utf-8')
request_url = 'http://www.lagou.com/jobs/positionAjax.json?'               # 需要請求的URL地址

# 發(fā)起請求
# 此處增加了第二個參數(shù)為傳送的POST數(shù)據(jù)(默認(rèn)為None)
# 第三個參數(shù)為請求超時時間,默認(rèn)為socket._GLOBAL_DEFAULT_TIMEOUT
response = urllib.request.urlopen(request_url, post_data_encode, 3)
print(response.read().decode('utf-8'))         # 打印響應(yīng)的文本,并進(jìn)行UTF-8解碼

from urllib import request, parse

print('Login to weibo.cn...')
email = input('Email: ')
passwd = input('Password: ')
login_data = parse.urlencode([
    ('username', email),
    ('password', passwd),
    ('entry', 'mweibo'),
    ('client_id', ''),
    ('savestate', '1'),
    ('ec', ''),
    ('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
])

req = request.Request('https://passport.weibo.cn/sso/login')
req.add_header('Origin', 'https://passport.weibo.cn')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')

with request.urlopen(req, data=login_data.encode('utf-8')) as f:
    print('Status:', f.status, f.reason)
    for k, v in f.getheaders():
        print('%s: %s' % (k, v))
    print('Data:', f.read().decode('utf-8'))

04 使用Request 設(shè)置Headers屬性

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import urllib.request
import urllib.parse

user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87'
referer = 'http://www.lagou.com/jobs/positionAjax.json?'
post_data = {'first': 'true', 'pn': 1, 'kd': 'Python'}                              # 此處將POST的數(shù)據(jù)定義為一個字典
headers = {'User-Agent': user_agent, 'Referer': referer}                            # Headers屬性初始化
post_data_encode = urllib.parse.urlencode(post_data)                                # 將POST的數(shù)據(jù)進(jìn)行編碼

# UTF-8編碼
# 否則會報錯:POST data should be bytes or an iterable of bytes. It cannot be of type str.
post_data_encode = post_data_encode.encode(encoding='utf-8')
request_url = 'http://www.lagou.com/zhaopin/Python/?labelWords=label'               # 需要請求的URL地址

# 使用Request來設(shè)置Headers
request = urllib.request.Request(request_url, post_data_encode, headers)

response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))         # 打印響應(yīng)的文本,并進(jìn)行UTF-8解碼

06 Proxy(代理)的設(shè)置

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from urllib import request

request_url = 'http://www.xmgc360.com/project/test.php'
proxy = request.ProxyHandler({'http': '119.28.54.102:3389'})   # 設(shè)置代理服務(wù)器
opener = request.build_opener(proxy)                            # 掛載opener
request.install_opener(opener)                                  # 安裝opener
response = request.urlopen(request_url)
print(response.read().decode('utf-8'))         # 打印響應(yīng)的文本,并進(jìn)行UTF-8解碼

07 異常處理

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from urllib import request

request_url = 'http://www.lagou.com/jobs/positionAjax.json?'
proxy = request.ProxyHandler({'http': '127.0.0.1:8989'}) # 設(shè)置代理服務(wù)器
opener = request.build_opener(proxy)                         # 掛載opener
request.install_opener(opener)                               # 安裝opener
try:
    response = request.urlopen(request_url)
except Exception as e:
    print(e)                   # 打印錯誤碼

08 練習(xí)

http://image.baidu.com/channel/listjson?pn=1&rn=30&tag1=%E6%98%8E%E6%98%9F&tag2=%E5%85%A8%E9%83%A8&ie=utf8

抓取信息并保存到數(shù)據(jù)庫

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,649評論 19 139
  • 一、網(wǎng)絡(luò)爬蟲的定義 網(wǎng)絡(luò)爬蟲,即Web Spider,是一個很形象的名字。把互聯(lián)網(wǎng)比喻成一個蜘蛛網(wǎng),那么Spide...
    隨風(fēng)化作雨閱讀 1,285評論 0 0
  • urllib2是Python的一個獲取URLs的組件。他以urlopen函數(shù)的形式提供了一個非常簡單的接口,具有利...
    查無此人asdasd閱讀 1,626評論 0 3
  • 沐水自那次找小伍月之后,有很長一段時間陷入了沉思,也變得“沉默”起來。因?yàn)殂逅肭宄粋€問題,這個問題雖然并不...
    隱身小fox閱讀 345評論 0 0
  • 文/綠河 怕,就不走了嗎? 不是有句話叫做自己選的路跪著也要走完嗎? 很多事情無可奈何,卻想要奮不顧身的試一試。 ...
    綠河219閱讀 422評論 2 6

友情鏈接更多精彩內(nèi)容