如何使用node.js抓取包含动态内容的页面? [英] How can I scrape pages with dynamic content using node.js?

查看:156
本文介绍了如何使用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_listclass =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抓取包含动态内容的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆