Python爬蟲requests庫多種用法實(shí)例
requests安裝和使用
下載安裝:pip install requests
#requests模塊import requests#發(fā)送請(qǐng)求 content:以二進(jìn)制的形式獲取網(wǎng)頁的內(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)頁上的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':'(.*?)'}]]’ #字符串中有'',再用’’括起來表示字符串# 獲取到的是字符串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'}#通過傳遞用戶名密碼得到cookie信息ses.post('http://www.renren.com/PLogin.do',data=data,headers=header)#請(qǐng)求需要的頁面,每次請(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. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. 解析原生JS getComputedStyle4. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. 讀大數(shù)據(jù)量的XML文件的讀取問題8. css代碼優(yōu)化的12個(gè)技巧9. 利用CSS3新特性創(chuàng)建透明邊框三角10. 前端從瀏覽器的渲染到性能優(yōu)化
