简体   繁体   English

标题关键词搜索

[英]keyword search on title

The current code provides me with the data based on id of the API. My idea is to create a search based on keyword for it.当前代码为我提供了基于 API id 的数据。我的想法是为其创建一个基于关键字的搜索。

The idea is when you search for 'coding', it should log the data/posts with the keyword as a title.这个想法是,当您搜索“编码”时,它应该以关键字作为标题记录数据/帖子。

function script(id) {
  request(`https://hacker-news.firebaseio.com/v0/item/${id}.json`, function (error, res, body) {
    if (error) {
      console.log(error);
    } else {
      let myData = (JSON.parse(body))
      var titleUrl = `https://news.ycombinator.com/item?id=${myData.id}`;
      request(titleUrl, function (err, res, body) {
          console.log(myData.title);
          console.log(myData.score)
          console.log(titleUrl)
      });
    }
  });
}

script(23202120);

The second request does not return json instead it returns a html-page.第二个请求不返回json ,而是返回一个 html 页面。 You can use a html-parser like https://github.com/cheeriojs/cheerio to actually read the contents of the page.您可以使用像https://github.com/cheeriojs/cheerio这样的 html 解析器来实际读取页面的内容。

...
 request(titleUrl, function (err, res, body) {
          const $ = cheerio.load(body);
          const title = $('title');
          console.log(title);
          ...

      });
...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM