你将如何从资料资料中提取“2021-12-30”?
<td class="select_date" data-dat="2021-12-30" data-dat-f="30. Dec 2021" data-global-index="32" data-title="30.12.2021" id="d30-12-2021"><span class="bubble">30</span></td>
uj5u.com热心网友回复:
你可以试试attrs
:
>>> soup.find("td").attrs['data-dat']
2021-12-30
完整示例:
# import module
from bs4 import BeautifulSoup
# create data
dom = """<td data-dat="2021-12-30" data-dat-f="30. Dec 2021" data-global-index="32" data-title="30.12.2021" id="d30-12-2021"><span >30</span></td>"""
soup = BeautifulSoup(dom, 'html.parser')
# Extract "data-dat" attribute
print(soup.find("td").attrs['data-dat'])
# 2021-12-30
0 评论