免费注册 查看新帖 |

Chinaunix

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

利用perl提取google网页搜索结果中的内容 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-28 10:40 |只看该作者 |倒序浏览
本帖最后由 xingzhou823 于 2012-08-28 10:46 编辑


各位,早上好

利用google搜索时,输入关键词以后可能会出现很多结果,例如 输入“医学 email @ .com” 以后,会有43000个结果,我的目的就是要得到这么多结果中的邮箱信息。
只要每个结果中含有 邮箱格式的内容就提取出来。

利用perl来实现这一过程该怎么弄呢,

我之前只用过利用perl来提取 生物数据库NCBI 的pubmed数据库中的pubmed id,如下所示
  1. #!/usr/bin/perl
  2. use strict;
  3. #use warnings;
  4. die "Usage : perl $0 <KEY.list> <key_word> <count.txt>\n"unless(@ARGV==3);
  5. open(IN,$ARGV[0])or die $!;
  6. my $key_word=$ARGV[1];
  7. open(OUT1,">$ARGV[2]") or die "could not open file!";
  8. open(OUT,">result_abstract.tmp.v2") or die "could not open file!";
  9. #perl **.pl  KEY.list  count.txt
  10. my %has;
  11. my @key;
  12. my %notkey;
  13. while(<IN>){
  14.         chomp (my $tmp=$_);
  15.    my ($key,$notkey,$a2)=split(/\s+/,$tmp);
  16.          $key=[        DISCUZ_CODE_0        ] if($key=~/\w+.*\w+/);
  17.         $notkey=[        DISCUZ_CODE_0        ] if($notkey=~/\w+.*\w+/);
  18.         $has{$key}=$tmp;
  19.         $notkey{$key}=$notkey;
  20.         push @key,$key;
  21. }





  22. sub ask_user {
  23.   print "$_[0] [$_[1]]: ";
  24.   my $rc = <>;
  25.   chomp $rc;
  26.   if($rc eq "") { $rc = $_[1]; }
  27.   return $rc;
  28. }

  29. # ---------------------------------------------------------------------------
  30. # Define library for the 'get' function used in the next section.
  31. # $utils contains route for the utilities.
  32. # $db, $query, and $report may be supplied by the user when prompted;
  33. # if not answered, default values, will be assigned as shown below.

  34. use LWP::Simple;

  35. my $utils = "http://www.ncbi.nlm.nih.gov/entrez/eutils";
  36. my $num;
  37. # ---------------------------------------------------------------------------
  38. # $esearch cont羒ns the PATH & parameters for the ESearch call
  39. # $esearch_result containts the result of the ESearch call
  40. # the results are displayed 羘d parsed into variables
  41. # $Count, $QueryKey, and $WebEnv for later use and then displayed.
  42. foreach my $key (@key){
  43. my        $db="Pubmed";
  44. my $report="abstract";
  45. my $query ="($key NOT $notkey{$key})AND $key_word";
  46. my $esearch = "$utils/esearch.fcgi?" .
  47.               "db=$db&retmax=1&usehistory=y&term=";
  48. $num++;
  49.         #print "$key\t$num\n";
  50.         print "$query\t$num\n";
  51. my $esearch_result = get($esearch . $query);

  52. $esearch_result =~
  53.   m|<Count>(\d+)</Count>.*<QueryKey>(\d+)</QueryKey>.*<WebEnv>(\S+)</WebEnv>|s;

  54. my $Count    = $1;
  55. my $QueryKey = $2;
  56. my $WebEnv   = $3;
  57. $has{$key}=[        DISCUZ_CODE_0        ] if($has{$key}=~/\w+.*\w+/);
  58. print OUT1 "$has{$key}\tQuery \: $query \tpubmed_count\t$Count\n";
  59. # ---------------------------------------------------------------------------
  60. # this area defines a loop which will display $retmax citation results from
  61. # Efetch each time the the Enter Key is pressed, after a prompt.

  62. my $retstart;
  63. my $retmax=$Count;

  64. for($retstart = 0; $retstart < $retmax; $retstart += $retmax) {
  65.   my $efetch = "$utils/efetch.fcgi?" .
  66.                "rettype=$report&retmode=text&retstart=$retstart&retmax=$retmax&" .
  67.                "db=$db&query_key=$QueryKey&WebEnv=$WebEnv";
  68.        

  69.   my $efetch_result = get($efetch);
  70.   
  71.   print OUT "---------\nQuery\: $key\t$Count\t$query\nEFETCH RESULT(".
  72.          ($retstart + 1) . ".." . ($retstart + $retmax) . "): ".
  73.         "[$efetch_result]\n-----PRESS ENTER!!!-------\n";
  74.   ;
  75. }
  76. }
  77. close OUT;
复制代码
应用到google上面就不一样了

该怎么写呢?

论坛徽章:
42
19周年集字徽章-周
日期:2019-10-14 14:35:31平安夜徽章
日期:2015-12-26 00:06:30数据库技术版块每日发帖之星
日期:2015-12-01 06:20:002015亚冠之首尔
日期:2015-11-04 22:25:43IT运维版块每日发帖之星
日期:2015-08-17 06:20:00寅虎
日期:2014-06-04 16:25:27狮子座
日期:2014-05-12 11:00:00辰龙
日期:2013-12-20 17:07:19射手座
日期:2013-10-24 21:01:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34IT运维版块每日发帖之星
日期:2016-01-27 06:20:0015-16赛季CBA联赛之新疆
日期:2016-06-07 14:10:01
2 [报告]
发表于 2012-08-28 10:58 |只看该作者
用lwp或者curl先把网页抓下来
然后就很简单吧
用关键字/正则匹配含@的字符串就可以了
注意一下前后的分界就行了吧

论坛徽章:
0
3 [报告]
发表于 2012-08-28 11:35 |只看该作者
能具体点吗,还是不会改回复 2# laputa73


   

论坛徽章:
42
19周年集字徽章-周
日期:2019-10-14 14:35:31平安夜徽章
日期:2015-12-26 00:06:30数据库技术版块每日发帖之星
日期:2015-12-01 06:20:002015亚冠之首尔
日期:2015-11-04 22:25:43IT运维版块每日发帖之星
日期:2015-08-17 06:20:00寅虎
日期:2014-06-04 16:25:27狮子座
日期:2014-05-12 11:00:00辰龙
日期:2013-12-20 17:07:19射手座
日期:2013-10-24 21:01:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34IT运维版块每日发帖之星
日期:2016-01-27 06:20:0015-16赛季CBA联赛之新疆
日期:2016-06-07 14:10:01
4 [报告]
发表于 2012-08-28 13:23 |只看该作者
你可以先cpan一下lwp的例子看看啊
试试看有什么地方不明白的再问
  1. my $url = 'http://freshair.npr.org/dayFA.cfm?todayDate=current';
  2.     # Just an example: the URL for the most recent /Fresh Air/ show

  3.   use LWP::Simple;
  4.   my $content = get $url;
  5.   die "Couldn't get $url" unless defined $content;

  6.   # Then go do things with $content, like this:

  7.   if($content =~ m/jazz/i) {
  8.     print "They're talking about jazz today on Fresh Air!\n";
  9.   } else {
  10.     print "Fresh Air is apparently jazzless today.\n";
  11.   }
复制代码

论坛徽章:
0
5 [报告]
发表于 2012-08-28 15:06 |只看该作者
这个需求的难点  应该是你反复请求google时   ip被重置 的 应对策略 ~

论坛徽章:
42
19周年集字徽章-周
日期:2019-10-14 14:35:31平安夜徽章
日期:2015-12-26 00:06:30数据库技术版块每日发帖之星
日期:2015-12-01 06:20:002015亚冠之首尔
日期:2015-11-04 22:25:43IT运维版块每日发帖之星
日期:2015-08-17 06:20:00寅虎
日期:2014-06-04 16:25:27狮子座
日期:2014-05-12 11:00:00辰龙
日期:2013-12-20 17:07:19射手座
日期:2013-10-24 21:01:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34IT运维版块每日发帖之星
日期:2016-01-27 06:20:0015-16赛季CBA联赛之新疆
日期:2016-06-07 14:10:01
6 [报告]
发表于 2012-08-28 15:30 |只看该作者
那就百度或者360咯
google已经悲剧了

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
7 [报告]
发表于 2012-08-28 15:39 |只看该作者
先占个位置,坐等答案出现

论坛徽章:
42
19周年集字徽章-周
日期:2019-10-14 14:35:31平安夜徽章
日期:2015-12-26 00:06:30数据库技术版块每日发帖之星
日期:2015-12-01 06:20:002015亚冠之首尔
日期:2015-11-04 22:25:43IT运维版块每日发帖之星
日期:2015-08-17 06:20:00寅虎
日期:2014-06-04 16:25:27狮子座
日期:2014-05-12 11:00:00辰龙
日期:2013-12-20 17:07:19射手座
日期:2013-10-24 21:01:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34IT运维版块每日发帖之星
日期:2016-01-27 06:20:0015-16赛季CBA联赛之新疆
日期:2016-06-07 14:10:01
8 [报告]
发表于 2012-08-28 17:30 |只看该作者
LZ的意思大概是不知道怎么翻页遍历结果?
这个在下面的页码上有每一页的连接,可以用程序拼接
循环取直到最后一页
和你那个生物网页的fcgi翻页原理其实也差不多。
我用google搜索了一下“医学 email @ .com”
搜索获得约 206,000,000 条结果,以下是第 23 页 (用时 0.24 秒) 。。。
页数好多。。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2012-08-29 08:22 |只看该作者
有这样需求的多半是要做坏事。

做坏事就要有做坏事的本事,得有基本的技术能力和钻研精神。就这么个问题都解决不了,趁早放弃。

LZ做这件事,后面可能出现的问题还多着呢。想做成你就必须能自己解决这些问题。

论坛徽章:
0
10 [报告]
发表于 2012-08-31 15:26 |只看该作者
这个例子我是能看明白的,可是google搜索中我直接引用网页
my $ulr = "http://scholar.google.com.hk/scholar?start=0&q=%E5%8C%BB%E5%AD%A6+email+%40+.com&hl=zh-CN&as_sdt=1,5&as_vis=1"
是得不到结果的,并且没有自动翻页的功能。

回复 4# laputa73


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP