你怎么用PhantomJS蜘蛛

Joh*_*rch 16 web-crawler phantomjs

我正在尝试利用PhantomJS和蜘蛛整个域.我想从根域开始,例如www.domain.com - 拉取所有链接(a.href),然后获取每个新链接并添加新的链接到que,如果它们没有被爬行或在que中.

想法,帮助?

提前致谢!

nra*_*itz 20

您可能有兴趣查看Pjscrape(免责声明:这是我的项目),一个构建在PhantomJS之上的开源抓取库.它内置支持抓取页面并从中逐步抓取信息.您可以使用如下的简短脚本来抓取整个站点,查看每个锚点链接:

pjs.addSuite({
    url: 'http://www.example.com/your_start_page.html',
    moreUrls: function() {
        // get all URLs from anchor links,
        // restricted to the current domain by default
        return _pjs.getAnchorUrls('a');
    },
    scraper: function() {
        // scrapers can use jQuery
        return $('h1').first().text();
    }
});
Run Code Online (Sandbox Code Playgroud)

默认情况下,这将跳过已经抓取的页面,并且只跟随当前域上的链接,尽管这些都可以在您的设置中更改.


Har*_*ood 5

这是一个老问题,但更新,一个真棒现代答案是http://www.nightmarejs.org/(github上:https://github.com/segmentio/nightmare)

从他们的主页引用一个引人注目的例子:

RAW PHANTOMJS:

phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open('http://yahoo.com', function (status) {
      page.evaluate(function () {
        var el =
          document.querySelector('input[title="Search"]');
        el.value = 'github nightmare';
      }, function (result) {
        page.evaluate(function () {
          var el = document.querySelector('.searchsubmit');
          var event = document.createEvent('MouseEvent');
          event.initEvent('click', true, false);
          el.dispatchEvent(event);
        }, function (result) {
          ph.exit();
        });
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

与夜生活:

new Nightmare()
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('.searchsubmit')
  .run();
Run Code Online (Sandbox Code Playgroud)


Cam*_*ker 3

首先,选择索引页面上的所有锚点并列出 href 值。您可以使用 PhantomJS 的文档选择器或 jQuery 选择器来完成此操作。然后对每个页面执行相同的操作,直到页面不再包含任何新链接。您应该有一个所有链接的主列表和每个页面的链接列表,以便能够确定链接是否已被处理。您可以将网络爬行视为一棵树。树的根节点是索引页,子节点是从索引页链接的页面。每个子节点可以有一个或多个子节点,具体取决于子页面包含的链接。我希望这有帮助。