Javascript 在API中打开数组(木偶演员)
我正在尝试从API打开一个数组 尝试使用代码 const names_2=wait page.evaluate(()=>Array.from(document.querySelectorAll('.mainDiv>defaction'),defaction=>defaction.innerText)) 但是没有运气 这是我的意见Javascript 在API中打开数组(木偶演员),javascript,arrays,Javascript,Arrays,我正在尝试从API打开一个数组 尝试使用代码 const names_2=wait page.evaluate(()=>Array.from(document.querySelectorAll('.mainDiv>defaction'),defaction=>defaction.innerText)) 但是没有运气 这是我的意见 const puppeteer = require('puppeteer'); (async () => { const browser = await p
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('http://xmlopen.rejseplanen.dk/bin/rest.exe/multiDepartureBoard?id1=8600646&format=json')
const result = await page.evaluate(() => {
let temperature = document.getElementsByTagName("pre")[0].innerText;
temperature = JSON.parse(temperature);
return {
temperature
}
})
console.log(result)
})()
这是我的输出
{
temperature: {
MultiDepartureBoard: {
noNamespaceSchemaLocation: 'http://xmlopen.rejseplanen.dk/xml/rest/hafasRestMultiDepartureBoard.xsd',
Departure: [Array]
}
}
}
你在这里做的事毫无意义。只需请求数据即可
const rp = require('request-promise');
rp.get({
uri: 'http://xmlopen.rejseplanen.dk/bin/rest.exe/multiDepartureBoard?id1=8600646&format=json',
json: true
})
.then(res => res.MultiDepartureBoard.Departure)
.map(e => console.log(e))
;
为什么要访问返回JSON字符串的页面?你就不能直接得到它并解析它吗?我不知道怎么做,但我会调查的。刚刚尝试从url中删除“&format=json”,但没有成功。这正是我想要它做的!