轉(zhuǎn)自:https://zhuanlan.zhihu.com/p/26681429
我們來著重講一下 **kwargs 這個(gè)參數(shù)kwargs:
控制訪問的參數(shù),均為可選項(xiàng)
params : 字典或字節(jié)序列,作為參數(shù)增加到url中
data : 字典、字節(jié)序列或文件對象,作為Request的內(nèi)容
json : JSON格式的數(shù)據(jù),作為Request的內(nèi)容
headers : 字典,HTTP定制頭
cookies : 字典或CookieJar,Request中的cookie
auth : 元組,支持HTTP認(rèn)證功能
files : 字典類型,傳輸文件timeout : 設(shè)定超時(shí)時(shí)間,秒為單位
proxies : 字典類型,設(shè)定訪問代理服務(wù)器,可以增加登錄認(rèn)證
allow_redirects : True/False,默認(rèn)為True,重定向開關(guān)
stream : True/False,默認(rèn)為True,獲取內(nèi)容立即下載開關(guān)
verify : True/False,默認(rèn)為True,認(rèn)證SSL證書開關(guān)
cert : 本地SSL證書路徑
url: 擬更新頁面的url鏈接
data: 字典、字節(jié)序列或文件,Request的內(nèi)容
json: JSON格式的數(shù)據(jù),Request的內(nèi)容
importre,requests
r=requests.head('http://www.baidu.com')
#HTTP請求的返回狀態(tài),比如,200表示成功,404表示失敗print(r.status_code)
#HTTP請求中的headers
print(r.headers)
#從header中猜測的響應(yīng)的內(nèi)容編碼方式
print(r.encoding)
#從內(nèi)容中分析的編碼方式(慢)
print(r.apparent_encoding)
#響應(yīng)內(nèi)容的二進(jìn)制形式
print(r.content)
import requests
def getHtmlText(url):
????try:
????????r=requests.get(url,timeout=30)
????????# 如果狀態(tài)碼不是200 則應(yīng)發(fā)HTTOError異常
????????r.raise_for_status()
????????# 設(shè)置正確的編碼方式
????????r.encoding=r.apparent_encoding
????????returnr.text
????except:
????????return"Something Wrong!"
requests 文檔:
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html