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. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲2. JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理詳解3. Java SE 6 : Classpath通配符和OutOfMemeory Error檢測(cè)4. python json 遞歸打印所有json子節(jié)點(diǎn)信息的例子5. Python中tkinter+MySQL實(shí)現(xiàn)增刪改查6. 源碼分析MinimalApi是如何在Swagger中展示7. windows下安裝PHP性能分析工具 xhprof 筆記8. Vue Element UI自定義描述列表組件9. js實(shí)現(xiàn)經(jīng)典掃雷游戲10. Geronimo 2.0 M1發(fā)布 支持Java EE
