首页 > 解决方案 > 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)
    }
  }
)

脚本标签

希望获得帮助以访问此对象...

标签: web-scrapingautomationcheerio

解决方案


只需对整个响应使用正则表达式:

json = html.match(/window.appConfig = (\{.*\})/)[1]

推荐阅读