基礎(chǔ)篇-Python的urllib庫(kù)

urllib是Python自帶的標(biāo)準(zhǔn)庫(kù),無需安裝,直接可以用。
提供了如下功能:

  • 網(wǎng)頁(yè)請(qǐng)求
  • 響應(yīng)獲取
  • 代理和cookie設(shè)置
  • 異常處理
  • URL解析

爬蟲所需要的功能,基本上在urllib中都能找到,學(xué)習(xí)這個(gè)標(biāo)準(zhǔn)庫(kù),可以更加深入的理解后面更加便利的requests庫(kù)。

urllib庫(kù)

urlopen 語(yǔ)法

urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None)
#url:訪問的網(wǎng)址
#data:額外的數(shù)據(jù),如header,form data

用法

# request:GET
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))

# request: POST
# http測(cè)試:http://httpbin.org/
import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read())

# 超時(shí)設(shè)置
import urllib.request
response = urllib.request.urlopen('http://httpbin.org/get',timeout=1)
print(response.read())

import socket
import urllib.request
import urllib.error

try:
    response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:
    if isinstance(e.reason,socket.timeout):
        print('TIME OUT')

響應(yīng)

# 響應(yīng)類型
import urllib.open
response = urllib.request.urlopen('https:///www.python.org')
print(type(response))
# 狀態(tài)碼, 響應(yīng)頭
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(response.status)
print(response.getheaders())
print(response.getheader('Server'))

Request

聲明一個(gè)request對(duì)象,該對(duì)象可以包括header等信息,然后用urlopen打開。

# 簡(jiǎn)單例子
import urllib.request
request = urllib.request.Requests('https://python.org')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

# 增加header
from urllib import request, parse
url = 'http://httpbin.org/post'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
    'Host':'httpbin.org'
}
# 構(gòu)造POST表格
dict = {
    'name':'Germey'
}
data = bytes(parse.urlencode(dict),encoding='utf8')
req = request.Request(url=url,data=data,headers=headers,method='POST')
response = request.urlopen(req)
print(response.read()).decode('utf-8')
# 或者隨后增加header
from urllib import request, parse
url = 'http://httpbin.org/post'
dict = {
    'name':'Germey'
}
req = request.Request(url=url,data=data,method='POST')
req.add_hader('User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
response = request.urlopen(req)
print(response.read().decode('utf-8'))

Handler:處理更加復(fù)雜的頁(yè)面

官方說明
代理

import urllib.request
proxy_handler = urllib.request.ProxyHandler({
    'http':'http://127.0.0.1:9743'
    'https':'https://127.0.0.1.9743'
})
opener = urllib.request.build_openner(proxy_handler)
response = opener.open('http://www.baidu.com')
print(response.read())

Cookie:客戶端用于記錄用戶身份,維持登錄信息

import http.cookiejar, urllib.request

cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.baidu.com")
for item in cookie:
    print(item.name+"="+item.value)

# 保存cooki為文本
import http.cookiejar, urllib.request
filename = "cookie.txt"
# 保存類型有很多種
## 類型1
cookie = http.cookiejar.MozillaCookieJar(filename)
## 類型2
cookie = http.cookiejar.LWPCookieJar(filename)

handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.baidu.com")

# 使用相應(yīng)的方法讀取
import http.cookiejar, urllib.request
cookie = http.cookiejar.LWPCookieJar()
cookie.load('cookie.txt',ignore_discard=True,ignore_expires=True)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.baidu.com")

異常處理

捕獲異常,保證程序穩(wěn)定運(yùn)行

# 訪問不存在的頁(yè)面
from urllib import request, error
try:
    response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.URLError as e:
    print(e.reason)

# 先捕獲子類錯(cuò)誤
from urllib imort request, error
try:
    response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.HTTPError as e:
    print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e:
    print(e.reason)
else:
    print("Request Successfully')
# 判斷原因
import socket
import urllib.request
import urllib.error

try:
    response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:
    if isinstance(e.reason,socket.timeout):
        print('TIME OUT')

URL解析

主要是一個(gè)工具模塊,可用于為爬蟲提供URL。

urlparse:拆分URL

urlib.parse.urlparse(urlstring,scheme='', allow_fragments=True)
# scheme: 協(xié)議類型
# 是否忽略’#‘部分

舉個(gè)例子

from urllib import urlparse
result = urlparse("https://edu.hellobi.com/course/157/play/lesson/2580")
result
##ParseResult(scheme='https', netloc='edu.hellobi.com', path='/course/157/play/lesson/2580', params='', query='', fragment='')

urlunparse:拼接URL,為urlparse的反向操作

from urllib.parse import urlunparse
data = ['http','www.baidu.com','index.html','user','a=6','comment']
print(urlunparse(data))

urljoin:拼接兩個(gè)URL

urljoin

urlencode:字典對(duì)象轉(zhuǎn)換成GET請(qǐng)求對(duì)象

from urllib.parse import urlencode
params = {
    'name':'germey',
    'age': 22
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params)
print(url)

最后還有一個(gè)robotparse,解析網(wǎng)站允許爬取的部分。

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

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

  • 1 前言 作為一名合格的數(shù)據(jù)分析師,其完整的技術(shù)知識(shí)體系必須貫穿數(shù)據(jù)獲取、數(shù)據(jù)存儲(chǔ)、數(shù)據(jù)提取、數(shù)據(jù)分析、數(shù)據(jù)挖掘、...
    whenif閱讀 18,313評(píng)論 45 523
  • Python爬蟲入門(urllib+Beautifulsoup) 本文包括:1、爬蟲簡(jiǎn)單介紹2、爬蟲架構(gòu)三大模塊3...
    廖少少閱讀 10,083評(píng)論 0 6
  • 一、概述 urllib2是Python的一個(gè)針對(duì)URLs的庫(kù)。他以u(píng)rlopen函數(shù)的形式提供了一個(gè)非常簡(jiǎn)單的...
    MiracleJQ閱讀 1,558評(píng)論 0 5
  • 人,習(xí)慣孤獨(dú),至少我是這樣的。像大多數(shù)人善良而脆弱的活著,因?yàn)樯鲁承蚜俗约航ㄔ斓睦硐胧澜纾嗀露享车淖呦蛭粗?..
    Lingbabysaid閱讀 312評(píng)論 0 0
  • 遞歸的特點(diǎn): 自己調(diào)用自己 設(shè)定終止條件 優(yōu)點(diǎn):算法簡(jiǎn)單缺點(diǎn):效率低下 用遞歸實(shí)現(xiàn)階乘 n! 用 for 循環(huán)實(shí)現(xiàn)...
    輝夜乀閱讀 310評(píng)論 0 0

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