Proxies with Python 'Requests' module

后端 未结 10 820
傲寒
傲寒 2020-11-22 12:13

Just a short, simple one about the excellent Requests module for Python.

I can\'t seem to find in the documentation what the variable \'proxies\' should contain. Whe

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 12:19

    I share some code how to fetch proxies from the site "https://free-proxy-list.net" and store data to a file compatible with tools like "Elite Proxy Switcher"(format IP:PORT):

    ##PROXY_UPDATER - get free proxies from https://free-proxy-list.net/

    from lxml.html import fromstring
    import requests
    from itertools import cycle
    import traceback
    import re
    
    ######################FIND PROXIES#########################################
    def get_proxies():
        url = 'https://free-proxy-list.net/'
        response = requests.get(url)
        parser = fromstring(response.text)
        proxies = set()
        for i in parser.xpath('//tbody/tr')[:299]:   #299 proxies max
            proxy = ":".join([i.xpath('.//td[1]/text()') 
            [0],i.xpath('.//td[2]/text()')[0]])
            proxies.add(proxy)
        return proxies
    
    
    
    ######################write to file in format   IP:PORT######################
    try:
        proxies = get_proxies()
        f=open('proxy_list.txt','w')
        for proxy in proxies:
            f.write(proxy+'\n')
        f.close()
        print ("DONE")
    except:
        print ("MAJOR ERROR")
    

提交回复
热议问题