Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用剧作家python处理多个页面?_Python_Web Scraping_Playwright_Playwright Python - Fatal编程技术网

如何使用剧作家python处理多个页面?

如何使用剧作家python处理多个页面?,python,web-scraping,playwright,playwright-python,Python,Web Scraping,Playwright,Playwright Python,如何收听新页面 在JavaScript中,它将被记录为: const playwright = require("playwright"); (async () => { const browser = await playwright.chromium.launch(); const context = await browser.newContext(); const page = await context.newPage(); context.

如何收听新页面

在JavaScript中,它将被记录为:

const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  context.on("page", async newPage => {
    console.log("newPage", await newPage.title())
  })

  // emulate some opening in a new tab or popup
  await page.evaluate(() => window.open('https://google.com', '_blank'))
  // Keep in mind to have some blocking action there so that the browser won't be closed. In this case we are just waiting 2 seconds.
  await page.waitForTimeout(2000)
  await browser.close();
})();
变成Python语言

from playwright import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False,
        executablePath='C:/Program Files/Google/Chrome/Application/chrome.exe'
    )
    context = browser.newContext()
    page = context.newPage()

'''
how to do in Python?
  context.on("page", async newPage => {
    console.log("newPage", await newPage.title())
  })

  // emulate some opening in a new tab or popup
  await page.evaluate(() => window.open('https://google.com', '_blank'))
'''

    page.waitForTimeout(2000)
    browser.close()
谢谢@hardkoded 以下是解决方案:

from playwright import sync_playwright

def newPage(page):
   print("newPage() page title:", page.title())

with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False,
        executablePath='C:/Program Files/Google/Chrome/Application/chrome.exe'
    )
    context = browser.newContext()
    page = context.newPage()

    context.on("page", lambda page: newPage(page))

    page.evaluate('''() => {
        window.open('https://google.com', '_blank')
    }''')
       
    page.waitForTimeout(2000)
    browser.close()

如果需要在没有事件侦听器的情况下处理新页面(即通过单击链接打开新选项卡),可以尝试以下代码:

    from playwright import sync_playwright

    with sync_playwright() as p:
        browser = p.chromium.launch()
        context = browser.newContext()
        page = context.newPage()
        page.goto('<site url>')
        with context.expect_page() as tab:
            page.click('.newTabByLink')
        # do some steps
        ...
        tab.close()
        
        browser.close()
从剧作家导入同步\u剧作家
将sync_playwright()作为p:
browser=p.chromium.launch()
context=browser.newContext()
page=context.newPage()
第页转到(“”)
使用context.expect_page()作为选项卡:
页面。单击(“.newtabylink”)
#做一些步骤
...
tab.close()
browser.close()

这有帮助吗?谢谢。下面是我的解决方案。