网络实验室

 找回密码
 注册账户
查看: 2666|回复: 3

文章采集

[复制链接]
admin 发表于 2007-6-3 00:18:36 | 显示全部楼层 |阅读模式
这个题目有点大。
下面这个是我抓取某个网站的文章内容的。不过是针对性很强的,假如对方修改了一些代码,抓取就会发生变化了。
要做通用的,不容易啊!
放出来看看,希望对你有用。
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" >
  4. <head>
  5.     <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  6.     <meta name="author" content="Smiling Dolphin" />
  7.     <meta name="keywords" content="design, css, cascading, style, sheets, xhtml, graphic design, w3c, web standards, visual, display, java, javascript, c++, php, jsp, asp, py, pl" />
  8.     <meta name="description" content="my favorites language." />
  9.     <meta name="robots" content="all" />
  10.     <title>文章读取器,收集器 (用于收藏)</title>
  11.     <style type="text/css" title="currentStyle" media="screen">
  12.         body{font-size:14px;}
  13.         #aurl{width:60%;}
  14.         #content{background:#efefef;border:1px dashed #ccc;padding:5px;}
  15.         p{text-indent:2em;}
  16.     </style>
  17. </head>
  18. <body>
  19. <div><h2>无广告,可复制文章读取器</h2></div>
  20. <div>
  21. <form method="post" action="" onsubmit="return chkform(this)">
  22. 请输入文章地址:<input type="text" name="aurl" id="aurl" value="<?php
  23. if($_POST['aurl'])
  24. {
  25.     echo $_POST['aurl'];
  26. }else{
  27.     echo "http://www.dzwx.net/2006/Article/10262.html";
  28. }
  29. ?>" /> <input type="submit" name="submitbtn1" value="view" />
  30. </form>
  31. </div>
  32. <div id="content">
  33. <?php
  34. if($_POST['submitbtn1']=="view"){
  35.     $filename = "data/".preg_replace("/.*?(\d{1,8})\..*/","\\1",$str).".xml";
  36.     if(!file_exists($filename)){
  37.         $fp = @fopen($_POST['aurl'],"r");
  38.         if($fp){
  39.             while(!@feof($fp)){
  40.                 $content .= @fread($fp,4096);
  41.             }
  42.             @fclose($fp);
  43.             $article = array();
  44.             function parseArticle($str,$var='title'){
  45.                 global $article;
  46.                 $article[$var] = $str;
  47.             }
  48.             preg_replace("/<div id="artitleb">(.*?)<\/div>/ies", "parseArticle('\\1', 'title')", $content);
  49.             preg_match("/<div class="artitlec">.*?<\/div>/ies",$content,$introinfo);
  50.             preg_replace("/<a href="mailto\:.*?>(.*?)<\/a>/ies", "parseArticle('\\1', 'author')", $introinfo[0]);
  51.             preg_match("/<div class="artitled" id="ArticleBody">.*?<div id="artline">/ies", $content, $artbody);
  52.             $artbody = preg_replace("/<span style='text-align : center;DISPLAY: none;'>.*?<\/span>/is", "", $artbody[0]);
  53.             $artbody = preg_replace("/<div align=right style='color=gray'>.*?<div id="artline">/is", "", $artbody);
  54.             $artbody = strip_tags($artbody);
  55.             $artbody = preg_replace("/&nbsp;/","|",preg_replace("/\s/","",$artbody));
  56.             $article['content'] = "<p>".preg_replace("/\|{1,10}|  /","\n</p>\n<p>\n",$artbody)."\n</p>";
  57.             if(!is_dir("./data")) @mkdir("./data");
  58.             $xmlstr = '<?xml version="1.0" encoding="gb2312"?>'."\n";
  59.             $xmlstr.= '<article>'."\n";
  60.             $xmlstr.= '    <title>'.$article['title'].'</title>'."\n";
  61.             $xmlstr.= '    <link>'.$_POST['aurl'].'</link>'."\n";
  62.             $xmlstr.= '    <author>'.$article['author'].'</author>'."\n";
  63.             $xmlstr.= '    <content>'.<![CDATA[$article['content']]]>.'</content>'."\n";
  64.             $xmlstr.= '</article>';
  65.             if($lfp = @fopen($filename,"w")){
  66.                 @fwrite($lfp,$xmlstr);
  67.                 @fclose($lfp);
  68.             }
  69.         }
  70.     }else{
  71.         
  72.     }
  73. }
  74. ?>
  75. </div>
  76. <script language="javascript" type="text/javascript">
  77. function chkform(obj){
  78.     if(!obj.aurl.value)
  79.     {
  80.         alert('请输入文章的地址!');
  81.         return false;
  82.     }
  83.     return true;
  84. }
  85. function $(sname){
  86.     return document.getElementById(sname);
  87. }
  88. window.onload = function(){
  89.     if($("a_title"))
  90.         document.title = $("a_title").innerHTML + "  " + $("a_author").innerHTML;
  91. }
  92. </script>
  93. </body>
  94. </html>
复制代码
exhoo 发表于 2007-6-18 12:42:17 | 显示全部楼层

真的是不错,
taby 发表于 2007-6-18 16:51:15 | 显示全部楼层
遍地都是采集了 与时俱进
hahamy 发表于 2007-6-27 17:23:14 | 显示全部楼层
刚学PHP,看着头有点晕
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

黑屋|存档|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2024-4-29 07:37 , Processed in 0.527476 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表