- HTTP for Humans,更簡潔更友好
- 繼承了urllib的所有特征
- 底層使用的是urllib3
- 開源地址: https://github.com/requests/requests
- 中文文檔: http://docs.python-requests.org/zh_CN/latest/index.html
- 安裝: conda install requests
- get請求
- requests.get(url)
- requests.request("get", url)
- 可以帶有headers和parmas參數
案例一:
import requests
url = "http://www.baidu.com"
# 兩種請求方式
# 使用get請求
rsp = requests.get(url)
print(rsp.text)
# 使用request請求
rsp = requests.request("get",url)
print(rsp.text)
運行結果如下:

案例二:
'''
使用參數headers和params
研究返回結果
'''
import requests
url = "http://www.baidu.com/s?"
kw = {
"wd": "好人"
}
headers = {
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
rsp = requests.get(url,params=kw,headers=headers)
# print(rsp.text)
# print(rsp.content)
print(rsp.url)
print(rsp.encoding)
print(rsp.status_code)
運行結果如下:

