检查页面是否包含特定的单词 [英] Check if page contains specific word

查看:278
本文介绍了检查页面是否包含特定的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查页面是否包含特定字词?示例:如果页面包含单词candybar,则返回true或false。请注意,candybar有时可能位于标签(candybar)之间,有时候不会。如何做到这一点?



这是我的代码grabing的网站(只是不知道如何检查通过该网站):

 #!/ usr / bin / perl -w 

使用utf8;

使用RPC :: XML;
使用RPC :: XML :: Client;
使用Data :: Dumper;
使用Encode;
使用Time :: HiRes qw(usleep);

printContent-type:text / html\\\
\\\
;

使用LWP :: Simple;

$ pageURL =http://example.com;

$ simplePage = get($ pageURL);

if($ simplePage =〜m / candybar /){
printits there!;


解决方案

使用某种解析器,如果你正在寻找HTML中的单词或其他以已知方式标记的东西[例如XML]。我使用HTML :: Tokeparser,但在CPAN上有很多解析模块。

我已经将解析器的返回值解释为注释,以防使用此解析器。这是从一个实时的程序中提取的,我用它来翻译网页中的文本,所以我已经拿出了一些零碎的东西。



上面关于检查的注释LWP回报的状态和内容,也是非常明智的,如果网站是离线的,您需要知道。



open(my $ fh,< ;:utf8,$ file)||死无法打开$文件:$!;

  my $ p = HTML :: TokeParser-> new($ fh)||死不能打开:$!; 

$ p-> empty_element_tags(1); #配置它的行为
#把输出放到这里,它的累积
while(my $ token = $ p-> get_token){
#[S,$ tag,$ attr, $ attrseq,$ text]
#[E,$ tag,$ text]
#[T,$ text,$ is_data]
#[C,$ text ]
#[D,$ text]
#[PI,$ token0,$ text $ b $ my($ type,$ string)= get_output($ token);
#[T,$ text,$ is_data]:文本
的规则if($ type eq'T'&& $ string =〜/ ^ candybar /){

}


How can I check if a page contains a specific word. Example: I want to return true or false if the page contains the word "candybar". Notice that the "candybar" could be in between tags (candybar) sometimes and sometimes not. How do I accomplish this?

Here is my code for "grabing" the site (just dont now how to check through the site):

#!/usr/bin/perl -w

use utf8;

use RPC::XML;
use RPC::XML::Client;
use Data::Dumper;
use Encode;
use Time::HiRes qw(usleep);

print "Content-type:text/html\n\n";

use LWP::Simple; 

$pageURL = "http://example.com"; 

$simplePage=get($pageURL);

if ($simplePage =~ m/candybar/) {   
 print "its there!";
}

解决方案

I'd suggest that you use some kind of parser, if you're looking for words in HTML or anything else that's tagged in a known way [XML, for example]. I use HTML::Tokeparser but there's many parsing modules on CPAN.

I've left the explanation of the returns from the parser as comments, in case you use this parser. This is extracted from a live program that I use to machine translate the text in web pages, so I've taken out some bits and pieces.

The comment above about checking status and content of returns from LWP, is very sensible too, if the website is off-line, you need to know that.

open( my $fh, "<:utf8", $file ) || die "Can't open $file : $!";

my $p = HTML::TokeParser->new($fh) || die "Can't open: $!";

$p->empty_element_tags(1);    # configure its behaviour
# put output into here and it's cumulated
while ( my $token = $p->get_token ) {
    #["S",  $tag, $attr, $attrseq, $text]
    #["E",  $tag, $text]
    #["T",  $text, $is_data]
    #["C",  $text]
    #["D",  $text]
    #["PI", $token0, $text
    my ($type,$string) = get_output($token) ;             
    # ["T",  $text, $is_data] : rule for text
    if ( $type eq 'T' && $string =~ /^candybar/ ) {

    }

这篇关于检查页面是否包含特定的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆