詳解Python requests模塊
前言
雖然Python的標(biāo)準(zhǔn)庫中 urllib2 模塊已經(jīng)包含了平常我們使用的大多數(shù)功能,但是它的 API 使用起來讓人感覺不太好,而 Requests 自稱 “HTTP for Humans”,說明使用更簡潔方便。
Requests 繼承了urllib2的所有特性。Requests支持HTTP連接保持和連接池,支持使用cookie保持會(huì)話,支持文件上傳,支持自動(dòng)確定響應(yīng)內(nèi)容的編碼,支持國際化的 URL 和 POST 數(shù)據(jù)自動(dòng)編碼。
開源地址:https://github.com/kennethreitz/requests
中文文檔 API: http://docs.python-requests.org/zh_CN/latest/index.html
一、GET請求
1.1 最基本的GET請求
# 寫法一: response = requests.get("http://www.baidu.com/") # 寫法二: # response = requests.request("get", http://www.baidu.com/)
1.2 添加headers和查詢參數(shù)
如果想添加 headers,可以傳入headers參數(shù)來增加請求頭中的headers信息。如果要將參數(shù)放在url中傳遞,可以利用 params 參數(shù)。
import requests kw = {'wd':'長城'} headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} # params 接收一個(gè)字典或者字符串的查詢參數(shù),字典類型自動(dòng)轉(zhuǎn)換為url編碼,不需要urlencode() response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)
#查看響應(yīng)內(nèi)容,response.text 返回的是Unicode格式的數(shù)據(jù) print response.text #<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer> .....
# 查看響應(yīng)內(nèi)容,response.content返回的字節(jié)流數(shù)據(jù) print respones.content
# 查看完整url地址 print response.url # http://www.baidu.com/?wd=%E9%95%BF%E5%9F%8E
# 查看響應(yīng)頭部字符編碼 print response.encoding # ISO-8859-1
# 查看響應(yīng)碼 print response.status_code # 200
二、POST請求
2.1 最基本的POST請求
response = requests.post("http://www.baidu.com/", data = data)
2.2 傳入data數(shù)據(jù)
對于 POST 請求來說,我們一般需要為它增加一些參數(shù)。那么最基本的傳參方法可以利用 data 這個(gè)參數(shù)。
import requests formdata = { "type":"AUTO", "i":"i love python", "doctype":"json", "xmlVersion":"1.8", "keyfrom":"fanyi.web", "ue":"UTF-8", "action":"FY_BY_ENTER", "typoResult":"true" } url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null" headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"} response = requests.post(url, data = formdata, headers = headers)
print response.text # {"type":"EN2ZH_CN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"i love python","tgt":"我喜歡python"}]],"smartResult":{"type":1,"entries":["","肆文","高德納"]}}
# 如果是json文件可以直接顯示 print response.json() # {u'errorCode': 0, u'elapsedTime': 0, u'translateResult': [[{u'src': u'i love python', u'tgt': u'\u6211\u559c\u6b22python'}]], u'smartResult': {u'type': 1, u'entries': [u'', u'\u8086\u6587', u'\u9ad8\u5fb7\u7eb3']}, u'type': u'EN2ZH_CN'}
到此這篇關(guān)于詳解Python requests模塊的文章就介紹到這了,更多相關(guān)Python requests模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用實(shí)例分析Python中method的參數(shù)傳遞過程
這篇文章主要介紹了用實(shí)例分析Python中method的參數(shù)傳遞過程,包括instancemethod和staticmethod等實(shí)例,需要的朋友可以參考下2015-04-04Python 面向?qū)ο笾庋b、繼承、多態(tài)操作實(shí)例分析
這篇文章主要介紹了Python 面向?qū)ο笾庋b、繼承、多態(tài)操作,結(jié)合實(shí)例形式分析了Python面相對象程序設(shè)計(jì)中封裝、繼承、多態(tài)相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-11-11pytorch?實(shí)現(xiàn)情感分類問題小結(jié)
本文主要介紹了pytorch?實(shí)現(xiàn)情感分類問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02