Python3+Selenium,需要在<td>内获得值,即<b>和<script>内

2024-09-21 09:42:48 发布

您现在位置:Python中文网/ 问答频道 /正文

正如标题所说,我试图捕获这个td标记中的值999.999.999。已尝试以下操作:

  • 你知道吗td.文本你知道吗
  • 你知道吗td值你知道吗
  • 你知道吗脚本.值你知道吗
  • b、 价值
  • 执行脚本(“return sayMonto”)
  • 执行脚本(“return sayMonto;”)
  • 执行脚本(“return sayMonto()”)
  • 执行_脚本(“return sayMonto();”)
  • 执行脚本(“sayMonto”)
  • 执行脚本(“sayMonto()”)
  • 执行_脚本(“sayMonto();”)。你知道吗

但没有雪茄。这里你有一个html的狙击手,出于安全的原因,这是唯一的我想我可以给你看。每天这个数字都在变化,需要把它和其他人一起复制粘贴到同一个网站上。这纯粹是疯了,所以请帮我一把。有其他网站的自动机器人,但这个已经证明是很难拉,至少对我来说。你知道吗

<tr>
    <td style="boder-width:1px">
        <b>Irrelevant_text_that_i_capture_but_i_really_don´t</b>
       <!--&nbsp; 999.999.999-->
        <script language="javascript">
            sayMonto('999.999.999');
        </script>
       999.999.999
    </td>
   <!-- <td style="border-width:1px">&nbsp;&nbsp;999.999.999</td> -->
</tr>

PS:这是正在抛出的错误

selenium.common.exceptions.WebDriverException: Message: cTexto is undefined

我从未使用过这个变量,所以它一定是来自脚本。你知道吗


Tags: 标记文本脚本标题return网站stylescript
1条回答
网友
1楼 · 发布于 2024-09-21 09:42:48

我不知道有什么方法可以严格使用硒。你必须使用另一个库,也许BeautifulSoup。你知道吗

例如:

# use selenium to get the html
html = driver.page_source

# pass it to beautifulsoup
td = soup.find("td")
# extract the elements you don't want
b = td.find("b")
b.extract()
sc = td.find("script")
sc.extract()
# print just the number
print td.text.strip()

相关问题 更多 >

    热门问题