相比於urllib模塊,requests模塊使用起來更簡潔方便。requests支持HTTP鏈接保持和鏈接池,支持使用cookie保持會話,支持文件上傳,支持自動肯定響應內容的編碼,支持國際化的 URL 和 POST 數據自動編碼。requests的底層實現其實就是urllib3。web
sudo pip3 install requests
import requests response = requests.get("https://www.baidu.com/") # 也能夠寫成 # response = requests.request("get", "https://www.baidu.com/")
若是想添加headers,能夠傳入headers
參數來增長請求頭中的headers信息。若是要將參數放在url中傳遞,能夠利用 params
參數。json
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 接收一個字典或者字符串的查詢參數,字典類型自動轉換爲url編碼,不須要urlencode() response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers) # 查看響應內容,response.text 返回的是Unicode格式的數據 print(response.text) # 查看響應內容,response.content返回的字節流數據 print(respones.content) # 查看完整url地址 print(response.url) # 查看響應頭部字符編碼 print(response.encoding) # 查看響應碼 print(response.status_code)
使用response.text 時,Requests 會基於 HTTP 響應的文本編碼自動解碼響應內容,大多數 Unicode 字符集都能被無縫地解碼。使用response.content 時,返回的是服務器響應數據的原始二進制字節流,能夠用來保存圖片等二進制文件。api
import requests resp = requests.post('https://www.baidu.com/',data = data)
import requests formdata = { 'i':'I love you', "from":'AUTO', "to":'AUTO', "smartresult":"dict", "client":"fanyideskweb", "doctype":"json", "version":"2.1", "keyfrom":"fanyi.web", "action":"FY_BY_REALTlME" } 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) # 若是是json文件能夠直接顯示,返回的是dict類型 print(response.json())
import requests import json url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList' headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' } # 將json類型的數據轉換成字典類型的數據 data = { "portal":2,"lang":"zh-CN","country":"CN","brand":1,"province":"山西","city":"太原","pageNo":1,"pageSize":20 } # 調用json.dumps()方法,將數據以json格式傳遞 resp = requests.post(url=url, headers=headers, data=json.dumps(data) print(resp.text)
import requests url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList' headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' } json = {"portal":2,"lang":"zh-CN","country":"CN","brand":1,"province":"山西","city":"太原","pageNo":1,"pageSize":20} # 直接給post方法中的json參數賦值,賦dict類型的數據 response = requests.post(url=url, headers=headers, json=json) print(response.text)
若是須要使用代理,你能夠爲任意請求方法提供 proxies
參數來配置單個請求:瀏覽器
import requests # 根據協議類型,選擇不一樣的代理 proxies = { "http": "http://12.34.56.79:9527", "https": "http://12.34.56.79:9527", } resp = requests.get("https://www.baidu.com", proxies = proxies) print(resp.text)
import requests # 若是代理須要使用HTTP Basic Auth,能夠使用下面這種格式: proxy = { "http": "mr_mao_hacker:sffqry9r@61.158.163.130:16816" } resp = requests.get("http://www.baidu.com", proxies = proxy) print(resp.text)
mport requests #添加 auth = (帳戶名, 密碼) auth=('test', '123456') resp = requests.get('http://192.168.199.107', auth = auth) print(resp.text)
import requests response = requests.get("http://www.baidu.com/") # 返回CookieJar對象: cookiejar = response.cookies # 將CookieJar轉爲字典: cookiedict = requests.utils.dict_from_cookiejar(cookiejar) print(cookiejar) print(cookiedict)
import requests # 1. 建立session對象,能夠保存Cookie值 ssion = requests.session() # 2. 處理 headers 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"} # 3. 須要登陸的用戶名和密碼 data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"} # 4. 發送附帶用戶名和密碼的請求,並獲取登陸後的Cookie值,保存在ssion裏 ssion.post("http://www.renren.com/PLogin.do", data = data) # 5. ssion包含用戶登陸後的Cookie值,能夠直接訪問那些登陸後才能夠訪問的頁面 resp = ssion.get("http://www.renren.com/410043129/profile") # 6. 打印響應內容 print(resp.text)
import requests resp = requests.get("https://www.baidu.com/", verify=True) # 也能夠省略不寫 # resp = requests.get("https://www.baidu.com/") print(resp.text)
import requests resp = requests.get("https://www.12306.cn/mormhweb/", verify = False) print(resp.text)