1. 程式人生 > 程式設計 >Python urllib request模組傳送請求實現過程解析

Python urllib request模組傳送請求實現過程解析

1.Request()的引數

import urllib.request

request=urllib.request.Request('https://python.org')
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

通過構造這個資料結構,一方面可以我們可以將請求獨立成一個物件,另一方面可以更加豐富和靈活地配置引數。

它的構造方法如下:

class.urllib.request.Request(url,data=None,headers={},origin_rep_host=None,unverifiable=False,method=None)


引數:

1.url必傳引數

2.data,必須傳bytes型別。如果是字典,先使用urllib.parse裡的urlencode()

3.headers,是一個字典,請求頭,直接構造或者用add_header()方法新增

4.origin_rep_host,請求方的名稱或者ip地址

5.unverifiable,預設為false,表示這個請求是否無法驗證。如果沒有抓取的許可權,此時值就是true。

6.method,用來指示請求使用的方法。

嘗試傳入多個引數構建請求:

from urllib import request,parse

url='http://httpbin.org/post'
headers={
  'Url-Agent':'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)','Host':'httpbin.org'
}#也可以使用add_header()方法新增headers:#req=request.Request(url=url,data=data,method='POST')#req.add_header('User-Agent','Mozilla/4.0(compatible;MSIE 5.5;Windows NT)')
dict={
  'name':'Germey'
}
data=bytes(parse.urlencode(dict),encoding='utf-8')#用urlencode()將dict轉換成bytes型別,傳遞給data
req=request.Request(url=url,headers=headers,method='POST')
response=request.urlopen(req)
print(response.read().decode('utf-8'))

執行結果:

Python urllib request模組傳送請求實現過程解析

2.Handler與Opener

Handler:

它是各種處理器,幾乎可以做到HTTP請求中的所有事情。

urllib.request模組裡的BaseHandler類,它是所有其他Headler的父類,它提供了最基本的方法。

Opener:

例如urlopen()就是一個Opener,它是urllib為我們提供的。

它們的關係是:使用Handler來構建Opener。

3.用法

驗證:

建立一個需要驗證的網站,我這裡使用的是IIS

Python urllib request模組傳送請求實現過程解析

遇到的問題:

IIS怎樣安裝與配置-百度經驗 (baidu.com)

IIS網站如何設定基本身份驗證-百度經驗 (baidu.com)

window10家庭版解決IIS中全球資訊網服務的安全性中無Windows身份驗證 - enjoryWeb - 部落格園 (cnblogs.com)

程式碼:

from urllib.request import HTTPPasswordMgrWithDefaultRealm,HTTPBasicAuthHandler,build_opener
from urllib.error import URLError

username='username'#填上自己的使用者名稱和密碼
password='password'
url='http://localhost:5000/'

p=HTTPPasswordMgrWithDefaultRealm()
p.add_password(None,url,username,password)#新增使用者名稱和密碼,建立了一個處理驗證的Handler
auth_handler=HTTPBasicAuthHandler(p)#基本認證
opener=build_opener(auth_handler)#利用Handler構建一個Opener

try:
  result=opener.open(url)#開啟連結
  html=result.read().decode('utf-8')
  print(html)#結果列印html原始碼內容
except URLError as e:
  print(e.reason)

代理:

新增代理,在本地搭建一個代理,執行在9743埠上。

程式碼:

from urllib.request import ProxyHandler,build_opener
from urllib.error import URLError

proxy_handler=ProxyHandler({
  'http':'http://127.0.0.1:9743','https':'https://127.0.0.1:9743'
})#構建一個Handler
opener=build_opener(proxy_handler)#構建一個Opener
try:
  response=opener.open('https://www.baidu.com')
  print(response.read().decode('utf-8'))
except URLError as e:
  print(e.reason)

Cookies:

將網站的Cookies獲取下來:

程式碼:

import http.cookiejar,urllib.request

cookie=http.cookiejar.CookieJar()#宣告一個CookieJar物件
handler=urllib.request.HTTPCookieProcessor(cookie)#構建一個Handler
opener=urllib.request.build_opener(handler)#構建一個Opener
response=opener.open('http://www.baidu.com')
for item in cookie:
  print(item.name+"="+item.value)

執行結果:

Python urllib request模組傳送請求實現過程解析

將Cookie輸出成檔案格式:

程式碼:

import http.cookiejar,urllib.request

filename='cookies.txt'

cookie=http.cookiejar.MozillaCookieJar(filename)
#MozillaCookieJar()生成檔案時用到,用來處理Cookie和檔案相關的事件
#如果要儲存LWP格式的Cookies檔案,可以改為:
#cookie=http.cookiejar.LWPCookieJar(filename)

handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
response=opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True,ignore_expires=True)

執行結果:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.

.baidu.com  TRUE  /  FALSE  1638359640  BAIDUID  9BB1BA4FDD840EBD956A3D2EFB6BF883:FG=1
.baidu.com  TRUE  /  FALSE  3754307287  BIDUPSID  9BB1BA4FDD840EBD25D00EE8183D1125
.baidu.com  TRUE  /  FALSE    H_PS_PSSID  1445_33119_33059_31660_33099_33101_26350_33199
.baidu.com  TRUE  /  FALSE  3754307287  PSTM  1606823639
www.baidu.com  FALSE  /  FALSE    BDSVRTM  7
www.baidu.com  FALSE  /  FALSE    BD_HOME  1

LWP格式:

#LWP-Cookies-2.0
Set-Cookie3: BAIDUID="DDF5CB401A1543ED614CE42962D48099:FG=1"; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2021-12-01 12:04:18Z"; comment=bd; version=0
Set-Cookie3: BIDUPSID=DDF5CB401A1543ED00860C3997C3282C; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0
Set-Cookie3: H_PS_PSSID=1430_33058_31254_33098_33101_33199; path="/"; domain=".baidu.com"; path_spec; domain_dot; discard; version=0
Set-Cookie3: PSTM=1606824257; path="/"; domain=".baidu.com"; path_spec; domain_dot; expires="2088-12-19 15:18:25Z"; version=0
Set-Cookie3: BDSVRTM=0; path="/"; domain="www.baidu.com"; path_spec; discard; version=0
Set-Cookie3: BD_HOME=1; path="/"; domain="www.baidu.com"; path_spec; discard; version=0

以LWP格式的檔案為示例,展示讀取和利用的方法:

程式碼:

import http.cookiejar,urllib.request

cookie=http.cookiejar.LWPCookieJar()
#如果檔案儲存為Mozilla型瀏覽器格式,可以改為:
#cookie=http.cookiejar.MozillaCookieJar()

cookie.load('cookies.txt',ignore_discard=True,ignore_expires=True)
#呼叫load()方法來讀取本地的Cookies檔案,獲取Cookies的內容

handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
response=opener.open('http://www.baidu.com')
print(response.read().decode('utf-8'))

執行結果:輸出網頁原始碼。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。