Building a proxy rotator with specific URL and scr

2019-08-10 15:06发布

问题:

I am struggling to build a proxy rotator with existing code structured for a different url.

The URLs I want are provided in the code example below. I am trying to have the provided script call the desired URLs and get ALL the 'IP:PORT' (current script limits to ten) when proxy type is "HTTPS".
It can be done in xpath or bs4. I am familair with bs4 more though.

I understand the logic but I am failing on how to structure this. To start, I've tried stripping strings and trying to call specific td elements but its not working.

#URLs I want 
url_list = ['http://spys.one/free-proxy-list/US/','http://spys.one/free-proxy-list/US/1/']

#code I have 
 from lxml.html import fromstring
 import requests
 from itertools import cycle
 import traceback

 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')[:10]:
     if i.xpath('.//td[7][contains(text(),"yes")]'):
        proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
        proxies.add(proxy)
return proxies

proxies = get_proxies()
proxy_pool = cycle(proxies)
proxy = next(proxy_pool)
response = requests.get(url,proxies={"http": proxy, "https": proxy})

I hope to learn how the code provided is structured for the 2 desired URLs, return all IP:PORT numbers when proxy type is HTTPS

回答1:

One way is to issue port specific POST requests in a loop. You could amend to add to one final list. The endpoint is already https specific.

import requests
from bs4 import BeautifulSoup as bs

def get_proxies(number, port, p):
    r = requests.post('http://spys.one/en/https-ssl-proxy/', data = {'xpp': 5, 'xf4': number})
    proxies = [':'.join([str(i),port]) for i in p.findall(r.text)]
    return proxies

ports = ['3128', '8080', '80']
p = re.compile(r'spy14>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<script')
proxies = []

for number, port in enumerate(ports,1):
    proxies+=get_proxies(number, port, p)

print(proxies)

Example results:


For country specific:

import requests
from bs4 import BeautifulSoup as bs

def get_proxies(number, port, p, country):
    r = requests.post('http://spys.one/en/https-ssl-proxy/',  data = {'xpp': 5, 'xf4': number})
    soup = bs(r.content, 'lxml')
    proxies = [':'.join([p.findall(i.text)[0], port]) for i in soup.select('table table tr:has(.spy14:contains("' + country + '")) td:has(script) .spy14')]
    return proxies

ports = ['3128', '8080', '80']
p = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})document')
proxies = []

for number, port in enumerate(ports,1):
    proxies+=get_proxies(number, port, p, 'United States')

print(proxies)

For the one you said is already written I will refer to my original answer of:

from bs4 import BeautifulSoup as bs
import requests

def get_proxies(): 
    r = requests.get('https://free-proxy-list.net/')
    soup = bs(r.content, 'lxml')
    proxies = {tr.td.text + ':' + tr.td.next_sibling.text for tr in soup.select('tr:has(.hx:contains(yes))')} 
    return proxies 

get_proxies()