简体   繁体   English

使用 Beautifulsoup 从 aria-label 获取文本

[英]get text from aria-label using Beautifulsoup

<span class="_1n9k" data-hover="tooltip" tabindex="-1"><a ajaxify="/ufi/reaction/profile/dialog/? ft_ent_identifier=ZmVlZGJhY2s6MzgyODczMjYzMDg5MTQy&amp;reaction_type=1&amp;av=0" aria-label="좋아요 17" class="_1n9l" href="/ufi/reaction/profile/browser/?ft_ent_identifier=ZmVlZGJhY2s6MzgyODczMjYzMDg5MTQy&amp;av=0" rel="dialog" role="button" tabindex="0"><i class="sp_KIvjPBBBAwk sx_eaca68" role="img"></i></a></span>

How do I get text '좋아요 17' from 'aria-label'?如何从“aria-label”中获取文本“좋아요 17”? I tried using get('aria-label') but it isn't work我尝试使用 get('aria-label') 但它不起作用

'aria-label' is an attribute of the a tag that is within the span tag. 'aria-label'span标签内的a标签的一个attribute Here is how you extract the value of the attribute :以下是提取attribute值的方法:

from bs4 import BeautifulSoup

html = '<span class="_1n9k" data-hover="tooltip" tabindex="-1"><a ajaxify="/ufi/reaction/profile/dialog/? ft_ent_identifier=ZmVlZGJhY2s6MzgyODczMjYzMDg5MTQy&amp;reaction_type=1&amp;av=0" aria-label="좋아요 17" class="_1n9l" href="/ufi/reaction/profile/browser/?ft_ent_identifier=ZmVlZGJhY2s6MzgyODczMjYzMDg5MTQy&amp;av=0" rel="dialog" role="button" tabindex="0"><i class="sp_KIvjPBBBAwk sx_eaca68" role="img"></i></a></span>'

soup = BeautifulSoup(html,'html5lib')

span = soup.find('span', class_ = "_1n9k")

print(span.a['aria-label'])

Output:输出:

좋아요 17

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM