我想开发一个从种子URL开始的Web爬虫,然后抓取它找到的100个html页面,它们与种子URL属于同一个域,并保留遍历的URL的记录,避免重复.我写了以下内容,但$ url_count值似乎没有增加,检索到的URL甚至包含来自其他域的链接.我该如何解决这个问题?这里我插入了stackoverflow.com作为我的起始URL.
use strict;
use warnings;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
##open file to store links
open my $file1,">>", ("extracted_links.txt");
select($file1);
##starting URL
my @urls = 'http://stackoverflow.com/';
my $browser = LWP::UserAgent->new('IE 6');
$browser->timeout(10);
my %visited;
my $url_count = 0;
while (@urls)
{
my $url = shift @urls;
if (exists $visited{$url}) ##check if URL already exists
{
next;
}
else
{
$url_count++;
}
my $request = HTTP::Request->new(GET => $url);
my $response = $browser->request($request);
if ($response->is_error())
{
printf "%s\n", …Run Code Online (Sandbox Code Playgroud) 我正在读一个名为remin-freq的文本文件,它具有以下格式的数据:
1
1
13
2
Run Code Online (Sandbox Code Playgroud)
我想读取行并将值存储在数组中,如下所示:@a=(1, 1, 13, 2).Perl push函数给出索引值/行号,即1,2,3,4,而不是我想要的输出.你能指出错误吗?这是我做的:
use strict;
use warnings;
open(FH, "<mention-freq") || die "$!";
my @a;
my $line;
while ($line = <FH>)
{
$line =~ s/\n//;
push @a, $line;
print @a."\n";
}
close FH;
Run Code Online (Sandbox Code Playgroud) 我正在使用Weka中的Naive Bayes分类器进行NLP分类项目.我打算使用半监督机器学习,因此使用未标记的数据.当我在一组独立的未标记测试数据上测试从我的标记训练数据中获得的模型时,Weka会忽略所有未标记的实例.任何人都可以指导我如何解决这个问题?之前有人已在此处提出此问题,但未提供任何适当的解决方案.这是一个示例测试文件:
@relation referents
@attribute feature1 NUMERIC
@attribute feature2 NUMERIC
@attribute feature3 NUMERIC
@attribute feature4 NUMERIC
@attribute class{1 -1}
@data
1, 7, 1, 0, ?
1, 5, 1, 0, ?
-1, 1, 1, 0, ?
1, 1, 1, 1, ?
-1, 1, 1, 1, ?
Run Code Online (Sandbox Code Playgroud) 我正在使用向量空间模型构建基本搜索引擎,这是用于返回500个URL并从内容中删除SGML标记的爬虫.但是,它非常慢(仅检索URL需要超过30分钟).我该如何优化代码?我已插入wikipedia.org作为示例起始URL.
use warnings;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTML::LinkExtor;
my $starting_url = 'http://en.wikipedia.org/wiki/Main_Page';
my @urls = $starting_url;
my %alreadyvisited;
my $browser = LWP::UserAgent->new();
$browser->timeout(5);
my $url_count = 0;
while (@urls)
{
my $url = shift @urls;
next if $alreadyvisited{$url}; ## check if already visited
my $request = HTTP::Request->new(GET => $url);
my $response = $browser->request($request);
if ($response->is_error())
{
print $response->status_line, "\n"; ## check for bad URL
}
my $contents = $response->content(); ## get contents from URL
push …Run Code Online (Sandbox Code Playgroud)