在用node爬取網易雲音樂信息時候 在不少地方是不如python簡單 方便
可是下面的這個案例會報錯
再讀文檔時候 源碼有些地方出錯 要改源碼 (不建議更改源碼)
const fs = require("fs")
const cheerio = require("cheerio")
const Nightmare = require('nightmare')
const nightmare = Nightmare({ show: true, openDevTools: { mode: 'detach' } })
let home_url = "https://music.163.com/#/discover/artist/cat?id=1002"
nightmare
.goto(home_url)
.click(".m-cvrlst li:nth-child(2) a", () => { // 這個地方 要想不報錯 只能從源碼更改
console.log("點擊完了......")
setTimeout(() => {
console.log("新界面....")
nightmare.goto("https://music.163.com/#/artist?id=8103")
.evaluate(() => {
return document.querySelector(".g-iframe").contentWindow.document.querySelector("body").innerHTML
})
.then((data) => {
console.log("開始查詢.,,....")
let $ = cheerio.load(data)
let songs = []
$(".m-table tr .txt a").each((i, item) => {
let song_id = $(item).attr("href")
let song_name = $(item).find("b").attr("title")
let tmp_dic = {
"song_id": song_id,
"song_name": song_name
}
songs.push(tmp_dic)
})
let songs_string = JSON.stringify(songs)
let file_name = "./test" + $(".sname-max").text() + ".json"
fs.writeFile(file_name, songs_string, () => {
})
})
}, 2000);
})
.then(() => {
})
//------------------------------------------------------
//下面是源碼
從源碼作了一些更改
xports.click = function(selector, done) {
debug('.click() on ' + selector)
this.evaluate_now(
function(selector) {
document.activeElement.blur()
// var element = document.querySelector(selector)
var element = ""
if(document.querySelector(".g-iframe").contentWindow.document) {
element = document.querySelector(".g-iframe").contentWindow.document.querySelector(selector)
}else{
var element = document.querySelector(selector)
}
if (!element) {
throw new Error('Unable to find element by selector: ' + selector)
}