代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://music.163.com/#/discover/toplist%22)
driver.switch_to.frame("contentFrame")
li = driver.find_elements(by=By.TAG_NAME, value="tr")
for t in li:
list1 = t.find_elements(by=By.TAG_NAME, value='span')
for s in list1:
if s.get_attribute("class") == "ico u-icn u-icn-73 s-fc9":
#(问题如下)
# songn = t.find_element_by_tag_name("b").get_attribute("title")#报错:对象没有属性值
# songn = t.find_elements(by=By.TAG_NAME, value="b").get_attribute("title") #如果改成这样又不行了
songn = t.find_elements(by=By.TAG_NAME, value="b")#.getattribute("title")
# singer = t.find_element_by_class_name("text").get_attribute("title")
singer = t.find_elements(by=By.CLASS_NAME, value="text")#.__getattribute__("title")
print(f"歌曲:《{songn}》 歌手:{singer}")