soup.find("div", {"class":"real number"})['data-value']
在这里您正在寻找一个div
元素,但是span
您的示例 HTML 数据中有“实数”类,请尝试:
soup.find("span", {"class": "real number", "data-value": True})['data-value']
在这里我们还检查是否存在data-value
属性。
要查找具有“实数”或“假数”类的元素,您可以制作CSS 选择器 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors:
for elm in soup.select(".real.number,.fake.number"):
print(elm.get("data-value"))
为了得到69%
value:
soup.find("div", {"class": "percentage good"}).get_text(strip=True)
或者,CSS 选择器:
soup.select_one(".percentage.good").get_text(strip=True)
soup.select_one(".score .percentage").get_text(strip=True)
或者,找到h6
元素具有Audit score
文本,然后获取前一个兄弟姐妹 http://www.crummy.com/software/BeautifulSoup/bs4/doc/#next-sibling-and-previous-sibling:
soup.find("h6", text="Audit score").previous_sibling.get_text(strip=True)