Python 網路請求模塊 urllib 、requests
Python 給人的印象是抓取網頁非常方便,提供這種生產力的,主要依靠的就是 urllib、requests這兩個模塊,
urlib 介紹
- urllib.request 提供了一個 urlopen 函式,來實作獲取頁面,支持不同的協議、基本驗證、cookie、代理等特性,
- urllib 有兩個版本 urllib 以及 urllib2,
- urllib2 能夠接受 Request 物件,urllib 則只能接受 url,
- urllib 提供了 urlencode 函式來對GET請求的引數進行轉碼,urllib2 沒有對應函式,
- urllib 拋出了 一個 URLError 和一個 HTTPError 來處理客戶端和服務端的例外情況,
Requests 介紹
Requests 是一個簡單易用的,用Python撰寫的HTTP庫,這個庫讓我們能夠用簡單的引數就完成HTTP請求,而不必像 urllib 一樣自己指定引數,同時能夠自動將回應轉碼為Unicode,而且具有豐富的錯誤處理功能,
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with Cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value Cookies
- Automatic Decompression
- Unicode Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.6—3.4
- Thread-safe
以下為一些示例代碼,本文環境為 Python 3.6
無需引數直接請求單個頁面
import urllib
from urllib.request import request
from urllib.urlopen import urlopen
# import urllib2
import requests
# 使用 urllib 方式獲取
response = urllib.request.urlopen('http://www.baidu.com')
# read() 讀取的是服務器的原始回傳資料 decode() 后會進行轉碼
print(response.read().decode())
# 使用 requests 方式獲取
# request 模塊相比
resp = requests.get('http://www.baidu.com')
print(resp)
print(resp.text)
HTTP 是基于請求和回應的作業模式,urllib.request 提供了一個 Request 物件來代表請求,因此上面的代碼也可以這么寫
req = urllib.request.Request('http://www.baidu.com')
with urllib.request.urlopen(req) as response:
print(response.read())
Request物件可以增加header資訊
req = urllib.request.Request('http://www.baidu.com')
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')
with urllib.request.urlopen(req) as response:
print(response.read())
或者直接將 header 傳入 Request 構建函式,
帶引數的 GET 請求
帶有引數的請求和上面的例子本質一樣,可以事先拼出URL請求字串,然后再進行請求,
本例使用了 騰訊 的股票API,可以傳入不同的股票代碼以及日期,查詢對應股票在對應時間的價格、交易資訊,
# 使用帶引數的介面訪問
tencent_api = "http://qt.gtimg.cn/q=sh601939"
response = urllib.request.urlopen(tencent_api)
# read() 讀取的是服務器的原始回傳資料 decode() 后會進行轉碼
print(response.read())
resp = requests.get(tencent_api)
print(resp)
print(resp.text)
發送 POST 請求
urllib 沒有單獨區分 GET 和 POST 請求的函式,只是通過 Request 物件是否有 data 引數傳入來判斷,
import urllib.parse
import urllib.request
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = https://www.cnblogs.com/xxpythonxx/archive/2022/10/07/urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510985.html
標籤:其他
下一篇:ARC148游記
