【问题标题】:get the text from certain attribute using Beautifulsoup使用 Beautifulsoup 从某个属性中获取文本
【发布时间】:2021-02-05 13:27:05
【问题描述】:

我试过这段代码

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

结果是

[<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>]

我想获取属性“aria-label”中的文本。怎么能这样? 我不能使用“查找”。

如果我使用“选择”,我想知道这个答案。

谢谢。

【问题讨论】:

  • 可以分享网址吗?

标签: python beautifulsoup web-crawler


【解决方案1】:

只需遍历列表并打印所有span 标签的aria-label 属性,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2021-05-21
    • 2013-09-14
    • 2021-09-09
    • 1970-01-01
    • 2020-02-21
    • 2018-01-24
    相关资源
    最近更新 更多