Download scraper.py from Archy-Welcomer/Proxy-Scraper: direct link, hf CLI and curl.
- Browser
- Download file 7.5 kB
-
https://huggingface.co/spaces/Archy-Welcomer/Proxy-Scraper/resolve/main/scraper.py
- Command line
-
hf download hf://spaces/Archy-Welcomer/Proxy-Scraper/scraper.py
-
curl -L -o scraper.py https://huggingface.co/spaces/Archy-Welcomer/Proxy-Scraper/resolve/main/scraper.py
7.5 kB
| import re | |
| import json | |
| import time | |
| import logging | |
| import ipaddress | |
| from bs4 import BeautifulSoup | |
| from curl_cffi import requests as curl_requests | |
| import config | |
| logger = logging.getLogger('scraper') | |
| # Universal IP:PORT matcher | |
| _IPPORT_RE = re.compile( | |
| r'\b(?:\d{1,3}\.){3}\d{1,3}:\d{2,5}\b' | |
| ) | |
| # Dynamic extraction keywords | |
| _IP_KEYS = { | |
| 'ip', 'host', 'address', 'addr', | |
| 'proxy_address', 'proxy_host', | |
| 'hostname' | |
| } | |
| _PORT_KEYS = { | |
| 'port', 'proxy_port', | |
| 'port_number' | |
| } | |
| _PROTO_KEYS = { | |
| 'protocol', 'protocols', | |
| 'type', 'proxy_type', | |
| 'schema', 'kind', | |
| 'proxy_scheme' | |
| } | |
| # Protocol aliases | |
| _PROTOCOL_ALIASES = { | |
| 'socks5': 'socks5', | |
| 'socks5h': 'socks5', | |
| 's5': 'socks5', | |
| } | |
| _HEADERS = { | |
| 'User-Agent': ( | |
| 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' | |
| 'AppleWebKit/537.36 (KHTML, like Gecko) ' | |
| 'Chrome/124.0.0.0 Safari/537.36' | |
| ), | |
| 'Accept': '*/*', | |
| } | |
| def _valid_ip(ip: str) -> bool: | |
| try: | |
| ipaddress.ip_address(ip) | |
| return True | |
| except Exception: | |
| return False | |
| def _valid_port(port) -> bool: | |
| try: | |
| port = int(port) | |
| return 1 <= port <= 65535 | |
| except Exception: | |
| return False | |
| def _normalize_protocol(value) -> str | None: | |
| if value is None: | |
| return None | |
| if isinstance(value, list): | |
| for item in value: | |
| proto = _normalize_protocol(item) | |
| if proto == 'socks5': | |
| return 'socks5' | |
| return None | |
| value = str(value).strip().lower() | |
| for alias, normalized in _PROTOCOL_ALIASES.items(): | |
| if alias in value: | |
| return normalized | |
| return None | |
| def _extract_ip_port(text: str) -> list[str]: | |
| found = [] | |
| for match in _IPPORT_RE.findall(text): | |
| try: | |
| ip, port = match.split(':', 1) | |
| if not _valid_ip(ip): | |
| continue | |
| if not _valid_port(port): | |
| continue | |
| found.append(match) | |
| except Exception: | |
| continue | |
| return found | |
| def _extract_from_dict( | |
| item: dict, | |
| fallback_protocol: str, | |
| found: dict | |
| ) -> None: | |
| ip = None | |
| port = None | |
| protocol = None | |
| for key, value in item.items(): | |
| lowered = str(key).lower() | |
| if lowered in _IP_KEYS: | |
| ip = value | |
| elif lowered in _PORT_KEYS: | |
| port = value | |
| elif lowered in _PROTO_KEYS: | |
| protocol = _normalize_protocol(value) | |
| if ip and port: | |
| ip = str(ip).strip() | |
| port = str(port).strip() | |
| if _valid_ip(ip) and _valid_port(port): | |
| proxy = f'{ip}:{port}' | |
| found[proxy] = protocol or fallback_protocol | |
| def _walk(data, fallback_protocol: str, found: dict) -> None: | |
| """ | |
| Universal recursive parser. | |
| Handles: | |
| - nested JSON | |
| - lists | |
| - dicts | |
| - strings | |
| - embedded JSON strings | |
| """ | |
| if isinstance(data, dict): | |
| _extract_from_dict(data, fallback_protocol, found) | |
| for value in data.values(): | |
| _walk(value, fallback_protocol, found) | |
| elif isinstance(data, list): | |
| for item in data: | |
| _walk(item, fallback_protocol, found) | |
| elif isinstance(data, tuple): | |
| for item in data: | |
| _walk(item, fallback_protocol, found) | |
| elif isinstance(data, str): | |
| for proxy in _extract_ip_port(data): | |
| found[proxy] = fallback_protocol | |
| # Embedded JSON support | |
| stripped = data.strip() | |
| if ( | |
| stripped.startswith('{') or | |
| stripped.startswith('[') | |
| ): | |
| try: | |
| parsed = json.loads(stripped) | |
| _walk(parsed, fallback_protocol, found) | |
| except Exception: | |
| pass | |
| def _extract_from_html( | |
| html: str, | |
| fallback_protocol: str | |
| ) -> dict: | |
| """ | |
| Universal HTML parser. | |
| Supports: | |
| - tables | |
| - div layouts | |
| - inline text | |
| - script tags | |
| """ | |
| found = {} | |
| soup = BeautifulSoup(html, 'html.parser') | |
| # Global page text scan | |
| page_text = soup.get_text(' ', strip=True) | |
| for proxy in _extract_ip_port(page_text): | |
| found[proxy] = fallback_protocol | |
| # Script extraction | |
| for script in soup.find_all('script'): | |
| content = script.get_text(' ', strip=True) | |
| if not content: | |
| continue | |
| for proxy in _extract_ip_port(content): | |
| found[proxy] = fallback_protocol | |
| try: | |
| parsed = json.loads(content) | |
| _walk(parsed, fallback_protocol, found) | |
| except Exception: | |
| pass | |
| return found | |
| def add_candidate( | |
| proxy_str: str, | |
| source: str, | |
| protocol: str | |
| ) -> bool: | |
| with config.store_lock: | |
| if proxy_str in config.store['blacklist']: | |
| return False | |
| if proxy_str in config.store['active_proxies']: | |
| return False | |
| if proxy_str in config.store['candidate_proxies']: | |
| return False | |
| config.store['candidate_proxies'][proxy_str] = { | |
| 'proxy': proxy_str, | |
| 'source': source, | |
| 'protocol': protocol, | |
| 'found_at': time.time(), | |
| } | |
| return True | |
| def scraper_loop() -> None: | |
| logger.info( | |
| '[scraper] started with %d source(s)', | |
| len(config.PROXY_SOURCES) | |
| ) | |
| while True: | |
| total_seen = 0 | |
| total_added = 0 | |
| for url, fallback_protocol in config.PROXY_SOURCES: | |
| try: | |
| response = curl_requests.get( | |
| url, | |
| timeout=20, | |
| headers=_HEADERS, | |
| impersonate='chrome124', | |
| allow_redirects=True, | |
| ) | |
| text = response.text | |
| found = {} | |
| # Fast universal scan | |
| for proxy in _extract_ip_port(text): | |
| found[proxy] = fallback_protocol | |
| # Deep parsing only when needed | |
| if len(found) < 10: | |
| # JSON parsing | |
| try: | |
| parsed = json.loads(text) | |
| _walk(parsed, fallback_protocol, found) | |
| except Exception: | |
| pass | |
| # HTML parsing | |
| if '<html' in text.lower(): | |
| found.update( | |
| _extract_from_html( | |
| text, | |
| fallback_protocol | |
| ) | |
| ) | |
| source = ( | |
| url.split('//')[-1] | |
| .split('/')[0] | |
| .split('?')[0] | |
| ) | |
| for proxy, protocol in found.items(): | |
| # SOCKS5 ONLY | |
| if protocol != 'socks5': | |
| continue | |
| total_seen += 1 | |
| if add_candidate( | |
| proxy, | |
| source, | |
| protocol | |
| ): | |
| total_added += 1 | |
| except Exception as e: | |
| logger.error( | |
| '[scraper] failed [%s]: %s', | |
| url, | |
| e | |
| ) | |
| logger.info( | |
| '[scraper] cycle complete | seen=%d | added=%d', | |
| total_seen, | |
| total_added | |
| ) | |
| time.sleep(600) | |