簡體   English   中英

從網站抓取某些字段時無法繼續單擊下一頁按鈕

[英]Can't go on clicking on the next page button while scraping certain fields from a website

我已經使用 python 與pyppeteer結合創建了一個腳本,以繼續單擊下一頁按鈕,直到沒有更多按鈕為止。 單擊下一頁按鈕時腳本會拋出此錯誤pyppeteer.errors.TimeoutError: Navigation Timeout Exceeded: 30000 ms exceeded. 指向這一行await page.waitForNavigation() 不過,它可以從該站點的登錄頁面解析nameitem_type 我知道我可以使用適當的有效負載發出 post http 請求以從那里獲取數據,但我的目的是利用pyppeteer並在解析所需字段的同時繼續單擊下一頁按鈕。

網站地址

import asyncio
from pyppeteer import launch

link = "https://www.e-ports.com/ships"

async def get_content():
    wb = await launch(headless=True)
    [page] = await wb.pages()
    await page.goto(link)

    while True:
        await page.waitForSelector(".common_card", {'visible':True})

        elements = await page.querySelectorAll('.common_card')
        for element in elements:
            name = await element.querySelectorEval('span.title > a','e => e.innerText')
            item_type = await element.querySelectorEval('.bottom > span','e => e.innerText')
            print(name.strip(),item_type.strip())

        try:
            await page.click("button.btn-next")
            await page.waitForNavigation()
        except Exception: break

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(get_content())

順便說一句,如果我第一次手動點擊下一頁按鈕,它會成功完成其余的工作。

我不知道在Pypeteer有效語法,但常見的語法waitForNavigation也許這一個。

await Promise.all([
   page.waitForNavigation(),
   page.click("button.btn-next")
])

使用數組中的迭代器承諾,所有方法將在變為 true 或完成所需操作時解析。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM