Answer a question

I've this code to extract a song name from NightBot's channel page:

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)

But when i run it, shows me something like this:

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

And them this:

AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

Theres another way to just show the text without the tags?

Answers

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)
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐