【问题标题】:How to get span title using Beautifulsoup如何使用 Beautifulsoup 获取跨度标题
【发布时间】:2021-11-13 04:26:05
【问题描述】:

我有这个跨度:

<span class='title'>Model:</span><span class='val'>Galaxy S20 5G</span><br>

我想要“Galaxy S20 5G” 我该怎么办?

我已经选择了内容,但是我无法打印出来。

我尝试过这样的事情:

results = soup.find_all('span', attrs={'val'})
for pr in results:
    print('Name:' +pr.find('span', attrs ={'class':'val'}))

【问题讨论】:

    标签: python html beautifulsoup spyder


    【解决方案1】:
    from bs4 import BeautifulSoup
    spam = "<span class='title'>Model:</span><span class='val'>Galaxy S20 5G</span><br>"
    
    soup = BeautifulSoup(spam, 'html.parser')
    print(soup.find('span', {'class':'val'}).string) 
    

    输出

    Galaxy S20 5G
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 2019-03-09
      相关资源
      最近更新 更多