Класс Async:
Код: Выделить всё
class FreeProxyList(Proxy):
def __init__(self, testEach=False):
self.testEach = testEach
self.proxies = []
self.session = None
async def setup(self):
async with aiohttp.ClientSession() as session:
async with session.get("https://free-proxy-list.net/") as response:
html = await response.text()
souped = BeautifulSoup(html, "html.parser")
table = souped.find_all("table")[0]
headers = [header.text for header in table.find_all("th")]
proxies_list = []
for row in table.find_all("tr")[1:]:
columns = row.find_all("td")
if columns:
proxy_info = {headers[i]: columns[i].text for i in range(len(headers))}
proxies_list.append(proxy_info)
# Filter proxies
proxies_list = list(filter(lambda x: x['Https'] == "yes", proxies_list))
# proxies_list = list(filter(lambda x: x['Code'] in ["US", "CA", "GB", "AU", "NZ"], proxies_list))
if self.testEach:
self.proxies = []
tasks = [self.testOne(session, proxy, index, len(proxies_list)) for index, proxy in enumerate(proxies_list)]
results = await asyncio.gather(*tasks)
self.proxies = [proxy for proxy, result in zip(proxies_list, results) if result]
async def testOne(self, session, proxy, index, total):
import time
start = time.time()
try:
print(f"{index + 1} / {total :
Подробнее здесь: [url]https://stackoverflow.com/questions/79010776/asyncio-proxy-checking[/url]