我正在构建一个应用程序,以基于Linux桌面环境中的关键字进行一些文本挖掘。我的目标是使用wget从Wordpress网站列表中下载网页,将网页保存到磁盘,然后将每篇文章分开进行进一步处理。我的想法是,我可以根据某些单词的出现频率将单个文章排在最下面。Wordpress博客中的文章通常遵循以下约定:
<article></article>
介于两者之间。到目前为止,我已经提出了类似以下perl代码的内容:
$site = "somepage.somedomain"; #can be fed from a database later
$outfile = "out1.txt"; #can be incremented as we go along
$wgcommand = "wget --output-document $outfile $site";
system($wgcommand);
open SITEIN, '<', $outfile;
@sitebodyarr = <SITEIN>;
close SITEIN;
$pagescaler = join('', @sitebodyarr); #let us parse the page.
#this is where I have trouble. the though is to look for a mated pair of tags.
#word press documents are stored between <article> and </article>
$article =~ m/<article>*<\/article>/$pagescaler/g;
#I put the /g flag there, but it doesn't seem to get me
#what I want from the string - *ALL* of the articles one-by-one.
有什么想法使之匹配从html文档返回的所有文章标记对集?
如果不可能使用正则表达式,那么我的下一个想法是对整个数组进行顺序处理,并捕获模式
$line =~m/<article>/
然后启动一个字符串变量来保存文章内容。继续使用该变量,直到找到模式为止
$line =~m/<\/article>/
然后将字符串存储-现在将包含文章的字符串存储到我的数据库或磁盘中,然后重复直到@sitebodyarr结束。但是,如果可能的话,我真的很想单线正则表达式。如果是的话,有人可以告诉我它的外观吗?
看看Mojo套件,其中包括华丽的模块,例如Mojo::DOM
-轻松轻松地进行网络抓取。
use strict; use warnings;
use feature 'say';
use Mojo;
my $ua = Mojo::UserAgent->new;
my $request = $ua->get('http://example.com/');
if (my $resp = $request->success) {
my $dom = $resp->dom();
for my $article ($dom->find('article')->each) {
say "$article";
}
}
# short version:
say for Mojo::UserAgent->new->get('http://example.com/')->res->dom('article')->each;
您可以使用CSS选择器浏览DOM。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句