How can I scrape pages with dynamic content using node.js?(如何使用 node.js 抓取具有动态内容的页面?)
问题描述
我正在尝试抓取 网站,但我没有得到一些元素,因为这些元素是动态创建的.
I am trying to scrape a website but I don't get some of the elements, because these elements are dynamically created.
我在node.js中使用cheerio,我的代码如下.
I use the cheerio in node.js and My code is below.
var request = require('request');
var cheerio = require('cheerio');
var url = "http://www.bdtong.co.kr/index.php?c_category=C02";
request(url, function (err, res, html) {
var $ = cheerio.load(html);
$('.listMain > li').each(function () {
console.log($(this).find('a').attr('href'));
});
});
此代码返回空响应,因为当页面加载时,<ul id="store_list" class="listMain">
为空.
This code returns empty response, because when the page is loaded, the <ul id="store_list" class="listMain">
is empty.
内容尚未附加.
如何使用 node.js 获取这些元素?如何抓取包含动态内容的页面?
How can I get these elements using node.js? How can I scrape pages with dynamic content?
推荐答案
给你;
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
var url = "http://www.bdtong.co.kr/index.php?c_category=C02";
page.open(url, function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
$('.listMain > li').each(function () {
console.log($(this).find('a').attr('href'));
});
}, function(){
ph.exit()
});
});
});
});
});
这篇关于如何使用 node.js 抓取具有动态内容的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 node.js 抓取具有动态内容的页面?


基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01