一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python爬蟲之urllib3的使用示例

python爬蟲之urllib3的使用示例

2021-03-15 00:06瀟瀟漓燃 Python

這篇文章主要介紹了 python爬蟲之urllib3的使用示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Urllib3是一個功能強大,條理清晰,用于HTTP客戶端的Python庫。許多Python的原生系統已經開始使用urllib3。Urllib3提供了很多python標準庫urllib里所沒有的重要特性:

  1. 線程安全
  2. 連接池
  3. 客戶端SSL/TLS驗證
  4. 文件分部編碼上傳
  5. 協助處理重復請求和HTTP重定位
  6. 支持壓縮編碼
  7. 支持HTTP和SOCKS代理

一、get請求

urllib3主要使用連接池進行網絡請求的訪問,所以訪問之前我們需要創建一個連接池對象,如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
import urllib3
 
url = "http://httpbin.org"
http = urllib3.PoolManager();
r = http.request('GET',url+"/get")
print(r.data.decode())
print(r.status)
 
帶參數的get
r = http.request('get','http://www.baidu.com/s',fields={'wd':'周杰倫'})
print(r.data.decode())

經查看源碼:

?
1
def request(self, method, url, fields=None, headers=None, **urlopen_kw):
  • 第一個參數method 必選,指定是什么請求,'get'、'GET'、'POST'、'post'、'PUT'、'DELETE'等,不區分大小寫。
  • 第二個參數url,必選
  • 第三個參數fields,請求的參數,可選
  • 第四個參數headers 可選

request請求的返回值是<urllib3.response.HTTPResponse object at 0x000001B3879440B8>

我們可以通過dir()查看其所有的屬性和方法。

dir(r)

直截取了一部分

?
1
2
3
4
5
6
#'data', 'decode_content', 'enforce_content_length', 'fileno', 'flush', 'from_httplib',
# 'get_redirect_location', 'getheader', 'getheaders', 'headers', 'info', 'isatty',
# 'length_remaining', 'read', 'read_chunked', 'readable', 'readinto', 'readline',
# 'readlines', 'reason', 'release_conn', 'retries', 'seek', 'seekable', 'status',
# 'stream', 'strict', 'supports_chunked_reads', 'tell', 'truncate', 'version', 'writable',
# 'writelines']

二、post請求

?
1
2
3
4
5
6
7
8
import urllib3
url = "http://httpbin.org"
fields = {
  'name':'xfy'
}
http = urllib3.PoolManager()
r = http.request('post',url+"/post",fields=fields)
print(r.data.decode())

可以看到很簡單,只是第一個參數get換成了post。

并且參數不需要再像urllib一樣轉換成byte型了。

三、設置headers

?
1
2
3
4
5
6
7
import urllib3
headers = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
}
http = urllib3.PoolManager();
r = http.request('get',url+"/get",headers = headers)
print(r.data.decode())

四、設置代理

?
1
2
3
4
5
6
7
8
import urllib3
url = "http://httpbin.org"
headers = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
}
proxy = urllib3.ProxyManager('http://101.236.19.165:8866',headers = headers)
r = proxy.request('get',url+"/ip")
print(r.data.decode())

五、當請求的參數為json

在發起請求時,可以通過定義body 參數并定義headers的Content-Type參數來發送一個已經過編譯的JSON數據

?
1
2
3
4
5
6
7
8
9
10
import urllib3
url = "http://httpbin.org"
import json
data = {'name':'徐繁韻'}
 
json_data = json.dumps(data)
 
http = urllib3.PoolManager()
r = http.request('post',url+"/post",body = json_data,headers = {'Content-Type':'application/json'})
print(r.data.decode('unicode_escape'))

六、上傳文件

?
1
2
3
4
5
6
7
8
9
10
11
#元組形式
with open('a.html','rb') as f:
  data = f.read()
http = urllib3.PoolManager()
r = http.request('post','http://httpbin.org/post',fields = {'filefield':('a.html',data,'text/plain')})
print(r.data.decode())
 
#二進制形式
 
r = http.request('post','http://httpbin.org/post',body = data,headers={'Content-Type':'image/jpeg'})
print(r.data.decode())

七、超時設置

?
1
2
3
4
# 1全局設置超時
# http = urllib3.PoolManager(timeout = 3)
# 2在request里設置
# http.request('post','http://httpbin.org/post',timeout = 3)

八、重試和重定向

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import urllib3
http = urllib3.PoolManager()
#重試
r = http.request('post','http://httpbin.org/post',retries = 5) #請求重試測次數為5次 ,默認為3ci
print(r.retries) #Retry(total=5, connect=None, read=None, redirect=0, status=None)
#關閉重試
http.request('post','http://httpbin.org/post',retries = False) #請求重試測次數為5次 ,默認為3ci
 
r = http.request('get','http://httpbin.org/redirect/1',redirect = False)
print(r.retries)# Retry(total=3, connect=None, read=None, redirect=None, status=None)
print(r.status)
print(r.data.decode())
print("--------------------")
print(r.get_redirect_location())
#302不是異常

九、urllib3 本身設置了https的處理,但是有警告

雖然可以請求,但是報如下警告:

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)

禁用警告:

?
1
2
3
4
5
6
import urllib3
urllib3.disable_warnings() #禁用各種警告
url = "https://www.12306.cn/mormhweb/"
http = urllib3.PoolManager()
r = http.request('get',url)
print(r.data.decode())

urllib3很強大,但是并沒有requests好用。了解為主。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/cc2b9e9ef28a

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国语视频高清在线观看 | 四虎成人4hutv影院 | 男公厕里同性做爰 | 亚洲七七久久综合桃花 | 99r在线播放| 欧美人与禽交片在线播放 | 爱情岛论坛亚洲永久入口口 | 国产精品视频免费视频 | 含羞草国产亚洲精品岁国产精品 | 午夜精品久久久内射近拍高清 | 欧美伦乱| 关晓彤被草 | 日本视频在线播放 | 亚洲国产精品久久精品成人网站 | 精品综合久久久久久8888 | 美女污视频在线观看 | 精品亚洲麻豆1区2区3区 | 1717she精品视频在线观看 | 99re热这里只有精品 | 青青国产成人久久激情91麻豆 | 亚洲精品福利你懂 | 欧美性黑人巨大gaysex | 四虎影院网址大全 | 欧美日韩在线观看区一二 | 天天色资料 | 免费在线观看网址入口 | 精品欧美小视频在线观看 | 男人免费视频 | 日本高清va不卡视频在线观看 | 欧美成人一区二区三区 | 国产精品美女久久久久 | 九九精品99久久久香蕉 | 色播影音先锋 | 亚洲AV久久无码精品九号 | 9966久久精品免费看国产 | 精品久久亚洲 | 国色天香论坛社区在线视频 | 日韩精选在线 | a毛片免费全部在线播放毛 a级在线看 | 男女交性特一级 | 欧美日韩亚洲高清不卡一区二区三区 |