Python requests 安裝與開發

mimvp發表於2019-02-16

Requests 是用Python語言編寫HTTP客戶端庫,跟urllib、urllib2類似,基於 urllib,但比 urllib 更加方便,可以節約我們大量的工作,完全滿足 HTTP 測試需求,編寫爬蟲和測試伺服器響應資料時經常會用到。
Requests 的哲學是以 PEP 20 的習語為中心開發的,所以它比 urllib 更加 Pythoner,更重要的一點是它支援 Python3 
Beautiful is better than ugly. (美麗優於醜陋)
Explicit is better than implicit. (清楚優於含糊)
Simple is better than complex. (簡單優於複雜)
Complex is better than complicated. (複雜優於繁瑣)
Readability counts. (重要的是可讀性)

 
Requests 官網
https://pypi.python.org/pypi/requests
 
安裝 Requests
方式1)pip 安裝
pip install requests      # python2.7pip3 install requests    # python3.6
 
方式2)原始碼安裝
下載 requests-2.18.2.tar.gz
解壓安裝:
tar zxvf requests-2.18.2.tar.gzcd requestspython setup.py install
驗證安裝:

$ python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests
<module `requests` from `/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/__init__.pyc`>

 
使用 Requests
requests 封裝了Python的urlib和urllib2,所以爬取網頁非常簡潔
1. 爬取網頁

import requests
 
# 爬取米撲科技首頁
req = requests.get(url = `http://mimvp.com`)
print("status_code : " + str(req.status_code))
print("mimvp text : " + req.text)
 
# 爬取米撲代理(含請求引數)
req = requests.get(url=`http://proxy.mimvp.com/free.php`, params={`proxy`:`out_tp`,`sort`:`p_ping`})   
print("status_code : " + str(req.status_code))
print("mimvp text : " + req.text)

爬取網頁非常簡潔吧,Python urllib 三行程式碼,requests只需要一行程式碼搞定
urllib :  urllib.urlopen(`http://mimvp.com`).read()
urllib2: urllib2.urlopen(`http://mimvp.com`).read()
或者

import urllib2
req = urllib2.Request(`http://mimvp.com`)
res = urllib2.urlopen(req)
page = res.read()

 
requests 介面格式:

requests.get(`https://mimvp.com/timeline.json`)    # GET請求
requests.post(`http://mimvp.com/post`)            # POST請求
requests.put(`http://mimvp.com/put`)              # PUT請求
requests.delete(`http://mimvp.com/delete`)        # DELETE請求
requests.head(`http://mimvp.com/get`)             # HEAD請求
requests.options(`http://mimvp.com/get`)          # OPTIONS請求

 
requests 介面示例:

import requests
requests.get(`http://mimvp.com`, params={`love`: `mimvp`})    # GET引數例項
requests.post(`http://mimvp.com`, data={`love`: `mimvp`})     # POST引數例項

 
Requests 設定代理

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Python requests 支援 http、https、socks4、socks5
#
# 米撲代理示例:
# http://proxy.mimvp.com/demo2.php
# 
# 米撲代理購買:
# http://proxy.mimvp.com
# 
# mimvp.com
# 2016-09-16
 
 
import requests
import ssl
import socks, socket    # 需要引入socks.py檔案,請到米撲代理下載
 
 
mimvp_url = "http://proxy.mimvp.com/exist.php"
mimvp_url2 = "https://proxy.mimvp.com/exist.php"
mimvp_url3 = "https://apps.bdimg.com/libs/jquery-i18n/1.1.1/jquery.i18n.min.js"
             
             
# 使用代理 http, https
proxies = { 
            "http"  : "http://120.77.155.249:8888", 
            "https" : "http://54.255.211.38:80", 
           }   
   
req = requests.get(mimvp_url2, proxies=proxies, timeout=30, verify=False) 
print("mimvp text : " + req.text)
 
 
 
# 使用代理 socks4
proxies = { 
            `socks4` : `163.121.188.2:4000`,
           }   
  
socks4_ip = proxies[`socks4`].split(":")[0]
socks4_port = int(proxies[`socks4`].split(":")[1])
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, socks4_ip, socks4_port)
socket.socket = socks.socksocket
  
req = requests.get(mimvp_url2, timeout=30, verify=False) 
print("mimvp text : " + req.text)
 
 
 
# 使用代理 socks5
proxies = { 
            `socks5` : `190.9.58.211:45454`,
           }   
   
socks5_ip = proxies[`socks5`].split(":")[0]
socks5_port = int(proxies[`socks5`].split(":")[1])
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, socks5_ip, socks5_port)
socket.socket = socks.socksocket
   
req = requests.get(mimvp_url2, timeout=30, verify=False) 
print("mimvp text : " + req.text)

 
本示例採用的米撲代理,支援 http、https、socks4、socks5等多種協議,覆蓋全球120多個國家,中國34個省市
推薦米撲代理: http://proxy.mimvp.com
 
 
參考推薦:
Python pyspider 安裝與開發
Python3 urllib 用法詳解
PhantomJS 安裝與開發
Node.js 安裝與開發
Node.js SuperAgent 安裝與開發
 

相關文章