Python爬蟲(chóng)requests庫(kù)多種用法實(shí)例
requests安裝和使用
下載安裝:pip install requests
#requests模塊import requests#發(fā)送請(qǐng)求 content:以二進(jìn)制的形式獲取網(wǎng)頁(yè)的內(nèi)容response=requests.get('http://www.baidu.com').content.decode()#response=requests.request('get','http://www.baidu.com').content.decode()print(response)
添加請(qǐng)求頭和參數(shù)
import requestsurl='http://www.baidu.com/s?'headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}wd={'wd':'中國(guó)'}response=requests.get(url,params=wd,headers=headers)# 返回一個(gè)字符串形式的數(shù)據(jù)data=response.text# 返回一個(gè)二進(jìn)制形式的數(shù)據(jù)data2=response.contentprint(data2.decode())
處理Post請(qǐng)求
處理get請(qǐng)求:get()方法
處理post請(qǐng)求:post()方法
import requestsimport re#構(gòu)造請(qǐng)求頭信息header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}#谷歌瀏覽器#http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule 網(wǎng)頁(yè)上的urlurl='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'key='靚仔'#發(fā)送到web服務(wù)器的表單數(shù)據(jù)formdata={'i':key,'from':'AUTO','to':'AUTO','smartresult':'dict','client':'fanyideskweb','salt':'15880563488791','sign':'cc2c40d740538fc5edc0380891faef27','ts':'1588053583943','bv':'f9c86b1fdf2f53c1fefaef343285247b','doctype':'json','version':'2.1','keyfrom':'fanyi.web','action':'FY_BY_REALTlME'}response=requests.post(url,headers=header,data=formdata)# 獲取到的是json數(shù)據(jù)# 對(duì)應(yīng)的是字典# print(response.json())pat=r’'tgt':'(.*?)'}]]’ #字符串中有'',再用’’括起來(lái)表示字符串# 獲取到的是字符串result=re.findall(pat,response.text)print(result[0])
代理IP
import requests#設(shè)置ip地址#proxy={'http':'http://代理ip地址:端口號(hào)'}#可以設(shè)置多個(gè)proxy={'http':'http://222.82.130.23:8060','http':'http://101.248.64.68:80',}response=requests.get('http://www.baidu.com',proxies=proxy)print(response.content.decode())
獲取響應(yīng)的cookie
cookie:用戶信息
import requestsresponse=requests.get('http://www.baidu.com')#1.獲取返回的cooketjar對(duì)象cookiejar=response.cookies#2.將cookiejar轉(zhuǎn)換成字典cookiedict=requests.utils.dict_from_cookiejar(cookiejar)print(cookiedict)
session實(shí)現(xiàn)登陸
相比直接使用cookie,創(chuàng)建session可以得到新的cookie信息,不會(huì)出現(xiàn)cookie失效的情況
#使用session實(shí)現(xiàn)登陸import requests#構(gòu)造請(qǐng)求頭信息header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'}#谷歌瀏覽器#創(chuàng)建session對(duì)象ses=requests.session()#構(gòu)造登陸需要的參數(shù)data={'email':'325*****@qq.com','password':'123321a'}#通過(guò)傳遞用戶名密碼得到cookie信息ses.post('http://www.renren.com/PLogin.do',data=data,headers=header)#請(qǐng)求需要的頁(yè)面,每次請(qǐng)求會(huì)帶入cookie信息response=ses.get('http://www.renren.com/880151247/profile')print(response.text)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于PHP做個(gè)圖片防盜鏈2. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)3. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)4. XML在語(yǔ)音合成中的應(yīng)用5. jscript與vbscript 操作XML元素屬性的代碼6. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解7. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字8. 如何使用ASP.NET Core 配置文件9. .NET中實(shí)現(xiàn)對(duì)象數(shù)據(jù)映射示例詳解10. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)
