验证西刺代理ip有效性

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  8 14:50:04 2017


@author: markcheung


 check ip validity
"""
import requests
import pandas as pd


def check(proxy):
    try:
        requests.get('http://httpbin.org/ip', proxies=proxy)
    except:
        return "error"
    else:
        return "right"


if __name__ == "__main__":
    ips = pd.read_csv('***********', encoding="GBK")
    ip_port=[]    
    for i in range(len(ips)):
        str1 = "http://"+str(ips['ip'][i])+":"+str(ips['links_port'][i])
        str2 = "https://"+str(ips['ip'][i])+":"+str(ips['links_port'][i])
        proxy = {'http': str1,'https': str2}
        result = check(proxy)
        if(result=="right"):
            ip_port.append(str(ips['ip'][i])+":"+str(ips['links_port'][i]))
            print(str(ips['ip'][i])+":"+str(ips['links_port'][i]))
    pd.DataFrame(ip_port).to_csv("C:\\Users\\markcheung\\Desktop\\valid_ips_data.csv",index=False)
#60.169.78.218:808
#114.235.80.182:8118
#221.0.235.246:8118
#210.34.212.109:808

你可能感兴趣的:(spider,python)