首頁>技術>

介紹

介紹

福哥在使用urllib庫訪問API介面的時候,發現一個很奇葩的問題,就是網上滿天飛的教程裡都是教大家如何使用urllib的,但是透過pip安裝urllib提示找不到軟體包,這是怎麼回事?後來又看到有人使用urllib2來替換urllib庫,使用pip嘗試安裝一下,發現urllib2也是找不到軟體包,啊?怎麼辦?再後來又發現有人使用urllib3來替換urllib和urllib2,再使用pip嘗試安裝一下,發現urllib3還是找不到,崩潰了!最後看到有人說urllib和urllib2是自帶的,不需要安裝了,那麼是不是urllib3也不需要安裝呢?直接import一下,發現居然成功了!好吧,總算是用起來了~~

福哥對比urllib和urllib2以及最新版本的urllib3的語法,發現urllib3還是最完善的,怪不得要替換掉urllib和urllib2了,那麼童鞋們就跟福哥學習一下urllib3的使用方法吧。

GET

直接GET

直接請求一個網址的內容

import urllib3# argvurl = "http://www.baidu.com"# load url by gethttp = urllib3.PoolManager()res = http.request('GET',url)status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")print ("狀態碼:" + str(status))print ("原始碼:" + source)

帶引數GET

帶引數請求一個網址的內容,在百度裡搜尋“site:tongfu.net”

import urllib3import re# argvurl = "http://www.baidu.com/s"# load url by gethttp = urllib3.PoolManager()res = http.request('GET',url,fields={'wd':"site:tongfu.net"})status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")print ("狀態碼:" + str(status))regexp = re.compile("同福主頁 - 首頁 - 同福網 - TONGFU.net", re.M)mats = regexp.search(source)if mats:    print (mats.group())else:    print ("沒有找到")

可以看出我們透過傳遞wd引數,查詢到百度,得到了查詢結果

POST

POST普通資料

直接透過POST方式請求一個網址並提交一組資料

import urllib3import re# argvurl = "http://www.baidu.com/s"# load url by gethttp = urllib3.PoolManager()res = http.request('POST',url,fields={'wd':"site:tongfu.net"})status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")print ("狀態碼:" + str(status))regexp = re.compile("同福主頁 - 首頁 - 同福網 - TONGFU.net", re.M)mats = regexp.search(source)if mats:    print (mats.group())else:    print ("沒有找到")

可以看出使用POST方式傳遞wd引數,百度是不認的,查詢不到結果

POST Json資料

透過POST方式請求一個網址並以json格式提交一組資料

編碼JSON資料使用json.dumps,這個是有失敗情況的,所以需要在try下使用

import urllib3import json# argvurl = "http://www.baidu.com/s"# load url by gethttp = urllib3.PoolManager()try:    data = {'wd':"site:tongfu.net"}    json_data = json.dumps(data)except Exception as e:    doNothing = eres = http.request('POST',url,body=json_data,headers={'Content-Type':"application/json"})status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")print statusprint source
JSON

解析JSON資料

一般情況下,介面返回的資料都是json格式的,我們需要學習如何處理json資料

解析JSON資料使用json.loads,這個是有失敗情況的,所以需要在try下使用

import urllib3import json# argvurl = "https://tongfu.net/api/demo/user"# load url by gethttp = urllib3.PoolManager()res = http.request('POST',url)status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")try:    source_json = json.loads(source_uc)except Exception as e:    doNothing = eprint statusprint sourceprint source_json.get('errcode')print source_json.get('errmsg')print source_json.get('data').get('total')print source_json.get('data').get('datas')[0].get('title')
認證

http認證

使用urllib3實現http認證的方法

import urllib3import json# argvurl = "http://www.baidu.com/s"# load url by gethttp = urllib3.PoolManager()try:    data = {'wd':"site:tongfu.net"}    json_data = json.dumps(data)except Exception as e:    doNothing = eheaders_data = urllib3.util.make_headers(basic_auth="demo:123456")headers_data['Content-Type'] = "application/json"res = http.request('POST',url,body=json_data,headers=headers_data)status = res.statussource_uc = res.data.decode("utf-8")source = source_uc.encode("gbk","ignore")print statusprint source
總結

可以看到urllib3是個非常強大的軟體包,基本可以完成模擬絕大部分網路操作的需要。童鞋們需要掌握這一技術,因為沒有網路的軟體的功能是有限的,沒有網路的遊戲是沒有靈魂的,無論我們編寫什麼樣的程式,脫離網路基本都是小玩鬧了。

https://m.tongfu.net/home/35/blog/512785.html

17
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 知識圖譜上的機器學習@ NeurIPS 2020