西祠代理——检测代理使用

Posted gxsmm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了西祠代理——检测代理使用相关的知识,希望对你有一定的参考价值。

import requests
from lxml import etree



url = ‘http://www.xicidaili.com/‘

headers = {
    ‘user-agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/68.0.3440.106 Safari/537.36‘
}
response = requests.get(url, headers=headers)

# with open(‘xicidaili.html‘, ‘wb‘) as f:
#     f.write(response.content)

html_ele = etree.HTML(response.text)

tr_list = html_ele.xpath(‘//table[@id="ip_list"]/tr‘)
# print(tr_list)
country_td = []
for tr_ele in tr_list:
    # print(tr_ele)
    country_list = tr_ele.xpath(‘./td[2]/text()‘)
    country_list1 = tr_ele.xpath(‘./td[3]/text()‘)
    # country = country_list + country_list1
    # print(country)
    # print(type(country_list))
    # print(country_list)
    if country_list == []:
        continue
    elif country_list1 == []:
        continue
    else:
        ip = country_list[0]
        ip_d = country_list1[0]
        # print(ip)
        # print(ip_d)
    #     ip_list = country_list
        # print(ip_list)
        ip_1 = ip + ‘:‘ + ip_d
        # print(ip_1)
        # print(type(ip_1))
        # http_t = ‘http://‘ + ip_1

        proxies = ip_1


        url = ‘http://www.baidu.com‘
        # response = requests.get(url, headers=headers, proxies=proxies)
        normal_proxies = []
        count= 1

        print("第%s个。。" % count)
        count += 1
        try:
            response = requests.get(url, headers=headers, proxies={"http": proxies}, timeout=1)
            if response.status_code == 200:
                print("该代理IP可用:", proxies)
                normal_proxies.append(proxies)
            else:
                print("该代理IP不可用:", proxies)
        except Exception:
            print("该代理IP无效:", proxies)
            pass

  

以上是关于西祠代理——检测代理使用的主要内容,如果未能解决你的问题,请参考以下文章

C# 自动检测代理设置

检测 IE 代理设置并与 TIdHTTP 一起使用

Forge Viewer - 如何在场景中访问(或获取渲染/片段代理)克隆的网格?

使用redis+flask维护动态代理池

使用 JavaScript 检测 iPad 应用内浏览器用户代理

python使用redis实现ip代理池