Python爬蟲requests庫多種用法實例
requests安裝和使用
下載安裝:pip install requests
#requests模塊 import requests #發(fā)送請求 content:以二進制的形式獲取網(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 requests url="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 # 返回一個二進制形式的數(shù)據(jù) data2=response.content print(data2.decode())
處理Post請求
處理get請求:get()方法
處理post請求:post()方法
import requests import 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)頁上的url url="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 requests response=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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?matplotlib數(shù)據(jù)可視化圖繪制
這篇文章主要介紹了Python?matplotlib數(shù)據(jù)可視化圖繪制,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07Python循環(huán)中else,break和continue的用法實例詳解
這篇文章主要介紹了Python循環(huán)中else,break和continue的用法,結(jié)合實例形式詳細(xì)分析了Python for循環(huán)、while循環(huán)中else,break和continue的功能、用法及相關(guān)操作注意事項,需要的朋友可以參考下2019-07-07Python遍歷目錄下文件、讀取、千萬條數(shù)據(jù)合并詳情
這篇文章主要介紹了Python遍歷目錄下文件、讀取、千萬條數(shù)據(jù)合并詳情,對文件夾和文件進行屬性判斷,首先對文件夾進行遍歷,看文件夾里有什么樣的文件,讀取出文件夾中的所有文件,下面文章將詳細(xì)介紹該內(nèi)容,需要的小伙伴可以參考一下2022-01-01對python中Matplotlib的坐標(biāo)軸的坐標(biāo)區(qū)間的設(shè)定實例講解
今天小編就為大家分享一篇對python中Matplotlib的坐標(biāo)軸的坐標(biāo)區(qū)間的設(shè)定實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05python 實現(xiàn)"神經(jīng)衰弱"翻牌游戲
這篇文章主要介紹了python 實現(xiàn)"神經(jīng)衰弱"游戲,幫助大家更好的理解和使用python的pygame庫,感興趣的朋友可以了解下2020-11-11Python Pandas數(shù)據(jù)分析工具用法實例
這篇文章主要介紹了Python Pandas數(shù)據(jù)分析工具用法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11