web-scraping - Cheerio 在脚本标签 Node.js 中访问对象
问题描述
我试图在没有成功的情况下访问数据我尝试了所有我发现但没有成功的文档希望得到天才的帮助......
request(
"https://www.answers.com/Q/What_are_examples_of_prefixes_and_suffixes",
(error, response, html) => {
if (!error && response.statusCode == 200) {
let $ = cheerio.load(html)
// here what can i do to find the data for the <script>
console.log((str = $("script")[0].children[0].data))
console.log("Scraping Done...")
} else {
console.log("Scrapping Failed")
console.log(error)
}
}
)
希望获得帮助以访问此对象...
解决方案
只需对整个响应使用正则表达式:
json = html.match(/window.appConfig = (\{.*\})/)[1]
推荐阅读
- azure - Azure asp.net core 2.2 获得高 CPU 使用率
- java - 同步多个线程之间的状态
- spring-boot - Spring Boot & RabbitMQ:消息转换内容类型
- javascript - 将 React 添加到工作区失败,EACCES:权限被拒绝,mkdir '/home/mint/.cache/Cypress'
- foreign-keys - Javers - 外键约束因 mysql 而失败
- mysql - 将空白单元格更新为 NULL 会导致所有单元格为 NULL
- java - 如何解决 maven-formatter-plugin 的问题?
- python - 如何在部分文本中搜索字符串?
- php - 之间添加内容
- linux - shell 脚本已从带有回显输出的 edbplus sql 结果中检索