在寫到解析歌單的時候發現幾個問題
擷取歌曲名字和id的xpath寫法明明和爬取熱榜的寫法一樣

id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
for id in id_list:
href=id.xpath('./@href')[0]
song_id=href.split('=')[1]
songid.append(song_id)
song_name=id.xpath('./text()')[0]
songname.append(song_name)
但無法擷取明文資訊
研究了下發現,歌單中的資訊被放在了iframe裡.
通過查詢後發現可以通過安裝selenium實作.如果僅僅作為爬蟲工具來使用則很簡單,沒什麼入門成本.
from selenium import webdriver
wd = webdriver.Chrome(r'd:\chromedriver.exe')
wd.get('https://music.163.com/#/playlist?id=6702371651')
wd.switch_to.frame(0)
elements =wd.find_elements_by_xpath('//a[contains(@href,"/song?id")]')
name=[]
for i in elements:
print(i.get_attribute('href'))
elements2 =wd.find_elements_by_xpath('//b[@title]')
for i in elements2:
print(i.get_attribute('title'))
擷取成功
受限于未登入狀态隻能看到部分資訊,隻能看到十條