python 代理选择器,代理文件列表中的验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 代理选择器,代理文件列表中的验证相关的知识,希望对你有一定的参考价值。

"""
Proxy selector with validation from list of proxies file

Input:
- path_proxy: path of list of proxies input file
- url: url to be used for the validation
Output:
- proxy_url: url of the selected proxy
- proxy_port: port of the selected proxy

List of proxies:
http://multiproxy.org/txt_all/proxy.txt
http://www.nntime.com/proxy-list-01.htm

"""
def proxy_selector(path_proxy,url = "https://google.com"):
    # read proxies
    INFILE = open(path_input, "r")
    lproxies = [proxy.strip() for proxy in INFILE]

    # validation of random proxie
    isok = False
    while isok==False:
        # random selection
        import random
        proxy_url, proxy_port = random.choice(lproxies).split(':')
        # validate proxy
        import urllib.request as web
        try:
            proxy_handler = web.ProxyHandler({'http':proxy_url+':'+proxy_port})
            new_opener = web.build_opener(proxy_handler)
            page = new_opener.open(url)
            isok = True
            print('Successfull PROXY connection: %s'%proxy_handler.__dict__['proxies']['http'])
        except IOError as e:
            print('ERROR on PROXY connection (%s:%s) --> %s'%(proxy_url, proxy_port,str(e)))

    # close list proxies files        
    INFILE.close()   
    return (proxy_url, proxy_port)

以上是关于python 代理选择器,代理文件列表中的验证的主要内容,如果未能解决你的问题,请参考以下文章

如何通过代理解决选择器选择?

python--IP代理池验证可用性

python 代理ip爬取,ip代理,数据库存储,去重,验证。

Python Selenium Webdriver - 代理身份验证

Python笔记 · 函数装饰器(Decorators)

python获取ip代理列表爬虫