我如何与 python 剧作家一起阅读跨度 class 的内容

How do I read the content of a span class with python playwright

我想从网页上读取乐谱。分数位于:

<span _ngcontent-fnp-c148="" class="score"> &nbsp;<!--bindings={
"ng-reflect-ng-if": "false"
}--> 96%, </span>

此处结果为“96%”

我想直接搜索类名“score”,或者如果有必要将整个页面 html 转储到一个字符串中并手动搜索。 两种方法都可以。

我用的是python剧作家,但我想方法应该和原来的差不多。

您可以使用 Page.innerText(".score") 方法来完成此操作,请参见此处的示例:

// @ts-check
const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.setContent(`
    <span _ngcontent-fnp-c148="" class="score"> &nbsp;<!--bindings={
"ng-reflect-ng-if": "false"
}--> 96%, </span>
`);
  const content = await page.innerText(".score")
  console.log(content)
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();

或在此处交互:https://try.playwright.tech/?s=bl1fed4

对于 Python 用法,请参见此处:https://microsoft.github.io/playwright-python/sync_api.html#playwright.sync_api.Page.innerText