简体   繁体   English

使用 Beautifulsoup 从特定属性中获取文本

[英]get the text from certain attribute using Beautifulsoup

I tried this code我试过这个代码

reaction=soup.find_all('span',class_='1n9k')

and the result is结果是

[<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>,<span class="_1n9k" data-hover="tooltip" tabindex="-1"><a ajaxify="/ufi/reaction/profile/dialog/?ft_ent_identifier=ZmVlZGJhY2s6MzgxNjk0Mjk2NTQwMzcy&amp;reaction_type=1&amp;av=0" aria-label="좋아요 55" class="_1n9l" href="/ufi/reaction/profile/browser/?ft_ent_identifier=ZmVlZGJhY2s6MzgxNjk0Mjk2NTQwMzcy&amp;av=0" rel="dialog" role="button" tabindex="0"><i class="sp_KIvjPBBBAwk sx_eaca68" role="img"></i></a></span>,<span class="_1n9k" data-hover="tooltip" tabindex="-1"><a ajaxify="/ufi/reaction/profile/dialog/ft_ent_identifier=ZmVlZGJhY2s6MzgxNjk0Mjk2NTQwMzcy&amp;reaction_type=2&amp;av=0" aria-label="최고예요 3" class="_1n9l"href="/ufi/reaction/profile/browser/ft_ent_identifier=ZmVlZGJhY2s6MzgxNjk0Mjk2NTQwMzcy&amp;av=0" rel="dialog" role="button" tabindex="-1"><i class="sp_KIvjPBBBAwk sx_b917e0" role="img"></i></a></span>]

I want to get the text in the attribute 'aria-label'.我想获取属性“aria-label”中的文本。 How could do this ?怎么会这样? I can't use 'find'.我不能使用“查找”。

And I want to know this answer if i use 'select'.如果我使用“选择”,我想知道这个答案。

Thanks.谢谢。

Just loop through the list and print the aria-label attribute of all span tags, like this:只需遍历列表并打印所有span标签的aria-label属性,如下所示:

for span in reaction:
    print(span.a['aria-label'])

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

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