【发布时间】: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