詳解基于pycharm的requests庫使用教程
requests庫安裝和導(dǎo)入
第一步:cmd打開命令行,使用如下命令安裝requests庫。
pip install requests
由于我的安裝過了,所以如下:
如果提示你pip版本需要更新,按照提示的指令輸入即可更新。
第二步:cmd使用如下命令,驗(yàn)證requests庫安裝完成。
pip list
第三步:在pycharm中,點(diǎn)擊file——settings——project——python interpreter——點(diǎn)擊+號——搜索requests——install package!
第四步:在你寫的.py文件中,使用如下命令導(dǎo)入即可。
import requests
requests庫的一個類型六個屬性
import requests url = "https://www.baidu.com" response = requests.get(url=url) # 一個類型六個屬性 # 類型 print(type(response)) # 設(shè)置響應(yīng)的編碼格式 response.encoding = 'utf-8' # 以字符串的形式返回網(wǎng)頁的源碼 print(response.text) # 返回一個url地址 print(response.url) # 返回的是二進(jìn)制數(shù)據(jù) print(response.content) # 返回相應(yīng)的狀態(tài)碼 print(response.status_code) # 返回的響應(yīng)頭 print(response.headers)
輸出結(jié)果如下:
<class 'requests.models.Response'>
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
post一般是表單請求,如果你直接在百度搜一個東西,那是get請求奧!
requests庫的get請求
首先將代碼寫出來,然后根據(jù)代碼給大家將對應(yīng)的知識點(diǎn),算是入門。
import requests url = "https://www.baidu.com/s?" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } data = { 'wd': '北京' } # url請求路徑 params參數(shù) kwargs字典 response = requests.get(url=url, params=data, headers=headers) # 參數(shù)使用params傳遞,且參數(shù)無需url encode編碼 ,且參數(shù)也不需要對象定制,請求資源路徑中的?可加可不加 print(response.text)
第一步:首先來看requests庫的get方法使用及參數(shù)含義。
response = requests.get(url=url, params=data, headers=headers)
url表示請求路徑,params表示參數(shù),kwargs表示字典。
參數(shù)使用params傳遞,且參數(shù)無需url encode編碼 ,且參數(shù)也不需要對象定制,請求資源路徑中的?可加可不加。
第二步:下面演示一下,這三個參數(shù)怎么傳遞。
接下來的講解,學(xué)過前端的應(yīng)該都知道怎么弄吧?
右鍵檢查——選擇如下——然后刷新
這個地方是我們請求的url!
這個地方是我們傳遞的數(shù)據(jù)params!
可能很多人會找From Data,這個地方應(yīng)該是PayLoad,注意一下!
這個地方是我們傳遞的字典!
選擇下面的user agent,其中有我們的瀏覽器相關(guān)信息。
在上述中,應(yīng)該注意,由于get的后兩個其實(shí)都是用python中的字典的形式存儲的,所以獲取數(shù)據(jù)后,注意一下格式。
第三步:我們來看看有沒有數(shù)據(jù),可以在輸出地方,使用ctrl + f來搜索驗(yàn)證我們想要的內(nèi)容在不在。
requests庫的post請求
首先將代碼寫出來,然后根據(jù)代碼給大家將對應(yīng)的知識點(diǎn),算是入門。
import requests url = "https://fanyi.baidu.com/sug" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } data = { 'kw': 'eye' } # url請求路徑 data請求參數(shù) kwargs字典 response = requests.post(url=url, data=data, headers=headers) # 參數(shù)使用data傳遞,且參數(shù)無需url encode編碼 ,且參數(shù)也不需要對象定制 print(response.text)
輸出結(jié)果:
{"errno":0,"data":[{"k":"eye","v":"n. \u773c\u775b; \u89c6\u529b; \u773c\u72b6\u7269; \u98ce\u7eaa\u6263\u6263\u773c vt. \u5b9a\u775b\u5730\u770b; \u6ce8\u89c6; \u5ba1\u89c6; \u7ec6\u770b"},{"k":"Eye","v":"[\u4eba\u540d] \u827e; [\u5730\u540d] [\u82f1\u56fd] \u827e\u4f0a"},{"k":"EYE","v":"abbr. European Year of the Environment \u6b27\u6d32\u73af\u5883\u5e74; Iwas"},{"k":"eyed","v":"adj. \u6709\u773c\u7684"},{"k":"eyer","v":"n. \u6ce8\u89c6\u7684\u4eba"}]}
第一步:首先來看requests庫的post方法使用及參數(shù)含義。
response = requests.post(url=url, data=data, headers=headers)
這里的參數(shù)和get方法還有點(diǎn)不同,我們想看詳細(xì)的話可以這樣看,在pycharm中選中方法,即可看到提示。
url表示的是請求路徑,data表示的是請求參數(shù),kwargs表示的是字典。
其實(shí)難點(diǎn)在于怎么找這個url奧!!即哪一個是我們想要的url?。∠旅嬉园俣确g為例??!
我圈起來的這些地方,一定要注意,選中Preserve log?。?/p>
就在左邊的Name中找,如果其對應(yīng)的這個PayLoad中的kw和我們搜索的一致,那就是的啦?。?!
第二步,可能返回的數(shù)據(jù)我們也看不懂,那就轉(zhuǎn)換成json的格式來看就行啦!!
import requests import json url = "https://fanyi.baidu.com/sug" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } data = { 'kw': 'eye' } # url請求路徑 data請求參數(shù) kwargs字典 response = requests.post(url=url, data=data, headers=headers) obj = json.loads(response.text, encoding='utf-8') print(obj)
輸出結(jié)果:
{'errno': 0, 'data': [{'k': 'eye', 'v': 'n. 眼睛; 視力; 眼狀物; 風(fēng)紀(jì)扣扣眼 vt. 定睛地看; 注視; 審視; 細(xì)看'}, {'k': 'Eye', 'v': '[人名] 艾; [地名] [英國] 艾伊'}, {'k': 'EYE', 'v': 'abbr. European Year of the Environment 歐洲環(huán)境年; Iwas'}, {'k': 'eyed', 'v': 'adj. 有眼的'}, {'k': 'eyer', 'v': 'n. 注視的人'}]}
requests庫的代理
代理主要處理的是,我們在模擬瀏覽器給服務(wù)器發(fā)送請求的時候,我們高速的快速的高頻次的訪問某個網(wǎng)站,那樣的話網(wǎng)站會崩潰的,所以會把我們的ip封掉,那我們怎么辦呢?換ip地址就好啦!
import requests url = "https://www.baidu.com/s?" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } data = { 'wd': 'ip' } response = requests.get(url=url, params=data, headers=headers) with open('daili.html', 'w', encoding='utf-8') as fp: fp.write(response.text)
就會發(fā)現(xiàn)寫了這個文件!
requests庫的cookie
我們是以古詩文網(wǎng)為例!
我們現(xiàn)在想要實(shí)現(xiàn)的功能就是,不用登錄,直接進(jìn)入內(nèi)部的頁面。
# 通過登錄進(jìn)入主頁面 # 通過找登錄接口 我們發(fā)現(xiàn)需要的參數(shù)很多 """ __VIEWSTATE: 9Y4yHRQS2k2z739MJJ/8Z0sKfZNltkFId83Z8jCtY3g00xYgg9bsv5oK+KT5DypNl37KWa0IyB+uOwrRPBvTybqGLDdd0chyrWLxhhlHBeAGWL/SLTGYfOh5L1M= __VIEWSTATEGENERATOR: C93BE1AE from: http://so.gushiwen.cn/user/collect.aspx email: 13237153218 pwd: wxm20010428 code: PDBG denglu: 登錄 """ # 我們觀察到__VIEWSTATE __VIEWSTATEGENERATOR code是一個可以變化的量 # __VIEWSTATE __VIEWSTATEGENERATOR 看不到的數(shù)據(jù)一般都是在頁面的源碼中 # 我們觀察到其在頁面源碼中 所以我們需要獲取頁面源碼 然后進(jìn)行解析就可以獲取了 # code是驗(yàn)證碼 import requests # 登錄url頁面 url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } response = requests.get(url=url, headers=headers) # print(response.text) # 解析頁面源碼 然后獲取__VIEWSTATE __VIEWSTATEGENERATOR from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'lxml') # 獲取__VIEWSTATE viewstate = soup.select('#__VIEWSTATE')[0].attrs.get('value') # 獲取__VIEWSTATEGENERATOR viewstategenerator = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value') # print(viewstate) # print(viewstategenerator) # 獲取驗(yàn)證碼圖片 code = soup.select('#imgCode')[0].attrs.get('src') # print(code) code_url = 'https://so.gushiwen.cn' + code # print(code_url) # 獲取驗(yàn)證碼的圖片后 下載到本地 然后觀察驗(yàn)證碼 觀察之后 然后在控制臺輸入這個驗(yàn)證碼 就將這個值給code # 怎么下載??? # import urllib.request # 此處和后面的請求不是同一個請求 驗(yàn)證碼就變了 # urllib.request.urlretrieve(url=code_url, filename='code.jpg') # request里面有一個方法session() 通過session的返回值就能使請求變成一個對象 session = requests.session() response_code = session.get(code_url) # 注意此處使用二進(jìn)制數(shù)據(jù) 因?yàn)槲覀円菆D片的下載 content_code = response_code.content with open('code.jpg', 'wb') as fp: fp.write(content_code) code_name = input('請輸入驗(yàn)證碼:') # 點(diǎn)擊登錄 url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx' data_post = { '__VIEWSTATE': viewstate, '__VIEWSTATEGENERATOR': viewstategenerator, 'from': 'http://so.gushiwen.cn/user/collect.aspx', 'email': '13237153218', 'pwd': 'wxm20010428', 'code': code_name, 'denglu': '登錄', } response_post = session.post(url=url_post, headers=headers, data=data_post) with open('gushiwen.html', 'w', encoding='utf-8') as fp: fp.write(response_post.text)
首先我們打開這個古詩文網(wǎng)的登錄頁面(假設(shè)已經(jīng)都注冊過了),現(xiàn)在我們要輸入正確的賬號,錯誤的密碼,正確的驗(yàn)證碼,點(diǎn)擊登錄,但是在提示后不要點(diǎn)擊確定,否則頁面會跳轉(zhuǎn),然后抓到這個登錄所需要的參數(shù)。
觀察參數(shù)后,先找到變化的參數(shù),再試圖去獲取變化的參數(shù),而且一般這種看不見的參數(shù),一般就是在源碼中,我們點(diǎn)擊查看源碼,然后ctrl+F搜索看不見的參數(shù),找到其位置。
然后我們模擬瀏覽器給服務(wù)器發(fā)送請求,獲取網(wǎng)頁源代碼后,使用bs4解析源代碼,然后相應(yīng)變化的參數(shù)后,再發(fā)送請求即可!
此處會生成兩個文件,并且code.jpg,在運(yùn)行的時候如果加載不出來,那就去項(xiàng)目的文件夾中查找。
自動識別驗(yàn)證碼
超級鷹!下載python開發(fā)文檔,并且將.py和一個圖片復(fù)制到項(xiàng)目中!
打開后,看一下.py文件,更改用戶名和密碼上去!
根據(jù)其中的提示更改這個用戶ID
但是由于我沒有充錢,沒給我返回哈哈哈哈哈!
大家可以去第三方平臺搞驗(yàn)證碼識別平臺??!
到此這篇關(guān)于詳解基于pycharm的requests庫使用教程的文章就介紹到這了,更多相關(guān)pycharm requests庫 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用Tesseract實(shí)現(xiàn)從圖像中讀取文本
Tesseract?是一個基于計(jì)算機(jī)的系統(tǒng),用于光學(xué)字符識別?(OCR)?和其他圖像到文本處理,本文將介紹如何使用?Python?中的?Tesseract?創(chuàng)建一個可以從圖像中讀取文本的程序,需要的可以參考下2023-11-11Python+pandas計(jì)算數(shù)據(jù)相關(guān)系數(shù)的實(shí)例
今天小編就為大家分享一篇Python+pandas計(jì)算數(shù)據(jù)相關(guān)系數(shù)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法
今天小編就為大家分享一篇Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python3 利用requests 庫進(jìn)行post攜帶賬號密碼請求數(shù)據(jù)的方法
今天小編就為大家分享一篇Python3 利用requests 庫進(jìn)行post攜帶賬號密碼請求數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10