抓取烂番茄没有返回数据
No data returned from scraping rottentomatoes
我不知道如何从 https://www.rottentomatoes.com/browse/in-theaters/
中抓取以下数据
电影名称
评分
发布日期
Link 到电影详情
Link 到电影海报
我没有取回任何数据或进入我的每个循环。
Rotten Tomatoes screenshot
我的代码:
var cheerio = require("cheerio");
var request = require("request");
// Make a request call to grab the HTML body from the site of your
choice
request("https://www.rottentomatoes.com/browse/in-theaters/",
function(error, response, html) {
// Load the HTML into cheerio and save it to a variable
// '$' becomes a shorthand for cheerio's selector commands, much like
jQuery's '$'
var $ = cheerio.load(html);
// An empty array to save the data that we'll scrape
var results = [];
// Select each element in the HTML body from which you want
information.
// NOTE: Cheerio selectors function similarly to jQuery's selectors,
// but be sure to visit the package's npm page to see how it works
$('mb-movie').each(function(i, element) {
console.log("inside each");
console.log($(element));
var link = $(element).children().attr("href");
var title = $(element).find('h3').text();
// // Save these results in an object that we'll push into the results array we defined earlier
results.push({
title: title,
link: link
});
});
// Log the results once you've looped through each of the elements
found with cheerio
console.log(results);
});
经过一些调查,我发现 rottentomatoes 实际上会在休息时加载电影列表 api :
https://www.rottentomatoes.com/api/private/v2.0/search/default-list
这应该可以提供您需要的数据,而无需通过无头浏览器或任何东西!
您想要的数据在 cheerio 只能看到的页面的 RAW HTML 中不可用。如果您只是将 URL 加载到浏览器然后执行 View/Source 并查看原始 HTML.
,您可以自己看到这一点
如果您查看那个 HTML,您会看到一个空白部分:
<section class="mb-movies" style="opacity:1">
</section>
您要查找的数据显然是通过 Javascript 添加到页面的,这是 cheerio 无法看到的。要在通过 Javascript 添加的页面中查看数据,您必须使用无头浏览器引擎,例如 PhantomJS。或者,您可以在 Web 上找到提供 API 的服务,以更直接地检索您想要的数据。
此外,您的选择器应该是 $('.mb-movie')
,但数据不存在,因此如果没有页面中的 运行 和 Javascript 将无法工作,因此您可以访问页面中动态添加的数据。
首先,您的选择器不正确,因为它缺少 class 名称的点前缀,即 $(".mb-movie")。
但即使您更正了选择器,它仍然不会匹配任何内容,因为电影是在页面加载后使用 JS 动态呈现在页面上的。您可以通过在浏览器的页面上执行 "view source" 并搜索选择器 mb-movie
来测试它 - 您将找不到任何东西。 mb-movie
元素由作为页面的一部分执行的 media-browser.js 动态添加。请求不是网络浏览器,它只能下载原始 HTML.
RT 以前有一个API,现在好像没有了。 fandango 有一个,但我怀疑你会从中得到你需要的东西。
另一种选择是使用网站自动化库,例如 Selenium or PhantomJS which run a headless browser to actually load the page and are programmable. See this part of the docs for more info: http://phantomjs.org/page-automation.html#dom-manipulation
我们不知道您这样做的目的是什么,但请注意,RT 的 terms of use 明确禁止以创建某种数据库为目的的检索。谢谢@DaveNewton
我不知道如何从 https://www.rottentomatoes.com/browse/in-theaters/
中抓取以下数据电影名称 评分 发布日期 Link 到电影详情 Link 到电影海报
我没有取回任何数据或进入我的每个循环。
Rotten Tomatoes screenshot
我的代码:
var cheerio = require("cheerio");
var request = require("request");
// Make a request call to grab the HTML body from the site of your
choice
request("https://www.rottentomatoes.com/browse/in-theaters/",
function(error, response, html) {
// Load the HTML into cheerio and save it to a variable
// '$' becomes a shorthand for cheerio's selector commands, much like
jQuery's '$'
var $ = cheerio.load(html);
// An empty array to save the data that we'll scrape
var results = [];
// Select each element in the HTML body from which you want
information.
// NOTE: Cheerio selectors function similarly to jQuery's selectors,
// but be sure to visit the package's npm page to see how it works
$('mb-movie').each(function(i, element) {
console.log("inside each");
console.log($(element));
var link = $(element).children().attr("href");
var title = $(element).find('h3').text();
// // Save these results in an object that we'll push into the results array we defined earlier
results.push({
title: title,
link: link
});
});
// Log the results once you've looped through each of the elements
found with cheerio
console.log(results);
});
经过一些调查,我发现 rottentomatoes 实际上会在休息时加载电影列表 api :
https://www.rottentomatoes.com/api/private/v2.0/search/default-list
这应该可以提供您需要的数据,而无需通过无头浏览器或任何东西!
您想要的数据在 cheerio 只能看到的页面的 RAW HTML 中不可用。如果您只是将 URL 加载到浏览器然后执行 View/Source 并查看原始 HTML.
,您可以自己看到这一点如果您查看那个 HTML,您会看到一个空白部分:
<section class="mb-movies" style="opacity:1">
</section>
您要查找的数据显然是通过 Javascript 添加到页面的,这是 cheerio 无法看到的。要在通过 Javascript 添加的页面中查看数据,您必须使用无头浏览器引擎,例如 PhantomJS。或者,您可以在 Web 上找到提供 API 的服务,以更直接地检索您想要的数据。
此外,您的选择器应该是 $('.mb-movie')
,但数据不存在,因此如果没有页面中的 运行 和 Javascript 将无法工作,因此您可以访问页面中动态添加的数据。
首先,您的选择器不正确,因为它缺少 class 名称的点前缀,即 $(".mb-movie")。
但即使您更正了选择器,它仍然不会匹配任何内容,因为电影是在页面加载后使用 JS 动态呈现在页面上的。您可以通过在浏览器的页面上执行 "view source" 并搜索选择器 mb-movie
来测试它 - 您将找不到任何东西。 mb-movie
元素由作为页面的一部分执行的 media-browser.js 动态添加。请求不是网络浏览器,它只能下载原始 HTML.
RT 以前有一个API,现在好像没有了。 fandango 有一个,但我怀疑你会从中得到你需要的东西。
另一种选择是使用网站自动化库,例如 Selenium or PhantomJS which run a headless browser to actually load the page and are programmable. See this part of the docs for more info: http://phantomjs.org/page-automation.html#dom-manipulation
我们不知道您这样做的目的是什么,但请注意,RT 的 terms of use 明确禁止以创建某种数据库为目的的检索。谢谢@DaveNewton