Untitled

mail@pastecode.io avatarunknown
python
21 days ago
2.7 kB
4
Indexable
Never
import asyncio
import base64
from playwright.async_api import async_playwright

board = "https://openbudget.uz/boards/initiatives/initiative/31/57f53c33-3b2b-4360-a076-e5fc7503a687"


async def get_captcha():
    apw = await async_playwright().start()
    browser = await apw.chromium.launch(headless=False)
    page = await browser.new_page()
    await page.goto(board)
    button = await page.query_selector('//button[@class="mf-button mf-button-block mf-button-primary mb-2"]')
    await button.click()
    image = await page.query_selector('img[class="captcha-img"]')
    captcha = await image.get_attribute('src')
    image = base64.b64decode(captcha.split(" ")[1], validate=True)
    file_to_save = "get_all_votes_captcha.png"
    with open(file_to_save, "wb") as f:
        f.write(image)
    n = input("Captcha code... ")
    input_element = await page.query_selector(f'//input[@class="captcha-result"]')

    await input_element.fill(n)
    button = await page.query_selector('//button[@class="mf-button mf-button-block mf-button-primary"]')
    await button.click()
    await asyncio.sleep(3)
    all_rows = await page.query_selector_all('//li[@class="page-item"]')
    end_row = int(await all_rows[-2].text_content())
    next_btn = all_rows[-1]
    table = await page.query_selector('//tbody[@class="mf-tbody"]')
    content = (await table.text_content()).split("2023")
    times = []
    numbers = []
    print("OK! Please wait.")
    numbers.append(content[0].split("**-***-*")[1])
    for i in content[1:-1]:
        piec = i.split("**-***-*")
        numbers.append(piec[-1])
        times.append(piec[0][1:12])
    times.append(content[-1][1:])
    if end_row != 1:
        for i in range(end_row-1):
            await next_btn.click()
            await asyncio.sleep(1)
            while True:
                try:
                    table = await page.query_selector('//tbody[@class="mf-tbody"]')
                    content = (await table.text_content()).split("2023")
                    numbers.append(content[0].split("**-***-*")[1])
                    for i in content[1:-1]:
                        piec = i.split("**-***-*")
                        numbers.append(piec[-1])
                        times.append(piec[0][1:12])
                    times.append(content[-1][1:])
                    break
                except:
                    await asyncio.sleep(3)
                    pass

    print(times)
    print(numbers)

    input("Press any button close page... ")
    await page.screenshot(path="screen1.png")
    await page.close()


asyncio.run(get_captcha())