在共享主机上使用javascript内容进行Web抓取页面

时间:2012-07-20 15:14:58

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

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

像无头浏览器,我可以在没有X的Linux共享主机上使用。

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

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

非常感谢。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

参见图书馆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]->...

答案 2 :(得分:-2)

在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;

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