- Fix SQL injection risks in proxy_repo and task_repo - Atomic acquire_pending with UPDATE ... RETURNING - Reuse aiohttp ClientSession in ValidatorService - Replace polling with asyncio.Event in SchedulerService - Optimize ValidationQueue.drain with asyncio.Condition - Concurrent plugin crawling with asyncio.gather - Unify ProxyRaw model import path - Fix test baseline and remove tracked __pycache__ files
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import re
|
|
from typing import List
|
|
from bs4 import BeautifulSoup
|
|
from app.core.plugin_system import ProxyRaw
|
|
from app.plugins.base import BaseHTTPPlugin
|
|
from app.core.log import logger
|
|
|
|
VALID_PROTOCOLS = ("http", "https", "socks4", "socks5")
|
|
|
|
|
|
class YunDaiLiPlugin(BaseHTTPPlugin):
|
|
default_config = {"max_pages": 5}
|
|
name = "yundaili"
|
|
display_name = "云代理"
|
|
description = "从云代理网站爬取免费代理"
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.urls = [
|
|
f"http://www.ip3366.net/free/?stype=1&page={i}" for i in range(1, 6)
|
|
] + [
|
|
f"http://www.ip3366.net/free/?stype=2&page={i}" for i in range(1, 6)
|
|
]
|
|
|
|
async def crawl(self) -> List[ProxyRaw]:
|
|
results = []
|
|
for url in self.urls:
|
|
html = await self.fetch(url, timeout=15)
|
|
if not html:
|
|
continue
|
|
soup = BeautifulSoup(html, "lxml")
|
|
list_table = soup.find("div", id="list")
|
|
if not list_table:
|
|
continue
|
|
table = list_table.find("table")
|
|
if not table:
|
|
continue
|
|
|
|
for row in table.find_all("tr"):
|
|
tds = row.find_all("td")
|
|
if len(tds) >= 5:
|
|
ip = tds[0].get_text(strip=True)
|
|
port = tds[1].get_text(strip=True)
|
|
protocol = tds[4].get_text(strip=True).lower() if len(tds) > 4 else "http"
|
|
if protocol not in VALID_PROTOCOLS:
|
|
protocol = "http"
|
|
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit():
|
|
results.append(ProxyRaw(ip, int(port), protocol))
|
|
|
|
if results:
|
|
logger.info(f"{self.display_name} 解析完成,获取 {len(results)} 个潜在代理")
|
|
return results
|