獲取響應(yīng)信息
import requests
response = requests.get('http://www.baidu.com')
print(response.status_code) # 狀態(tài)碼
print(response.url) # 請求url
print(response.headers) # 響應(yīng)頭信息
print(response.cookies) # cookie信息
print(response.content) # bytes形式的響應(yīng)內(nèi)容
print(response.encoding) # 獲取響應(yīng)內(nèi)容編碼
response.encoding=”utf-8” # 指定響應(yīng)內(nèi)容編碼
print(response.text) # 文本形式的響應(yīng)內(nèi)容,response.content編碼后的結(jié)果
發(fā)送Get請求
不帶參數(shù)的Get請求
response = requests.get('http://www.baidu.com')
print(response.text)
帶參數(shù)的Get請求
直接寫在url后面
在url后面用?表示帶上參數(shù),每對參數(shù)用&分隔。如下url:
https://www.bilibili.com/vide...
注意:url最長2048字節(jié),且數(shù)據(jù)透明不安全
作為字典參數(shù)傳入
data = {'name': 'xiaoming', 'age': 26}
response = requests.get('http://www.abcd.com', params=data)
print(response.text)
發(fā)送post請求
只能作為字典參數(shù)傳入,注意參數(shù)名字是data而不是params
data = {'name': 'xiaoming', 'age': 26}
response = requests.post('http://www.abcd.com', data=data)
print(response.text)
添加headers
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 ' /
'(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' /
'(KHTML, like Gecko) Version/5.1 Safari/534.50'
response = requests.get('http://www.baidu.com',headers=headers)
使用代理
proxy = {'http': '49.89.84.106:9999', 'https': '49.89.84.106:9999'}
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'
req = requests.get(url, proxies=proxy, headers=heads)
print(req.text)
使用加密代理
from requests.auth import HTTPProxyAuth
proxies= {'http': '127.0.0.1:8888', 'https': '127.0.0.1:8888'}
auth = HTTPProxyAuth('user', 'pwd')
requests.get(url, proxies=proxies, auth=auth)
也可以這樣
proxies = {"http": "http://user:pass@10.10.1.10:3128/",}
req = requests.get(url, proxies=proxy, headers=heads)
Cookie
獲取Cookie
import requests
response = requests.get("http://www.baidu.com")
print(type(response.cookies))
# 把cookiejar對象轉(zhuǎn)化為字典
cookies = requests.utils.dict_from_cookiejar(response.cookies)
print(cookies)
使用Cookie
cookie = {"Cookie":"xxxxxxxx"}
response = requests.get(url,cookies=cookie)
Session
session = requests.Session()
session.get('http://httpbin.org/cookies/set/number/12345')
response = session.get('http://httpbin.org/cookies')
print(response.text)
限定響應(yīng)時間
from requests.exceptions import ReadTimeout
try:
response = requests.get('https://www.baidu.com', timeout=1)
print(response.status_code)
except :
print('給定時間內(nèi)未響應(yīng)')
解析JSON格式的響應(yīng)內(nèi)容
通過response.json()方法可以將為JSON格式的響應(yīng)內(nèi)容轉(zhuǎn)變?yōu)?a href="http://www.www27dydycom.cn/tags/python/" target="_blank">Python的對象,json.loads(response.text)也能起到同樣的作用
response = requests.get('http://www.abcd.com')
print(response.text)
print(response.json())
print(type(response.json()))
想進(jìn)一步了解編程開發(fā)相關(guān)知識,與我一同成長進(jìn)步,請關(guān)注我的公眾號“松果倉庫”,共同分享宅&程序員的各類資源,謝謝?。。?br />
審核編輯 黃昊宇
-
python
+關(guān)注
關(guān)注
56文章
4822瀏覽量
85876 -
爬蟲
+關(guān)注
關(guān)注
0文章
83瀏覽量
7284
發(fā)布評論請先 登錄
相關(guān)推薦
評論