【问题标题】:Web Scraping pages with javascript content on a shared hosting在共享主机上使用 javascript 内容抓取网页
【发布时间】:2012-07-19 21:27:46
【问题描述】:

我想抓取使用 Javascript 或类似内容动态加载内容的网页。

类似于无头浏览器的东西,我可以在没有 X 的 Linux 共享主机上使用它。

我可以使用 PHP、Perl、Ruby 或 Python。

你们中有人知道一些可以帮助我的框架/无头浏览器吗?

非常感谢。

【问题讨论】:

  • headless internet browser? 的可能重复项
  • 有什么理由你不能得到一个便宜的 VPS 并在上面安装你想要的任何东西?共享主机通常是运行此类密集操作的糟糕场所。

标签: php ruby perl web-scraping shared-hosting


【解决方案1】:

如果您需要模拟按键或点击以加载内容,请尝试Selenium 控制浏览器。

对于无头浏览器,这里列出了一些:headless internet browser?

【讨论】:

    【解决方案2】:

    见图书馆WWW::Scripter

    简介:

    use WWW::Scripter;
    
    $w = new WWW::Scripter;
    $w->use_plugin('Javascript');
    $w->get('http://some.site.com/that/uses/javascript');
    $w->content; # returns the HTML content, possibly modified by scripts
    $w->eval('alert("Hello from JavaScript")');
    $w->document->getElementsByTagName('div')->[0]->...
    

    【讨论】:

      【解决方案3】:

      在 Perl 中使用 Perl WWW::Mechanize。该模块有许多方法可以执行类似 Web 浏览器的功能。下面是一个示例代码:

      use WWW::Mechanize;
      use strict;
      
      my $username = "admin";
      my $password = "welcome1";  
      my $outpath  = "/home/data/output";
      my $fromday = 7;
      my $url  = "https://www.myreports.com/tax_report.php";
      my $name = "tax_report";
      my $outfile = "$outpath/$name.html";
      
      my $mech = WWW::Mechanize->new(noproxy =>'0');  
      
      $mech->get($url);
      $mech->field(login => "$username");
      $mech->field(passwd => "$password");
      
      $mech->add_handler("request_send",  sub { shift->dump; return });
      $mech->add_handler("response_done", sub { shift->dump; return });
      
      $mech->click_button(value=>"Login now");
      
      my $response = $mech->content();
      
      print "Generating report: $name...\n";
      
      open (OUT, ">>$outfile")|| die "Cannot create report file $outfile";
      print OUT "$response";
      close OUT;
      

      如果您想要处理网页中的 Javascript(您想要抓取),您可以查看 WWW::Mechanize::Firefox,但这可能需要为 Mozilla 安装 MozRepl 插件。

      【讨论】:

      • W:M:Firefox 不是无头浏览器,它非常需要 X。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2013-06-02
      • 2010-10-09
      相关资源
      最近更新 更多