1. 程式人生 > >python學習之二 代理ip有效性驗證

python學習之二 代理ip有效性驗證

python學習之二 代理ip有效性驗證

驗證程式碼Demo

# -*- coding: utf-8 -*-
import requests
def gitIp():
    ipList=[]
    with open("../../proxies.txt", "r") as f:
        ipList=f.read().split("\n")
        # print ipList
        # print "----------------------"
    return ipList


ipList=['https://39.88.13.3:53281'
, 'https://182.43.203.43:4306', 'https://183.62.71.242:3128', 'https://61.160.208.222:8080', 'https://123.179.28.159:8080', 'https://58.56.90.202:53281', 'https://222.88.144.119:53281', 'https://61.158.111.142:53281', 'https://139.224.24.26:8888', 'https://124.232.148.7:3128'
, 'https://218.15.23.229:53281', 'https://222.34.139.52:808', 'https://218.18.10.11:9797', 'https://119.136.113.49:9797'] def valVer(proxys): badNum = 0 goodNum = 0 for proxy in proxys: try: proxy_host = proxy protocol = 'https' if 'https'
in proxy_host else 'http' proxies = {protocol: proxy_host} response = requests.get('http://www.baidu.com', proxies=proxies, timeout=2) if response.status_code != 200: badNum += 1 print proxy_host, 'bad proxy' else: goodNum += 1 print proxy_host, 'success proxy' except Exception, e: print e # print proxy_host, 'bad proxy' badNum += 1 continue print 'success proxy num : ', goodNum print 'bad proxy num : ', badNum # valVer(gitIp()) #讀取本地檔案裡的IP valVer(ipList)