python爬蟲之requests庫使用代理方式
在看這篇文章之前,需要大家掌握的知識技能:
- python基礎
- html基礎
- http狀態(tài)碼
讓我們看看這篇文章中有哪些知識點:
- get方法
- post方法
- header參數,模擬用戶
- data參數,提交數據
- proxies參數,使用代理
- 進階學習
安裝上requests庫
pip install requests
先來看下幫助文檔,看看requests的介紹,用python自帶的help命令
import requests help(requests)
output:
Help on package requests: NAME requests DESCRIPTION Requests HTTP Library ~~~~~~~~~~~~~~~~~~~~~ Requests is an HTTP library, written in Python, for human beings. Basic GET usage: >>> import requests >>> r = requests.get('https://www.python.org') >>> r.status_code 200 >>> 'Python is a programming language' in r.content True ... or POST: >>> payload = dict(key1='value1', key2='value2') >>> r = requests.post('https://httpbin.org/post', data=payload) >>> print(r.text) { ... "form": { "key2": "value2", "key1": "value1" }, ... } The other HTTP methods are supported - see `requests.api`. Full documentation is at <http://python-requests.org>. :copyright: (c) 2017 by Kenneth Reitz. :license: Apache 2.0, see LICENSE for more details.
這里解釋下,requests庫是由python編寫的對人類友好的http庫,并且舉例了GET與POST的方法。
GET方法
好的,那我們自己來測試下,就以請求百度為例吧,,,(誰讓百度這么耐抗的)
import requests r = requests.get('https://www.baidu.com') print(r.status_code) #打印返回的http code print(r.text) #打印返回結果的text
方便點,截了個圖給大家看,返回的code是200,說明請求正常拉回網頁了。
看下返回的text,有點不對頭,少了一些html標簽,最起碼百度兩個字得有吧。嗯,這是什么原因,,,
相信有些同學已經想到了,是沒有真實模擬用戶的請求,你去爬數據,還不模擬用戶請求,那肯定限制你啊。這個時候需要加一個header參數來搞定,至少要加一個user-agent吧。好,那咋們去找一個ua吧。別百度了,自己動手,豐衣足食。教大家一個辦法,用谷歌或者火狐的開發(fā)者工具。
谷歌瀏覽器的開發(fā)者工具
打開新標簽 —— 按F12——訪問下百度——找到NetWork——隨便點開一個——往下翻——看到ua了吧,復制上。
import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} r = requests.get('https://www.baidu.com', headers=headers) print(r.status_code) print(r.text)
嗯~~~數據有點多,往下翻翻,這下就正常了嘛,數據都有了。。。PS:不信?可以自己輸出一個html文件,瀏覽器打開看看唄
POST方法
只需要把get改成post就好了
import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} r = requests.post('https://www.baidu.com', headers=headers) print(r.status_code) print(r.text)
運行下試試看。一般post都是用來提交表單信息的,嗯,這里找一個能提交數據的url,去post下。
用我自己寫的接口(PS:django寫的,挺方便),大家復制過去就好了。注意看代碼,data是要post的數據,post方法里加了一個data參數。
import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} # post的數據 data = {"info": "biu~~~ send post request"} r = requests.post('http://dev.kdlapi.com/testproxy', headers=headers, data=data) #加一個data參數 print(r.status_code) print(r.text)
截個圖給大家看下,http code 200,body信息說的post成功,并且返回的了我自己的IP信息以及post的數據
使用代理
為什么用代理?一般網站都有屏蔽的限制策略,用自己的IP去爬,被封了那該網站就訪問不了,這時候就得用代理IP來解決問題了。封吧,反正封的不是本機IP,封的代理IP。
既然使用代理,得先找一個代理IP。PS:自己寫個代理服務器太麻煩了,關鍵是我也不會寫啊,,,哈哈哈
import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} # post的數據 data = {"info": "biu~~~ send post request"} # 代理信息,由快代理贊助 proxy = '115.203.28.25:16584' proxies = { "http": "http://%(proxy)s/" % {'proxy': proxy}, "https": "http://%(proxy)s/" % {'proxy': proxy} } r = requests.post('http://dev.kdlapi.com/testproxy', headers=headers, data=data, proxies=proxies) #加一個proxies參數 print(r.status_code) print(r.text)
主要方法里加個proxies參數,這就用上代理IP了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
利用python讀取YUV文件 轉RGB 8bit/10bit通用
今天小編就為大家分享一篇利用python讀取YUV文件 轉RGB 8bit/10bit通用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python?tkinter中l(wèi)abel控件動態(tài)改變值問題
這篇文章主要介紹了Python?tkinter中l(wèi)abel控件動態(tài)改變值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Matplotlib實現subplot和subplots簡單對比
在畫布創(chuàng)建子圖會有很多方法,本文主要介紹了Matplotlib實現subplot和subplots簡單對比,簡單的介紹了這兩種方法區(qū)別,感興趣的可以了解一下2021-05-05