90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
import asyncio
|
|
from random import randint, choice
|
|
from playwright.async_api import async_playwright
|
|
|
|
BASE_URL = "https://rule34.xxx"
|
|
URL = f"{BASE_URL}/index.php?page=post&s=list"
|
|
MAXIMUM = 999
|
|
|
|
# Хранилище тегов в памяти
|
|
TAGS = set()
|
|
|
|
def add_tags(tags: list[str]):
|
|
TAGS.update(tags)
|
|
|
|
def del_tags(tags: list[str]):
|
|
for t in tags:
|
|
TAGS.discard(t)
|
|
|
|
def get_tags_str() -> str:
|
|
return "+".join(TAGS) if TAGS else "(нет тегов)"
|
|
|
|
def get_tags() -> str:
|
|
return "+".join(TAGS) if TAGS else ""
|
|
|
|
|
|
async def get_url():
|
|
async with async_playwright() as p:
|
|
browser = await p.firefox.launch(headless=True)
|
|
page = await browser.new_page(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
|
|
|
|
while True:
|
|
try:
|
|
tags = get_tags()
|
|
pid = randint(1, MAXIMUM)
|
|
|
|
# Формируем корректный URL
|
|
if tags:
|
|
url_page = f"{URL}&tags={tags}&pid={pid}"
|
|
else:
|
|
url_page = f"{URL}&pid={pid}"
|
|
|
|
await page.goto(url_page, timeout=5000)
|
|
|
|
# Ищем блок с картинками
|
|
block = await page.query_selector(".image-list")
|
|
if not block:
|
|
continue
|
|
|
|
spans = await block.query_selector_all("span")
|
|
if not spans:
|
|
continue
|
|
|
|
link_el = await choice(spans).query_selector("a")
|
|
if not link_el:
|
|
continue
|
|
|
|
href = await link_el.get_attribute("href")
|
|
if not href:
|
|
continue
|
|
|
|
await page.goto(f"{BASE_URL}{href}", timeout=30000)
|
|
|
|
flexi = await page.query_selector(".flexi")
|
|
if not flexi:
|
|
continue
|
|
|
|
img_el = await flexi.query_selector("img")
|
|
if not img_el:
|
|
continue
|
|
|
|
url = await img_el.get_attribute("src")
|
|
if not url:
|
|
continue
|
|
|
|
await browser.close()
|
|
return url
|
|
|
|
except Exception as e:
|
|
print(f"[get_url ERROR] {e}")
|
|
continue
|
|
|
|
|
|
# Пример использования
|
|
async def main():
|
|
result = await get_url()
|
|
print("Result URL:", result)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|