python爬蟲(chóng)基礎(chǔ)教程:requests庫(kù)(二)代碼實(shí)例
get請(qǐng)求
簡(jiǎn)單使用
import requests ''' 想要學(xué)習(xí)Python?Python學(xué)習(xí)交流群:973783996滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載! ''' response = requests.get("https://www.baidu.com/") #text返回的是unicode的字符串,可能會(huì)出現(xiàn)亂碼情況 # print(response.text) #content返回的是字節(jié),需要解碼 print(response.content.decode('utf-8')) # print(response.url) #https://www.baidu.com/ # print(response.status_code) #200 # print(response.encoding) #ISO-8859-1
添加headers和params
import requests params = { 'wd':'python' } headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36' } response = requests.get("https://www.baidu.com/s",params=params,headers=headers) #content返回的是字節(jié),需要解碼 with open('baidu.html','w',encoding='utf-8') as f: f.write(response.content.decode('utf-8'))
POST請(qǐng)求
爬去拉鉤網(wǎng)職位信息
import requests url = "https://www.lagou.com/jobs/positionAjax.json?city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false" data = { 'first':'true', 'pn':1, 'kd':'python' } headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36", "Referer":"https://www.lagou.com/jobs/list_python?city=%E5%8C%97%E4%BA%AC&cl=false&fromSearch=true&labelWords=&suginput=" } response = requests.post(url,data=data,headers=headers) # print(response.text) print(type(response.text)) #<class 'str'> print(type(response.json())) #<class 'dict'> print(response.json()) #獲取為字典的形式
使用代理
import requests proxy = {'http':'115.210.31.236.55:9000'} response = requests.get("https://www.baidu.com/",proxies=proxy) print(response.content.decode('utf-8'))
session登錄
# _*_ coding:utf-8 _*_ import requests # 1. 創(chuàng)建session對(duì)象,可以保存Cookie值 ssion = requests.session() # 2. 處理 headers headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'} # 3. 需要登錄的用戶名和密碼 data = {"email":"158xxxxxxxx", "password":"pythonxxxxxxx"} # 4. 發(fā)送附帶用戶名和密碼的請(qǐng)求,并獲取登錄后的Cookie值,保存在ssion里 ssion.post("http://www.renren.com/PLogin.do", data = data) # 5. ssion包含用戶登錄后的Cookie值,可以直接訪問(wèn)那些登錄后才可以訪問(wèn)的頁(yè)面 response = ssion.get("http://zhibo.renren.com/news/108") # 6. 打印響應(yīng)內(nèi)容 print(response.text)
以上所述是小編給大家介紹的python爬蟲(chóng)基礎(chǔ)教程:requests庫(kù)(二)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python實(shí)現(xiàn)二叉樹(shù)的常見(jiàn)遍歷操作總結(jié)【7種方法】
這篇文章主要介紹了Python實(shí)現(xiàn)二叉樹(shù)的常見(jiàn)遍歷操作,結(jié)合實(shí)例形式總結(jié)分析了二叉樹(shù)的前序、中序、后序、層次遍歷中的迭代與遞歸等7種操作方法,需要的朋友可以參考下2019-03-03Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
今天小編就為大家分享一篇關(guān)于Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04pytorch中tensor轉(zhuǎn)換為float的實(shí)現(xiàn)示例
本文主要介紹了pytorch中tensor轉(zhuǎn)換為float,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03Python logging日志庫(kù)空間不足問(wèn)題解決
這篇文章主要介紹了Python logging日志庫(kù)空間不足問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Pytorch中torch.unsqueeze()與torch.squeeze()函數(shù)詳細(xì)解析
torch.squeeze()這個(gè)函數(shù)主要對(duì)數(shù)據(jù)的維度進(jìn)行壓縮,去掉維數(shù)為1的的維度,下面這篇文章主要給大家介紹了關(guān)于Pytorch中torch.unsqueeze()與torch.squeeze()函數(shù)詳細(xì)的相關(guān)資料,需要的朋友可以參考下2023-02-02Python實(shí)現(xiàn)遞歸遍歷文件夾并刪除文件
本文給大家匯總了3個(gè)Python實(shí)現(xiàn)遍歷文件夾并刪除的代碼,主要是給大家分享下這3種方法的實(shí)現(xiàn)思路,有需要的小伙伴可以參考下2016-04-04