Perl6 必应抓取(1):测试版代码
发布日期:2025-05-02 00:37:11 浏览次数:1 分类:技术文章

本文共 1989 字,大约阅读时间需要 6 分钟。

一个相当丑漏的代码, 以后有时间再优化了。

默认所有查找都是15页, 如果结果没有15页这么多估计会有重复。速度还是很快的。

sub MAIN() {  my $fp = open 'bin_result.txt', :w;  my $number = 15;  print 'String:';  my $string = get;  $string  = do given $string {S:g/\s/+/};  use HTTP::UserAgent;  my $url = 'http://cn.bing.com/search?q=';  my $ua = HTTP::UserAgent.new;  my $check = rx/'<'cite'>'(.*?)''/;#要查的内容  my @number = '';  @number.append(0..$number);  my $page='';  my $html;  my $target = $url~$string~'&first=20&FROM=FERE'~$page;  $html = $ua.get($target).content;  loop {    say '===============> '~$target;    $html ~~ $check;    $html = $/.postmatch;    #$0 = do given ~$0 {S:g/''//;}    if not $0 {      #当是null时, 说明这一页已全部提取, 构造下一页      $page = Int($page);      my $page_next = $string~'&first='~$page~'0&FROM=FERE'~$page;      $target = $url~$page_next;      $html = $ua.get($target).content;      $page++;    #/search?q=123&first=10&FORM=PERE    #/search?q=123&first=20&FORM=PERE1    #/search?q=123&first=30&FORM=PERE2    #/search?q=123&first=30&FORM=PERE2    #last;     $html ~~ $check;        $html = $/.postmatch;        if ($page > $number) {
last;} } my $ok_check = $0.Str; my $result = $ok_check; $result = do given $result {S:g/''//;} $result = do given $result {S:g/''//;} say $result; $fp.say($result); } #$fp.print($html);}

 

 

 

 

 

 

 

 

 

 

 

 

下次代码优化:

总结一下必应的规律, 如下:

http://cn.bing.com/search?q=123456789&first=1&FORM=PEREhttp://cn.bing.com/search?q=123456789&first=11&FORM=PEREhttp://cn.bing.com/search?q=123456789&first=21&FORM=PERE1http://cn.bing.com/search?q=123456789&first=31&FORM=PERE2http://cn.bing.com/search?q=123456789&first=41&FORM=PERE3http://cn.bing.com/search?q=123456789&first=51&FORM=PERE4http://cn.bing.com/search?q=123456789&first=61&FORM=PERE4http://cn.bing.com/search?q=123456789&first=71&FORM=PERE4http://cn.bing.com/search?q=123456789&first=81&FORM=PERE4http://cn.bing.com/search?q=123456789&first=91&FORM=PERE4

 

在页面上测试, 参数只虽两个即可:

q=查询字符串&first=起始帐号

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/perl6/p/7425641.html

上一篇:Perl处理文本一例
下一篇:perl---2012学习笔记

发表评论

最新留言

不错!
[***.144.177.141]2025年04月27日 08时54分27秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章