免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2112 | 回复: 7
打印 上一主题 下一主题

哪位朋友看过perl & LWP? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-09-26 17:38 |只看该作者 |倒序浏览
我正在看perl & LWP,p26的例子alta_count.pl中使用的http://www.altavista.com已经不可使用,如果改为百度该如何修改?
  1. #!/usr/bin/perl

  2. # Example code from Chapter 2 of /Perl and LWP/ by Sean M. Burke
  3. # http://www.oreilly.com/catalog/perllwp/
  4. # sburke@cpan.org

  5. require 5;
  6. use strict;
  7. use warnings;

  8. use URI::Escape;
  9. foreach my $word (@ARGV) {
  10.   next unless length $word; # sanity-checking
  11.   my $url = 'http://www.altavista.com/sites/search/web?q=%22'
  12.     . uri_escape($word) . '%22&kl=XX';#此处如何修改?
  13.   
  14.   my ($content, $status, $is_success) = do_GET($url);
  15.   if (!$is_success) {
  16.     print "Sorry, failed: $status\n";
  17.   } elsif ($content =~ m/>We found ([0-9,]+) results?/) { # like "1,952"
  18.     print "$word: $1 matches\n";
  19.   } else {
  20.     print "$word: Page not processable, at $url\n";
  21.   }
  22.   sleep 2; # Be nice to Altavista's servers!!!
  23. }

  24. # And then my favorite do_GET routine:
  25. use LWP; # loads lots of necessary classes.
  26. my $browser;
  27. sub do_GET {
  28.   $browser = LWP::UserAgent->new unless $browser;
  29.   my $resp = $browser->get(@_);
  30.   return ($resp->content, $resp->status_line, $resp->is_success, $resp)
  31.     if wantarray;
  32.   return unless $resp->is_success;
  33.   return $resp->content;
  34. }
  35. __END__


  36. Output:

  37. C:\book\code\ch02>perl alta_count.pl asafetida asafoetida
  38. asafetida: 2,024 matches
  39. asafoetida: 5,630 matches

复制代码

论坛徽章:
0
2 [报告]
发表于 2016-09-27 13:35 |只看该作者

require 5;
use strict;
use warnings;
use utf8;

use URI::Escape;
use Encode;
foreach my $word (@ARGV) {
  next unless length $word; # sanity-checking
        my $url = "http:". "//". "www.". "sogou". ".com". "/sohu?query="
    . uri_escape($word) . '';
  
  my ($content, $status, $is_success) = do_GET($url);
  $content= decode( 'utf8', $content);
  
  if (!$is_success) {
    print "Sorry, failed: $status\n";
  } elsif ($content =~ m{找到约<resnum id="scd_num">(.+)</resnum>条}) { # like "1,952"
    print "$word: $1 matches\n";
  } else {
    print "$word: Page not processable, at $url\n";
  }
  sleep 2; # Be nice to Altavista's servers!!!
}

# And then my favorite do_GET routine:
use LWP; # loads lots of necessary classes.
my $browser;
sub do_GET {
  $browser = LWP::UserAgent->new unless $browser;
  my $resp = $browser->get(@_);
  return ($resp->content, $resp->status_line, $resp->is_success, $resp)
    if wantarray;
  return unless $resp->is_success;
  return $resp->content;
}

C:\Users\Administrator\Desktop\perl\perl\network>perl alta_count.pl clinton trump
clinton: 150,584 matches
trump: 488,868 matches

评分

参与人数 1信誉积分 +10 收起 理由
hztj2005 + 10 很给力!

查看全部评分

论坛徽章:
0
3 [报告]
发表于 2016-09-27 16:08 |只看该作者
本帖最后由 hztj2005 于 2016-09-27 16:10 编辑
fcl21721 发表于 2016-09-27 13:35
my $url = "http:". "//". "www.". "sogou". ".com". "/sohu?query="
     . uri_escape($word) . '';

请教这个语句是否必须通过抓包才能发现?那么通常用哪个工具抓包呢?
谢谢!

论坛徽章:
0
4 [报告]
发表于 2016-09-27 20:30 |只看该作者
不是啊,我修改的两个地方
1. 那个url,因为原来的url好像已经不用了,现在变成yahoo的了,我改成了搜狗,这个网站好像不能贴url,所以我就让他拼接了。(这个不难的,从ie的地址栏就能看出他的搜索关键字是哪个。另外加了一个转码)
2. 那个正则我也改过了。

抓包用winshark。我也不是很懂,网上有一些视频教程,你可以看看。


论坛徽章:
0
5 [报告]
发表于 2016-09-27 23:34 |只看该作者
fcl21721 发表于 2016-09-27 20:30
不是啊,我修改的两个地方
1. 那个url,因为原来的url好像已经不用了,现在变成yahoo的了,我改成了搜狗, ...

这是我在搜狗输入clinton,按回车之后,搜狗返回结果后,地址栏中复制过来的。而你代码中实际是下面这样的,我就不理解你如何推断出来的。
(如果输入clinton,不按回车,地址栏就是https://www.sogou.com/

http://www.sogou.com/sohu?query=clinton

论坛徽章:
0
6 [报告]
发表于 2016-09-28 09:58 |只看该作者
一般的话,搜索关键字就一个,比如kw=, query=,我以前看过selenium的例子,他用的是baidu做为搜索网站的,关键字就一个。那我想其他的估计也就一个关键字(其实你原始的url也就一个搜索关键字),其他的url参数都不重要,像我的例子里面,sohu?是指我是从www|sohu|com里面搜索的,他redirect到搜狗,你这个是直接在搜狗上搜索的,其他的url参数不重要,都是些状态信息。

用winshark看的话,你也不会知道哪个参数是重要的,靠看靠猜吧。

评分

参与人数 1信誉积分 +10 收起 理由
hztj2005 + 10 很给力!

查看全部评分

论坛徽章:
0
7 [报告]
发表于 2016-09-28 10:26 |只看该作者
fcl21721 发表于 2016-09-28 09:58
一般的话,搜索关键字就一个,比如kw=, query=,我以前看过selenium的例子,他用的是baidu做为搜索网站的, ...

谢谢!我被大堆参数唬住了。
不过我想看完perl & LWP这本书之后,应该会有比较深的了解。

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
8 [报告]
发表于 2016-09-28 10:34 |只看该作者
回复 7# hztj2005
用 google 搜索引擎, 相对来说对于参数的解释比较全面!
抓包的话, 如果不是特别底层的话 我推荐 Fiddler 虽然我也是个菜鸟.

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP