Python爬蟲requests庫多種用法實例
requests安裝和使用
下載安裝:pip install requests
#requests模塊import requests#發(fā)送請求 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)
添加請求頭和參數(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':'中國'}response=requests.get(url,params=wd,headers=headers)# 返回一個字符串形式的數(shù)據(jù)data=response.text# 返回一個二進(jìn)制形式的數(shù)據(jù)data2=response.contentprint(data2.decode())
處理Post請求
處理get請求:get()方法
處理post請求:post()方法
import requestsimport re#構(gòu)造請求頭信息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ù)# 對應(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地址:端口號'}#可以設(shè)置多個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對象cookiejar=response.cookies#2.將cookiejar轉(zhuǎn)換成字典cookiedict=requests.utils.dict_from_cookiejar(cookiejar)print(cookiedict)
session實現(xiàn)登陸
相比直接使用cookie,創(chuàng)建session可以得到新的cookie信息,不會出現(xiàn)cookie失效的情況
#使用session實現(xiàn)登陸import requests#構(gòu)造請求頭信息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對象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)#請求需要的頁面,每次請求會帶入cookie信息response=ses.get('http://www.renren.com/880151247/profile')print(response.text)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python中scrapy處理項目數(shù)據(jù)的實例分析2. 快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載)3. Python requests庫參數(shù)提交的注意事項總結(jié)4. js抽獎轉(zhuǎn)盤實現(xiàn)方法分析5. Django ORM實現(xiàn)按天獲取數(shù)據(jù)去重求和例子6. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程7. AJAX的跨域問題解決方案8. IntelliJ IDEA導(dǎo)入jar包的方法9. 通過Python pyecharts輸出保存圖片代碼實例10. vue-electron中修改表格內(nèi)容并修改樣式
