python中urllib.request和requests的使用及區(qū)別詳解
urllib.request
我們都知道,urlopen()方法能發(fā)起最基本對的請求發(fā)起,但僅僅這些在我們的實際應用中一般都是不夠的,可能我們需要加入headers之類的參數(shù),那需要用功能更為強大的Request類來構建了
在不需要任何其他參數(shù)配置的時候,可直接通過urlopen()方法來發(fā)起一個簡單的web請求
發(fā)起一個簡單的請求
import urllib.request url='https://www.douban.com' webPage=urllib.request.urlopen(url) print(webPage) data=webPage.read() print(data) print(data.decode('utf-8'))
urlopen()方法返回的是一個http.client.HTTPResponse對象,需要通過read()方法做進一步的處理。一般使用read()后,我們需要用decode()進行解碼,通常為utf-8,經(jīng)過這些步驟后,最終才獲取到我們想要的網(wǎng)頁。
添加Headers信息
import urllib.request url='https://www.douban.com' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36', } response=urllib.request.Request(url=url,headers=headers) webPage=urllib.request.urlopen(response) print(webPage.read().decode('utf-8'))
使用Request類返回的又是一個urllib.request.Request對象了。
通常我們爬取網(wǎng)頁,在構造http請求的時候,都需要加上一些額外信息,什么Useragent,cookie等之類的信息,或者添加代理服務器。往往這些都是一些必要的反爬機制
requests
通常而言,在我們使用python爬蟲時,更建議用requests庫,因為requests比urllib更為便捷,requests可以直接構造get,post請求并發(fā)起,而urllib.request只能先構造get,post請求,再發(fā)起。
import requests url='https://www.douban.com' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36', } get_response = requests.get(url,headers=headers,params=None) post_response=requests.post(url,headers=headers,data=None,json=None) print(post_response) print(get_response.text) print(get_response.content) print(get_response.json)
get_response.text得到的是str數(shù)據(jù)類型。
get_response.content得到的是Bytes類型,需要進行解碼。作用和get_response.text類似。
get_response.json得到的是json數(shù)據(jù)。
總而言之,requests是對urllib的進一步封裝,因此在使用上顯得更加的便捷,建議小伙伴們在實際應用當中盡量使用requests。
補充知識:python中urllib.request.Request()與urllib.request.urlopen()區(qū)別
蟒蛇中urllib.request.Request()與urllib.request.urlopen()的區(qū)別:
相對于urllib.request.urlopen()來說urllib.request.Request是進一步的包裝請求,下面是請求類的源碼示例:
class Request: # 主要看這塊,構造函數(shù)中指明了Request進一步包裝請求中可以傳遞的參數(shù)有(url,data,headers, # origin_req_host,unverifiable,method) def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None): self.full_url = url self.headers = {} self.unredirected_hdrs = {} self._data = None self.data = data self._tunnel_host = None for key, value in headers.items(): self.add_header(key, value) if origin_req_host is None: origin_req_host = request_host(self) self.origin_req_host = origin_req_host self.unverifiable = unverifiable if method: self.method = method pass
我們可以這樣使用(以下是模擬有道字典翻譯發(fā)送的請求):
# 請求地址url url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" # 請求頭 request_headers = { 'Host':'fanyi.youdao.com', "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36", } # 發(fā)送給服務器的表單 form_data = { "i": word, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_REALTIME", "typoResult": "false" } # POST發(fā)送的data必須為bytes或bytes類型的可迭代對象,不能是字符串 form_data = urllib.parse.urlencode(form_data).encode() # 構造請求對象Request req = urllib.request.Request(url, data=form_data, headers=request_headers) # 發(fā)起請求 response = urllib.request.urlopen(req) data = response.read().decode() print(data)
所以,總的來說,如果我們在獲取請求對象時,不需要過多的參數(shù)傳遞,我么可以直接選擇urllib.request.urlopen();如果需要進一步的包裝請求,則需要用urllib.request里。的urlopen()進行包裝處理。
以上這篇python中urllib.request和requests的使用及區(qū)別詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用with torch.no_grad():顯著減少測試時顯存占用
這篇文章主要介紹了使用with torch.no_grad():顯著減少測試時顯存占用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Python tabulate結合loguru打印出美觀方便的日志記錄
在開發(fā)過程中經(jīng)常碰到在本地環(huán)境無法完成聯(lián)調測試的情況,必須到統(tǒng)一的聯(lián)機環(huán)境對接其他系統(tǒng)測試。往往是出現(xiàn)了BUG難以查找數(shù)據(jù)記錄及時定位到錯誤出現(xiàn)的位置。本文將利用tabulate結合loguru實現(xiàn)打印出美觀方便的日志記錄,需要的可以參考一下2022-10-10Python實現(xiàn)將MongoDB中的數(shù)據(jù)導入到MySQL
這篇文章主要為大家詳細介紹了如何通過Python封裝一個將?MongoDB?中的數(shù)據(jù)導入到?MySQL?中的?Python?工具類?MongoToMysql,感興趣的可以了解一下2023-05-05