前幾天看了一下github上,IPProxys開源項目(https://github.com/qiyeboy/IPProxys)快100star了,看來你們對這個項目仍是比較感興趣的。最近一直沒更新文章,主要是忙實驗室的工做和寫一個以前給你們提到新的開源項目,我將它命名爲PowerProxy,寫的過程當中遇到了不少問題,算是一個不錯的學習經歷,對sock5協議,windows內核有了必定的理解。開源的日期還沒肯定,須要將一些關鍵問題解決,你們敬請期待。html
看到你們對IPProxys項目挺感興趣,下面就介紹一下它的使用方式。(個人新書《Python爬蟲開發與項目實戰》出版了,你們能夠看一下樣章)python
安裝sqlite數據庫: apt-get install sqlite sqlite3 git
安裝requests庫: pip install requests github
安裝lxml: apt-get install python-lxml sql
下載sqlite,路徑添加到環境變量 數據庫
安裝requests庫: pip install requests json
安裝lxml: pip install lxml或者下載lxml windows版ubuntu
將項目目錄clone到當前文件夾windows
$ git clone服務器
切換工程目錄
$ cd IPProxys
運行腳本
python IPProxys.py
模式
GET /
參數
IPProxys默認端口爲8000
若是是在本機上測試:
獲取5個ip地址在中國的高匿代理:http://127.0.0.1:8000/?types=0&count=5&country=中國
響應爲JSON格式,按照響應速度由高到低,返回數據:
[{"ip": "220.160.22.115", "port": 80}, {"ip": "183.129.151.130", "port": 80}, {"ip": "59.52.243.88", "port": 80}, {"ip": "112.228.35.24", "port": 8888}, {"ip": "106.75.176.4", "port": 80}]
示例代碼:
import requests import json r = requests.get('http://127.0.0.1:8000/?types=0&count=5&country=中國') ip_ports = json.loads(r.text) print ip_ports ip = ip_ports[0]['ip'] port = ip_ports[0]['port'] proxies={ 'http':'http://%s:%s'%(ip,port), 'https':'http://%s:%s'%(ip,port) } r = requests.get('http://ip.chinaz.com/',proxies=proxies) r.encoding='utf-8' print r.text
TODO
可自主選擇添加squid反向代理服務器,簡化爬蟲配置
重構HTTP API接口
增長更多代理網站和數據庫適配