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 抓取具有动态内容的页面?


- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01