Perl6 必应抓取(1):测试版代码

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

默认所有查找都是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'>'(.*?)'</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/'<strong>'//;}
    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/'<strong>'//;}
    $result = do given $result {S:g/'</strong>'//;}
    say $result;
    $fp.say($result);

  }
  #$fp.print($html);
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

下次代码优化:

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

复制代码
http://cn.bing.com/search?q=123456789&first=1&FORM=PERE
http://cn.bing.com/search?q=123456789&first=11&FORM=PERE
http://cn.bing.com/search?q=123456789&first=21&FORM=PERE1
http://cn.bing.com/search?q=123456789&first=31&FORM=PERE2
http://cn.bing.com/search?q=123456789&first=41&FORM=PERE3
http://cn.bing.com/search?q=123456789&first=51&FORM=PERE4
http://cn.bing.com/search?q=123456789&first=61&FORM=PERE4
http://cn.bing.com/search?q=123456789&first=71&FORM=PERE4
http://cn.bing.com/search?q=123456789&first=81&FORM=PERE4
http://cn.bing.com/search?q=123456789&first=91&FORM=PERE4
复制代码

 

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

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

 

 

 

 

 

 

 

 

 

 

posted on   Perl6  阅读(374)  评论(0编辑  收藏  举报

编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· 卧槽!C 语言宏定义原来可以玩出这些花样?高手必看!
· langchain0.3教程:从0到1打造一个智能聊天机器人

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示