Python Beautifulsoup获取属性值 [英] Python Beautifulsoup Getting Attribute Value

查看:813
本文介绍了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.

因此,我使用HTML 5问题的正确语法隔离了soup中标签的出现:

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.

请提供见解/帮助.

推荐答案

您可以使用键值对访问attrs

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆