python爬蟲——requests庫使用代理

在看這篇文章以前,須要你們掌握的知識技能:html

  • python基礎
  • html基礎
  • http狀態碼

 

讓咱們看看這篇文章中有哪些知識點:python

  • get方法
  • post方法
  • header參數,模擬用戶
  • data參數,提交數據
  • proxies參數,使用代理
  • 進階學習

安裝上requests庫

pip install requests

 

先來看下幫助文檔,看看requests的介紹,用python自帶的help命令django

import requests help(requests)

 

output:api

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吧。別百度了,本身動手,豐衣足食。教你們一個辦法,用谷歌或者火狐的開發者工具。post

谷歌瀏覽器的開發者工具

打開新標籤 —— 按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了。

 

進階學習

相關文章
相關標籤/搜索