安裝和文檔地址
直接使用pip可以非常方便的安裝
pip install requests
中文文檔:http://docs.python-requests.org/zh_CN/latest/index.html
github地址:https://github.com/requests/requests
發(fā)送get請(qǐng)求
- 最簡(jiǎn)單的發(fā)送get請(qǐng)求
import requests
response = requests.get('https://www.baidu.com/')
- respnse.text和response.content的區(qū)別
- response.content:這個(gè)是直接從網(wǎng)絡(luò)上抓取的數(shù)據(jù),沒(méi)有經(jīng)過(guò)任何的解碼哗戈,所以是bytes類型,其實(shí)在硬盤上和網(wǎng)絡(luò)上的字符串都是bytes類型
- response.text:這個(gè)是requests,將response.content進(jìn)行解碼的字符串,解碼需要指定一個(gè)編碼方式哭廉,requests對(duì)根據(jù)自己的猜測(cè)來(lái)判斷解碼的方式郑原,所以有時(shí)候會(huì)產(chǎn)生亂碼,這個(gè)時(shí)候就應(yīng)該使用
response.content.decode('utf-8')
來(lái)進(jìn)行手動(dòng)解碼
import requests
response = requests.get('https://www.baidu.com/')
# 這個(gè)返回的是Unicode格式的數(shù)據(jù)類型陶珠,也就是字符串,
# 但是使用這個(gè)有弊端享钞,中文亂碼揍诽,還不能自己指定打印出來(lái)的字符類型
print(type(response.text))
print(response.text)
# 這個(gè)返回的是字節(jié)流數(shù)據(jù)诀蓉,也就是bytes類型,使用這個(gè)打印出來(lái)的
# 中文依然是亂碼暑脆,但是這個(gè)可以進(jìn)行解碼,decode('utf-8')
print(type(response.content))
print(response.content.decode('utf-8'))
# 這是一些常用的方法
print(response.url) # 查看完整的url地址
print(response.encoding) # 查看響應(yīng)頭部的字符編碼
print(response.status_code) # 查看響應(yīng)碼
- text打印出來(lái)的.png
- [圖片上傳失敗...(image-2cf12e-1523456588525)]
- 添加headers和查詢參數(shù)
- 如果想添加 headers渠啤,可以傳入headers參數(shù)來(lái)增加請(qǐng)求頭中的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()
resp = requests.get('http://www.baidu.com/s',params=kw,headers=headers)
# 這里寫入的時(shí)候需要注意編碼格式
with open('baidu.html','w',encoding='utf-8') as fp:
# 寫入的格式一般就是這樣
fp.write(resp.content.decode('utf-8'))
print(resp.url)
看到打印的url.png
發(fā)送post請(qǐng)求
- 最基本的POST請(qǐng)求可以使用post方法
response = requests.post("http://www.baidu.com/",data=data)
- 傳入data數(shù)據(jù)
- 這時(shí)候就不要再使用urlencode進(jìn)行編碼了,直接傳入一個(gè)字典進(jìn)去就可以了
#encoding: utf-8
import requests
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false&isSchoolJob=0'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
'Referer':'https://www.lagou.com/jobs/list_python?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=&suginput='
}
data = {
'first':'true',
'pn':3,
'kd':'python'
}
resp = requests.post(url=url,data=data,headers=headers)
# 如果是json數(shù)據(jù)碟联,直接可以調(diào)用json方法,返回的字典
print(resp.json())
獲取到返回的json數(shù)據(jù).png
使用代理
使用requests添加代理也非常簡(jiǎn)單妓美,只要在請(qǐng)求的方法中(比如get或者post)傳遞proxies參數(shù)就可以了
#encoding: utf-8
import requests
url = 'https://httpbin.org/ip'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}
proxy = {
'http':'117.28.144.33:21879'
}
resp = requests.get(url,headers=headers,proxies=proxy)
print(resp.text)
cookie
如果在一個(gè)響應(yīng)中包含了cookie,那么可以利用cookies屬性拿到這個(gè)返回的cookie
#encdoing: utf-8
import requests
resp = requests.get('https://www.baidu.com')
# 獲取cookie信息
# print(resp.cookies)
# 以字典的形式獲取cookie信息
print(resp.cookies.get_dict())
session
之前使用urllib庫(kù)鲤孵,是可以使用opener發(fā)送多個(gè)請(qǐng)求壶栋,多個(gè)請(qǐng)求之間是可以共享cookie的,
如果使用requests也要達(dá)到共享cookie的目的,那么這個(gè)時(shí)候就可以使用session對(duì)象
#encdoing: utf-8
import requests
url = 'http://www.renren.com/PLogin.do'
data = {'email':'15837503603','password':'python'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}
# 登陸,這個(gè)session中已經(jīng)存了cookie信息普监,以后用session訪問(wèn)
session = requests.session()
session.post(url=url,headers=headers,data=data)
# 訪問(wèn)你想訪問(wèn)的個(gè)人主頁(yè)
url2 = 'http://www.renren.com/880151247/profile'
resp = session.get(url=url2,headers=headers)
print(resp.content.decode('utf-8'))
處理不信任的SSL證書(shū)
對(duì)于那些已經(jīng)被信任的SSL整數(shù)的網(wǎng)站贵试,比如https://www.baidu.com/,那么使用requests直接就可以正常的返回響應(yīng)
resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)
print(resp.content.decode('utf-8'))