【问题标题】:discord.py BS4 can't get info out of href elementdiscord.py BS4 无法从 href 元素中获取信息
【发布时间】:2021-02-03 16:14:13
【问题描述】:

我有以下 HTML:

<a href="//site.com/user/123/Username1"><img height="56px" src="//s.site.com/user/123/avatar" width="56px"/>Username</a>

如何在&lt;/a&gt; 标签前获得“Username1”?

【问题讨论】:

    标签: python html beautifulsoup discord discord.py


    【解决方案1】:

    你可以使用.text属性:

    from bs4 import BeautifulSoup
    
    html_doc = '''<a href="//site.com/user/123/Username1"><img height="56px" src="//s.site.com/user/123/avatar" width="56px"/>Username</a>'''
    soup = BeautifulSoup(html_doc, 'html.parser')
    
    print(soup.a.text)
    

    打印:

    Username
    

    如果要从href="..."属性中解析用户名:

    print(soup.a['href'].split('/')[-1])
    

    打印:

    Username1
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多