代碼如下:
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}")