首页 > 解决方案 > 使用汤 python 在 html 页面中查找 aria-label

问题描述

我有 html 页面,使用以下代码:

<span itemprop="title" data-andiallelmwithtext="15" aria-current="page" aria-label="you in page number 452">page 452</span>

我想找到 aria-label,所以我试过这个:

is_452 = soup.find("span", {"aria-label": "you in page number 452"})
print(is_452)

我想得到结果:

is_452 =page 452

我得到了结果:

is_452=none

怎么做 ?

标签: python-3.xseleniumbeautifulsoupfind

解决方案


它有换行符,因此它与文本不匹配。尝试以下

from simplified_scrapy.simplified_doc import SimplifiedDoc
html='''<span itemprop="title" data-andiallelmwithtext="15" aria-current="page" aria-label="you in page
number 452">page 452</span>'''
doc = SimplifiedDoc(html)
is_452 = doc.getElementByReg('aria-label="you in page[\s]*number 452"',tag="span")
print (is_452.text)

推荐阅读