python爬蟲基礎教程:requests庫(二)代碼實例
更新時間:2019年04月09日 16:50:45 作者:嗨學編程
這篇文章主要介紹了python爬蟲基礎教程:requests庫(二),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
get請求
簡單使用
import requests ''' 想要學習Python?Python學習交流群:973783996滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載! ''' response = requests.get("https://www.baidu.com/") #text返回的是unicode的字符串,可能會出現(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請求
爬去拉鉤網(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對象,可以保存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ā)送附帶用戶名和密碼的請求,并獲取登錄后的Cookie值,保存在ssion里 ssion.post("http://www.renren.com/PLogin.do", data = data) # 5. ssion包含用戶登錄后的Cookie值,可以直接訪問那些登錄后才可以訪問的頁面 response = ssion.get("http://zhibo.renren.com/news/108") # 6. 打印響應內(nèi)容 print(response.text)
以上所述是小編給大家介紹的python爬蟲基礎教程:requests庫(二)詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Python實現(xiàn)二叉樹的常見遍歷操作總結【7種方法】
這篇文章主要介紹了Python實現(xiàn)二叉樹的常見遍歷操作,結合實例形式總結分析了二叉樹的前序、中序、后序、層次遍歷中的迭代與遞歸等7種操作方法,需要的朋友可以參考下2019-03-03Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
今天小編就為大家分享一篇關于Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04pytorch中tensor轉(zhuǎn)換為float的實現(xiàn)示例
本文主要介紹了pytorch中tensor轉(zhuǎn)換為float,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-03-03Pytorch中torch.unsqueeze()與torch.squeeze()函數(shù)詳細解析
torch.squeeze()這個函數(shù)主要對數(shù)據(jù)的維度進行壓縮,去掉維數(shù)為1的的維度,下面這篇文章主要給大家介紹了關于Pytorch中torch.unsqueeze()與torch.squeeze()函數(shù)詳細的相關資料,需要的朋友可以參考下2023-02-02