python 爬虫 检测代理ip是否有效

import requests
import pandas
from lxml import etree
ip_table=pandas.read_excel('ip.xlsx')
ip = ip_table['ip']
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

for i in ip:
    proxies = {'http':'http://{ip}'.format(ip=i),'https':'http://{ip}'.format(ip=i)}
    try:
        response = requests.get('http://202020.ip138.com/',headers=headers,proxies=proxies,timeout=2)
        if response.status_code==200:
            response.encoding='utf-8'
            html = etree.HTML(response.text)
            info = html.xpath('/html/body/p[1]//text()')
            print(info)
    except Exception as e:
        print('错误异常信息为:',e)

版权声明:本文为dai1987csdn原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。