无法从Cheerio的 Node spider获取信息(Unable to get information from Node spider with Cheerio)

我正在尝试从巴尔的摩市网站(监控状态项目)下载闭路电视位置的纬度/经度位置,但没有让控制台记录任何内容。

这是网站:

我的代码是:

const request = require('request'); const cheerio = require('cheerio'); let URL = 'https://data.baltimorecity.gov/Public-Safety/CCTV-Locations/hdyb-27ak/data' let cameras = []; request(URL, function(err, res, body) { if(!err && res.statusCode == 200) { let $ = cheerio.load(body); $('div.blist-t1-c140113793').each(function() { let camera = $(this); let location = camera.text(); console.log(location); cameras.push(location); }); console.log(cameras); } });

我已经尝试设置为blist-t1-c140113793和blist-td blist-t1-c140113793但两者都没有奏效。

I'm trying to download the lat/long locations of CCTV locations from the City of Baltimore website (project on the surveillance state) but not getting the console to log anything.

Here's the site:

and my code is:

const request = require('request'); const cheerio = require('cheerio'); let URL = 'https://data.baltimorecity.gov/Public-Safety/CCTV-Locations/hdyb-27ak/data' let cameras = []; request(URL, function(err, res, body) { if(!err && res.statusCode == 200) { let $ = cheerio.load(body); $('div.blist-t1-c140113793').each(function() { let camera = $(this); let location = camera.text(); console.log(location); cameras.push(location); }); console.log(cameras); } });

I've tried setting the to blist-t1-c140113793 and blist-td blist-t1-c140113793 but neither has worked.

最满意答案

这是因为在呈现页面之后,这些div的数据是异步加载的。 Cherrio或任何其他此类库不执行JavaScript。 您需要分析网络流量并了解哪个HTTP调用加载此数据,或使用Selenium之类的东西,它实际上在浏览器中执行JavaScript。

That's because data for those divs are loaded asynchronously, after the page was rendered. JavaScript is not executed by Cherrio, or any other such library. You'll need either to analyze network traffic and understand which HTTP call loads this data, or use something like Selenium, that actually executes JavaScript inside the browser.

更多推荐