Python使用內置urllib模塊或第三方庫requests訪問網絡資源

前言

更多內容,請訪問個人 我的博客html


Python 訪問網絡資源有不少方法,urllib, urllib2, urllib3, httplib, httplib2, requests ,現介紹以下兩種方法:python

  • 內置的 urllib 模塊
    • 優勢:自帶模塊,無需額外下載第三方庫
    • 缺點:操做繁瑣,缺乏高級功能
  • 第三方庫 requests
    • 優勢:處理URL資源特別方便
    • 缺點:須要下載安裝第三方庫

內置的 urllib 模塊

發起GET請求

主要使用urlopen()方法來發起請求,以下:編程

from urllib import request

resp = request.urlopen('http://www.baidu.com')
print(resp.read().decode())
複製代碼

訪問的結果會是一 個http.client.HTTPResponse 對象,使用此對象的 read() 方法,則能夠獲取訪問網頁得到的數據。可是要注意的是,得到的數據會是 bytes 的二進制格式,因此須要 decode() 一下,轉換成字符串格式。json

發起POST請求

urlopen() 默認的訪問方式是GET,當在 urlopen() 方法中傳入data參數時,則會發起POST請求。注意:傳遞的data數據須要爲bytes格式。api

設置timeout參數還能夠設置超時時間,若是請求時間超出,那麼就會拋出異常。以下:bash

from urllib import request

resp = request.urlopen('http://www.baidu.com', data=b'word=hello', timeout=10)
print(resp.read().decode())
複製代碼

添加Headers

經過 urllib 發起的請求會有默認的一個Headers:"User-Agent":"Python-urllib/3.6",指明請求是由 urllib 發送的。 因此遇到一些驗證User-Agent的網站時,咱們須要自定義Headers,而這須要藉助於urllib.request中的 Request 對象。cookie

from urllib import request

url = 'http://httpbin.org/get'
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}

# 須要使用url和headers生成一個Request對象,而後將其傳入urlopen方法中
req = request.Request(url, headers=headers)
resp = request.urlopen(req)
print(resp.read().decode())
複製代碼

Request對象

如上所示, urlopen() 方法中不止能夠傳入字符串格式的url,也能夠傳入一個 Request 對象來擴展功能,Request 對象以下:網絡

class urllib.request.Request(url, data=None, headers={},
                                origin_req_host=None,
                                unverifiable=False, 
                                method=None)
複製代碼

構造 Request 對象必須傳入url參數,data數據和headers都是可選的。app

最後, Request 方法可使用method參數來自由選擇請求的方法,如PUT,DELETE等等,默認爲GET。post

添加Cookie

爲了在請求時能帶上Cookie信息,咱們須要從新構造一個opener。

使用request.build_opener方法來進行構造opener,將咱們想要傳遞的cookie配置到opener中,而後使用這個opener的open方法來發起請求。以下:

from http import cookiejar
from urllib import request

url = 'https://www.baidu.com'
# 建立一個cookiejar對象
cookie = cookiejar.CookieJar()
# 使用HTTPCookieProcessor建立cookie處理器
cookies = request.HTTPCookieProcessor(cookie)
# 並以它爲參數建立Opener對象
opener = request.build_opener(cookies)
# 使用這個opener來發起請求
resp = opener.open(url)

# 查看以前的cookie對象,則能夠看到訪問百度得到的cookie
for i in cookie:
    print(i)
複製代碼

或者也能夠把這個生成的opener使用install_opener方法來設置爲全局的。

則以後使用urlopen方法發起請求時,都會帶上這個cookie。

# 將這個opener設置爲全局的opener
request.install_opener(opener)
resp = request.urlopen(url)
複製代碼

設置Proxy代理

使用爬蟲來爬取數據的時候,經常須要使用代理來隱藏咱們的真實IP。以下:

from urllib import request

url = 'http://www.baidu.com'
proxy = {'http':'222.222.222.222:80','https':'222.222.222.222:80'}
# 建立代理處理器
proxies = request.ProxyHandler(proxy)
# 建立opener對象
opener = request.build_opener(proxies)

resp = opener.open(url)
print(resp.read().decode())
複製代碼

下載數據到本地

在咱們進行網絡請求時經常須要保存圖片或音頻等數據到本地,一種方法是使用python的文件操做,將read()獲取的數據保存到文件中。

而urllib提供了一個urlretrieve()方法,能夠簡單的直接將請求獲取的數據保存成文件。以下:

from urllib import request

url = 'http://python.org/'
request.urlretrieve(url, 'python.html')
複製代碼

urlretrieve() 方法傳入的第二個參數爲文件保存的位置,以及文件名。

注意:urlretrieve() 方法是python2直接移植過來的方法,之後有可能在某個版本中棄用。

第三方庫 requests

安裝

因爲 requests是第三方庫,因此要先安裝,以下:

pip install requests
複製代碼

發起GET請求

直接用 get 方法,以下:

import requests

r = requests.get('http://www.baidu.com/')
print(r.status_code)    #狀態
print(r.text)   #內容
複製代碼

對於帶參數的URL,傳入一個dict做爲params參數,以下:

import requests

r = requests.get('http://www.baidu.com/', params={'q': 'python', 'cat': '1001'})
print(r.url)    #實際請求的URL
print(r.text)
複製代碼

requests的方便之處還在於,對於特定類型的響應,例如JSON,能夠直接獲取,以下:

r = requests.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%202151330&format=json')
r.json()

# {'query': {'count': 1, 'created': '2017-11-17T07:14:12Z', ...
複製代碼

添加Headers

須要傳入HTTP Header時,咱們傳入一個dict做爲headers參數,以下:

r = requests.get('https://www.baidu.com/', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})
複製代碼

獲取響應頭,以下:

r.headers
# {Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', ...}

r.headers['Content-Type']
# 'text/html; charset=utf-8'
複製代碼

發起POST請求

要發送POST請求,只須要把get()方法變成post(),而後傳入data參數做爲POST請求的數據,以下:

r = requests.post('https://accounts.baidu.com/login', data={'form_email': 'abc@example.com', 'form_password': '123456'})
複製代碼

requests默認使用application/x-www-form-urlencoded對POST數據編碼。若是要傳遞JSON數據,能夠直接傳入json參數,以下:

params = {'key': 'value'}
r = requests.post(url, json=params) #內部自動序列化爲JSON
複製代碼

上傳文件

上傳文件須要更復雜的編碼格式,可是requests把它簡化成files參數,以下:

upload_files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=upload_files)
複製代碼

在讀取文件時,注意務必使用 'rb' 即二進制模式讀取,這樣獲取的 bytes 長度纔是文件的長度。

post() 方法替換爲 put()delete() 等,就能夠以PUT或DELETE方式請求資源。

添加Cookie

在請求中傳入Cookie,只需準備一個dict傳入cookies參數,以下:

cs = {'token': '12345', 'status': 'working'}
r = requests.get(url, cookies=cs)
複製代碼

requests對Cookie作了特殊處理,使得咱們沒必要解析Cookie就能夠輕鬆獲取指定的Cookie,以下:

r.cookies['token']
# 12345
複製代碼

指定超時

要指定超時,傳入以秒爲單位的timeout參數。超時分爲鏈接超時和讀取超時,以下:

try:
    # 3.1秒後鏈接超時,27秒後讀取超時
    r = requests.get(url, timeout=(3.1, 27))
except requests.exceptions.RequestException as e:
    print(e)
複製代碼

超時重連

def gethtml(url):
    i = 0
    while i < 3:
        try:
            html = requests.get(url, timeout=5).text
            return html
        except requests.exceptions.RequestException:
            i += 1
複製代碼

添加代理

同添加headers方法,代理參數也要是一個dict,以下:

heads = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'
}
proxy = {
    'http': 'http://120.25.253.234:812',
    'https' 'https://163.125.222.244:8123'
}
r = requests.get('https://www.baidu.com/', headers=heads, proxies=proxy)
複製代碼

更多編程教學請關注公衆號:潘高陪你學編程

image
相關文章
相關標籤/搜索