使用beautifulsoup提取<strong>标签值

问题描述

我想从下面的HTML代码提取strong和sup标签的值:

class SimpleviewmodelSolution : viewmodel() {
    private var _name = mutablelivedata("Ada")     // I modified from private val _name = mutablelivedata("Ada")
   
    val name: LiveData<String> = _name
  
     ...

    fun onLike() {
        _likes.value = (_likes.value ?: 0) + 1
        _name = mutablelivedata("My new")  // I added
    }
}

enter image description here

解决方法

尝试:

output = []
for soup in data3:
    output.append(soup.find("strong"))
    output.append(soup.find("sup"))

运行此命令后,output列表具有strong的所有元素中的前supdata3个元素。

如果要在标记内添加文本,可以在.content调用后使用.get_text()find。像soup.find("strong").get_text()

有关美丽汤的更多帮助,请参见https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find

,
soup3=BeautifulSoup(html,'html.parser')
spans=soup3.findAll('strong')
spans=soup3.findAll('sup')