我正在尝试用BeautifulSoup抓取页面,<span>
标签内有<script>
标签,如下所示
<span data-link="{include tmpl='productCardOrderCount' ^~ordersCount=selectedNomenclature^ordersCount}"><script type="jsv#28_"></script>
<script type="jsv#27^"></script>
<script type="jsv#29_"></script>
<script type="jsv#26^"></script>
более 20 раз
<script type="jsv/26^"></script>
<script type="jsv/29_"></script>
<script type="jsv/27^"></script>
<script type="jsv/28_"></script>
</span>
但是由于<script>
标记在bs4中不被解析为HTML,下面的代码返回没有文本("более 20 раз"
)的<span>
标记
rating = soup.find("p", {"class": "order-quantity"})
我怎么能得到<span>
标签内的文本?
文本位于<script type="jsv#26^">
标签下。您可以使用soup.find("script", type="jsv#26^")
搜索它。
from bs4 import BeautifulSoup
html = """
<span data-link="{include tmpl='productCardOrderCount' ^~ordersCount=selectedNomenclature^ordersCount}"><script type="jsv#28_"></script>
<script type="jsv#27^"></script>
<script type="jsv#29_"></script>
<script type="jsv#26^"></script>
более 20 раз
<script type="jsv/26^"></script>
<script type="jsv/29_"></script>
<script type="jsv/27^"></script>
<script type="jsv/28_"></script>
</span>
"""
soup = BeautifulSoup(html, "html.parser")
print(soup.find("script", type="jsv#26^").find_next(text=True).strip())
输出:
более 20 раз