【问题标题】:Websites using scrapy-playwright and only playwright work differently使用 scrapy-playwright 和 only playwright 的网站工作方式不同
【发布时间】:2022-11-22 09:20:57
【问题描述】:

我正在尝试使用 scrapy-playwright 登录网页,因为我希望与 scrapy 很好地集成。我无法使用 scrapy-playwright 登录,因为它会重定向到一个不存在的页面。我也尝试过发帖请求而不是点击,这也不起作用。

但是,如果我只使用 Playwright 尝试同样的事情,它会完美地工作......与仅使用 Playwright 相比,使用 scrapy-playwright 打开的网站有区别吗?有谁知道如何使用 scrapy-playwright 解决这个问题?

scrapy-剧作家代码:

def start_requests(self):
    yield scrapy.Request(
        url = self.url,
        meta = dict(
            playwright = True,
            playwright_include_page = True,
            playwright_page_methods = [PageMethod('wait_for_selector', 'a[data-toggle=dropdown]')],
                ),
        callback = self.sign_in,
        )

async def sign_in(self, response):
    page = response.meta['playwright_page']
    while await page.is_visible("button[class='close close-news']"):
        await page.click("button[class='close close-news']")
    await page.click('button#declineAllConsentSummary')
    await page.click('div.my-account-sub > a[data-toggle=dropdown]', timeout=10000)
    await page.fill('input#j_username_header', os.getenv(self.usernameKey), timeout=10000)
    await page.fill('input#j_password_header', os.getenv(self.passwordKey), timeout=10000)
    await page.click('button#responsiveMyAccLoginGA')

编剧代码:

async def test_async_playwright(self):
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=False)
        context = await browser.new_context(base_url=self.url)
        page = await context.new_page()
        
        await page.goto(self.url, wait_until='commit')
        while await page.is_visible("button[class='close close-news']"):
            await page.click("button[class='close close-news']")
        await page.click('button#declineAllConsentSummary')
        await page.wait_for_selector('a[data-toggle=dropdown]')
        await page.click('div.my-account-sub > a[data-toggle=dropdown]', timeout=5000)
        await page.fill('input#j_username_header', os.getenv(self.usernameKey), timeout=5000)
        await page.fill('input#j_password_header', os.getenv(self.passwordKey), timeout=5000)
        await page.click('button#responsiveMyAccLoginGA')

【问题讨论】:

  • 使用“错误”代码时出现的错误是什么?

标签: python web-scraping scrapy playwright


【解决方案1】:

作为一种可能的解决方法,如果在授予令牌/cookie 后您被重定向(到损坏的页面),您也可以导航到正常的站点 url,并且您应该登录

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 2023-02-17
    • 2022-07-12
    • 2021-09-22
    • 1970-01-01
    • 2023-03-21
    • 2021-08-28
    • 2022-08-18
    • 2020-07-09
    相关资源
    最近更新 更多