简体   繁体   English

Cheerio无法正常工作。 我究竟做错了什么?

[英]Cheerio not working. What am I doing wrong?

I am trying to scrape a classified ad search result page. 我正在尝试抓取分类广告搜索结果页。

I have tried console logging everything I can to make sure I am getting a response, which I am, but when I actually use cheerio to query something I don't get anything back. 我已经尝试过使用控制台记录所有内容,以确保得到响应,但是实际上当我使用cheerio查询某些内容时,我什么也得不到。 For instance if I just query for how many children using $('.listing-group').children('section').length I get back 0 instead of 24 when I console log it. 例如,如果我仅使用$('.listing-group').children('section').length查询多少个子级,则在我进行控制台登录时,它会返回0而不是24

Here is what I'm doing. 这是我在做什么。 Pardon the long URL. 请原谅长URL。

const request = require("request");
const cheerio = require("cheerio");
const app = express();

app.get("/scrape", function(req, res) {
  url =
    "http://classifieds.ksl.com/search/?keyword=code&category%5B%5D=Books+and+Media&zip=&miles=25&priceFrom=&priceTo=&city=&state=&sort=";

  request(url, function(error, response, html) {
    if (!error) {
      let $ = cheerio.load(html);

      let test = $("#search-results").find("section").length

      console.log(test);

    } else {

      console.log("there has been an error");

    }

    res.send("Check the console.");

  });
});

app.listen("8081");
console.log("check localhost:8081/scrape");
exports = module.exports = app;

I'm new to cheerio so I'm assuming I'm probably making a simple error, but with all the tutorials I've checked, I can't seem to find an answer. 我是cheerio的新手,所以我假设我可能正在犯一个简单的错误,但是在检查了所有教程之后,我似乎找不到答案。

Ultimately I want to grab each result on the page (found in the 'section' tags) to display the data for a personal project I'm working on. 最终,我想获取页面上的每个结果(在'section'标签中找到)以显示我正在处理的个人项目的数据。

看起来像:

JSON.parse(html.match(/listings: (\[.*\])/)[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异步/等待示例不起作用。 我究竟做错了什么 - async/await example not working. What am I doing wrong 我对数组的推送不起作用。 我究竟做错了什么? - My push to a array is not working. What am i doing wrong? 我正在尝试使用 MutationObserver 检测特定元素的更改,但它不起作用。 我究竟做错了什么? - I am trying to detect changes on a particular element using the MutationObserver but it is not working. What am I doing wrong? 我无法使用任何JSLink。 我究竟做错了什么 - I am not able to get any of my JSLink working. What am I doing wrong JavaScript,Php,AJAX,MySql网站上的Upvote按钮不起作用。 我究竟做错了什么? - Upvote button on website with JavaScript, Php, AJAX, MySql not working. What am I doing wrong? 我的JavaScript无法加载外部CSS文件。 我究竟做错了什么? - my javascript for loading external css files isn't working. what am i doing wrong? ngRoute不工作。 有什么我做错了 - ngRoute not working. is there something i am doing wrong 我做错了什么? - What i am doing wrong? 带有 OR 语句的 IF 公式未按预期工作 - 我做错了什么? - IF formula with OR statement not working as expected - what am I doing wrong? 这个jsonp应该很容易工作,我在做什么错? - This jsonp should be working so easily, what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM