免费注册 查看新帖 |

Chinaunix

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

如何用Perl读取一个WEB页面的内容? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-26 10:29 |只看该作者 |倒序浏览
刚学Perl,想用Perl定期读取一个WEB页的内容(就是在WEB页上查看源码看到的东西,这个WEB页是允许这个操作的),我完全不知道要用到哪些模块,怎么做,可否请大家指点一下,如果有这样的例子最好了,先谢了

论坛徽章:
0
2 [报告]
发表于 2008-05-26 11:06 |只看该作者
lwp

论坛徽章:
0
3 [报告]
发表于 2008-05-26 11:33 |只看该作者
我在前两天问了一个问题,贴的代码对你有启示作用

另外版里搜搜吧,有些有用的资料~~

论坛徽章:
0
4 [报告]
发表于 2008-05-26 12:01 |只看该作者
我先搜索过的,不过大家的问题都是挺复杂的,其实我就是简单的获取全部内容,不分什么头还是什么的,是要用到LWP模块吗?我先去找一下这个模块的资料

论坛徽章:
0
5 [报告]
发表于 2008-06-22 12:27 |只看该作者
LWP::Simple 或者LWP::UserAgent

论坛徽章:
0
6 [报告]
发表于 2008-06-22 16:43 |只看该作者
自己perldoc下快多了

论坛徽章:
0
7 [报告]
发表于 2008-06-24 09:32 |只看该作者
这种问题google一下比这边发帖快无数倍

论坛徽章:
0
8 [报告]
发表于 2008-06-25 12:11 |只看该作者
发个我自己写的代码,是取GOOGLE的天气的情况的,包括解析网页的.你应该可以拿来参考参考.还可以下图片的.

  1. #!/usr/bin/perl
  2. use strict;
  3. use Cwd;
  4. use Socket qw(:DEFAULT :crlf);

  5. ####################################给小于10的数值在前面添0
  6. sub fillzero
  7. {
  8.         my($num)=@_;
  9.         if($num<10)
  10.         {
  11.                 return "0".$num;
  12.         }
  13.         else{
  14.                 return $num;
  15.         }
  16. }

  17. my ($socket,$buffer,%image);
  18. my ($path)=getcwd()."/";
  19. my ($image_path)=$path."image/";
  20. my ($host,$port)=("www.google.com",80);
  21. my($sec,$min,$hour,$day,$month,$year,$wday,$yday,$isdst) = localtime();
  22. $year+=1900;$month+=1;$month=fillzero($month);$day=fillzero($day);
  23. my @city=("南宁",);
  24. my $head="GET http://www.google.cn/search?hl=zh-CN&q={city}+tq&btnG=Google+%E6%90%9C%E7%B4%A2&meta=&aq=f HTTP/1.0\r\n";
  25. $head.="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
  26. $head.="Referer: http://www.google.cn\r\n";
  27. $head.="Accept-Language: zh-cn\r\n";
  28. $head.="Proxy-Connection: Keep-Alive\r\n";
  29. $head.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)\r\n";
  30. $head.="Host: www.google.cn\r\n";
  31. $head.="Cookie: SS=Q0=55S35aWz5Lqk6YWN5Zu-; PREF=ID=555c912d17d9d47b:NW=1:TM=1209093843:LM=1209093843:S=Ee0KCVmzEj-c50qs\r\n\r\n".CRLF;
  32. my $image_head="GET http://www.google.com/images/weather/{file} HTTP/1.0\r\n";
  33. $image_head.="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
  34. $image_head.="Accept-Language: zh-cn\r\n";
  35. $image_head.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)\r\n";
  36. $image_head.="Host: www.google.com\r\n";
  37. $image_head.="Proxy-Connection: Keep-Alive\r\n\r\n".CRLF;
  38. $path=~s/\//\\/g;$image_path=~s/\//\\/g;
  39. print $path."\n";
  40. print $image_path."\n";
  41. system("del $path*.xml");
  42. #############################################
  43. ##
  44. ## 取网页
  45. ##
  46. #############################################
  47. foreach (@city)
  48. {
  49. my $shead=$head;$shead=~s/\{city\}/$_/;
  50. print $shead;

  51. socket ($socket, AF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "problem creating socket: $!\n";
  52. my $con_ok = connect ($socket, sockaddr_in($port, inet_aton($host))) or die "problem connecting to \"$host\", port $port: $!\n";
  53. print "error " unless $con_ok;
  54. open(HTML,">$path$_-google.htm")  or die "error";
  55. syswrite($socket,$shead,length($shead),0);
  56. while(1)
  57. {
  58.   $buffer="";
  59.   sysread($socket,$buffer,1024 );
  60.   $buffer=~s/\>\s*\</>\n</g;
  61.   print HTML $buffer;
  62.   last if($buffer=~/\<\/(html)|(HTML)\>\s*$/);
  63. }
  64. close(HTML);close($socket);
  65. #last;
  66. }
  67. #############################################
  68. ##
  69. ## 分析网页,取天气信息
  70. ##
  71. #############################################
  72. open(XML,">$path$year$month$day.xml") or die "error";
  73. print XML "\<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\<root\>\n";
  74. foreach (@city)
  75. {
  76. open(HTML,"$path$_-google.htm")  or die "error";
  77. while(my $line=<HTML>)
  78. {
  79.   if($line=~/\<FONT color\=\#cc0033\>(\S+)\<\/FONT\>/)
  80.   {
  81.    print XML "\t<region city=\"$1\">\n";
  82.   }
  83.   if($line=~/\<br\>(.+)\<br\>(.+)\<\/div\>/)
  84.   {
  85.    print XML "\t\t<wind>$1</wind>\n\t\t<humidity>$2</humidity>\n";
  86.   }
  87.   if($line=~/\<div align=center style=\"padding:\d+px;float:left;font-size:\d+\%\"\>(.+)\<br\>/)
  88.   {
  89.    print XML "\t\t<temperature day=\"$1\" ";
  90.   }
  91.   if($line=~/\<img style=\".+\" src=\"(\/\w+)+\/(.+)\" alt=\".+\"\s+title=\"(.+)\"/)
  92.   {
  93.    $image{$2}=0 if(!defined($image{$2}));
  94.    print XML "img=\"$2\" weather=\"$3\" ";
  95.   }
  96.   if($line=~/\<nobr\>(.+)\<\/nobr\>/)
  97.   {
  98.    print XML ">$1</temperature>\n";
  99.   }
  100.   if($line=~/\<div style=\"float:left;font-size:\d+\%;color:\#\w+\"\>(.+)\<\/div\>/)
  101.   {
  102.    print XML "\t\t<level></level>\n";
  103.    print XML "\t</region>\n";
  104.   }
  105. }
  106. close(HTML);
  107. #last;
  108. }
  109. print XML "</root>\n";
  110. close(XML);
  111. ######################################
  112. ##
  113. ##  取图片
  114. ##
  115. ######################################
  116. foreach (%image)
  117. {
  118. if($_=~/gif/)
  119. {
  120.   
  121.   my $simage_head=$image_head;$simage_head=~s/\{file\}/$_/;
  122.   print $simage_head;
  123.   socket ($socket, AF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "problem creating socket: $!\n";
  124.   my $con_ok = connect ($socket, sockaddr_in($port, inet_aton($host))) or die "problem connecting to \"$host\", port $port: $!\n";
  125.   print "error " unless $con_ok;
  126.   open(IMAGE,">$image_path$_")  or die "error";
  127.   binmode(IMAGE);
  128.   syswrite($socket,$simage_head,length($simage_head),0);
  129.   my $image="";
  130.   while(1)
  131.   {
  132.    $buffer="";
  133.    sysread($socket,$buffer,1024 );
  134.    $image.=$buffer;
  135.    last if(length($buffer)==0);
  136.   }
  137.   print IMAGE substr($image,index($image,"GIF"));
  138.   close(IMAGE);close($socket);
  139. }
  140. }
  141. ######################################
  142. ##
  143. ## 删除网页
  144. ##
  145. system("del $path*.htm");
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP