Python Beautifulsoup 获取属性值 [英] Python Beautifulsoup Getting Attribute Value
本文介绍了Python Beautifulsoup 获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 HTML 5.0 的 Beautifulsoup 中,我很难获得正确的语法来提取属性的值.
I'm having difficulty getting the proper syntax to extract the value of an attribute in Beautifulsoup with HTML 5.0.
所以我使用正确的语法在我的 soup
中隔离了标签的出现,其中存在 HTML 5 问题:
So I've isolated the occurrence of a tag in my soup
using the proper syntax where there is an HTML 5 issue:
tags = soup.find_all(attrs={"data-topic":"recUpgrade"})
仅使用标签[1]:
date = tags[1].find(attrs={"data-datenews":True})
这里的日期是:
<span class="invisible" data-datenews="2018-05-25 06:02:19" data-idnews="2736625" id="horaCompleta"></span>
但现在我想提取日期时间2018-05-25 06:02:19".无法获取语法.
But now I want to extract the date time "2018-05-25 06:02:19". Can't get the syntax.
请提供见解/帮助.
推荐答案
您可以使用键值对访问属性
You can access the attrs using key-value pair
例如:
from bs4 import BeautifulSoup
s = """<span class="invisible" data-datenews="2018-05-25 06:02:19" data-idnews="2736625" id="horaCompleta"></span>"""
soup = BeautifulSoup(s, "html.parser")
print(soup.span["data-datenews"])
输出:
2018-05-25 06:02:19
这篇关于Python Beautifulsoup 获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文