|
| 1 | +import asyncio |
| 2 | +import re |
| 3 | + |
| 4 | +from collectors import AbstractCollector |
| 5 | +from bs4 import BeautifulSoup |
| 6 | + |
| 7 | +import http_client |
| 8 | + |
| 9 | +SLEEP_BETWEEN_PAGES_SECONDS = 1 |
| 10 | + |
| 11 | + |
| 12 | +class Collector(AbstractCollector): |
| 13 | + __collector__ = True |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + super(Collector, self).__init__() |
| 17 | + # it provides really a lot of proxies so we'll check it rarely |
| 18 | + # 24 hours |
| 19 | + self.processing_period = 24 * 3600 |
| 20 | + self.url = "http://freeproxylists.com" |
| 21 | + |
| 22 | + async def collect(self): |
| 23 | + html = await http_client.get_text(self.url) |
| 24 | + soup = BeautifulSoup(html, features="lxml") |
| 25 | + |
| 26 | + for link in soup.select("a"): |
| 27 | + link = link["href"].strip() |
| 28 | + |
| 29 | + if re.match(r"^/[a-zA-Z0-9_-]+\.html$", link): |
| 30 | + async for proxy in self.collect_from_page(link): |
| 31 | + yield proxy |
| 32 | + |
| 33 | + async def collect_from_page(self, page_link): |
| 34 | + html = await http_client.get_text(self.url + page_link) |
| 35 | + |
| 36 | + soup = BeautifulSoup(html, features="lxml") |
| 37 | + |
| 38 | + for link in soup.select("a"): |
| 39 | + link = link["href"].strip() |
| 40 | + |
| 41 | + regex = r"^([a-zA-Z0-9_-]+)/([0-9]+)\.html$" |
| 42 | + match = re.match(regex, link) |
| 43 | + |
| 44 | + if match: |
| 45 | + type_of_proxies, proxies_id = match.groups() |
| 46 | + url = f"{self.url}/load_{type_of_proxies}_{proxies_id}.html" |
| 47 | + |
| 48 | + async for proxy in self.collect_from_table(url): |
| 49 | + yield proxy |
| 50 | + |
| 51 | + async def collect_from_table(self, table_url): |
| 52 | + html = await http_client.get_text(table_url) |
| 53 | + |
| 54 | + soup = BeautifulSoup(html, features="lxml") |
| 55 | + |
| 56 | + table_text = soup.find("quote").contents[0] |
| 57 | + soup = BeautifulSoup(table_text, features="lxml") |
| 58 | + |
| 59 | + for tr in soup.find_all("tr"): |
| 60 | + children = tr.find_all("td") |
| 61 | + if len(children) != 2: |
| 62 | + continue |
| 63 | + |
| 64 | + ip, port = [child.contents[0] for child in children] |
| 65 | + proxy = f"{ip}:{port}" |
| 66 | + yield proxy |
| 67 | + |
| 68 | + await asyncio.sleep(SLEEP_BETWEEN_PAGES_SECONDS) |
0 commit comments