Python 使用BeautifulSoup从文本中删除标记

Python 使用BeautifulSoup从文本中删除标记,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我有以下代码从NightBot的频道页面提取歌曲名称: import urllib.request from bs4 import BeautifulSoup from selenium import webdriver driver = webdriver.Firefox(executable_path=r'C:\Users\gabri\AppData\Local\Programs\Python\Python38-32\geckodriver.exe') driver.get ('https

我有以下代码从NightBot的频道页面提取歌曲名称:

import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\Users\gabri\AppData\Local\Programs\Python\Python38-32\geckodriver.exe')
driver.get ('https://nightbot.tv/t/tonyxzero/song_requests')

html = driver.page_source

soup = BeautifulSoup(html, 'html.parser')
list_item=soup.select("h4 > strong.ng-binding")
print (list_item)
name = list_item.text.strip()
print (name)
但当我运行它时,会显示如下内容:

[<strong class="ng-binding">Jamiroquai - Virtual Insanity (Official Video)<!-- ngIf: currentSong.track.artist --><span class="ng-binding ng-scope" ng-if="currentSong.track.artist" style=""> — JamiroquaiVEVO</span><!-- end ngIf: currentSong.track.artist --></strong>]
[Jamiroquai-虚拟精神错乱(官方视频)-JamiroquaiVEVO]
他们这样说:

AttributeError:ResultSet对象没有属性“text”。您可能将元素列表视为单个元素。当您打算调用find()时,是否调用了find_all()?

还有另一种方法可以只显示没有标记的文本?

soup.select()
将元素列表返回给元素,而不是元素。要获取每个元素值,需要迭代

import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\Users\gabri\AppData\Local\Programs\Python\Python38-32\geckodriver.exe')
driver.get ('https://nightbot.tv/t/tonyxzero/song_requests')

html = driver.page_source

soup = BeautifulSoup(html, 'lxml')
name=soup.find('strong',{'class':'ng-binding'}).text
#print (list_item)
#name = list_item.text.strip()
print (name)
list_item=soup.select("h4 > strong.ng-binding")
print (list_item)
for item in list_item:
  name = item.text.strip()
  print (name)
soup.select()
返回元素列表而不是元素。要获取每个元素值,需要迭代

list_item=soup.select("h4 > strong.ng-binding")
print (list_item)
for item in list_item:
  name = item.text.strip()
  print (name)

共享soup variableexecute my code snippet的输出,并告诉我它是否有效,以防失败共享错误详细信息列表项不是单个元素,并且没有属性“text”。name=list\u项[0]。text.strip()共享soup变量的输出执行我的代码段,如果它有效,请告诉我,以防失败共享错误详细信息list\u项不是单个元素,并且没有属性“text”。name=list_item[0].text.strip()不确定要使用哪个标记,请尝试在代码中更改标记查看是否获得所需的标记不确定要使用哪个标记,请尝试在代码中更改标记查看是否获得所需的标记