PHP实现小偷程序实例

2025-05-29 0 51

为什么使用“小偷程序”?

远程抓取文章资讯或商品信息是很多企业要求程序员实现的功能,也就是俗说的小偷程序。其最主要的优点是:解决了公司网编繁重的工作,大大提高了效率。只需要一运行就能快速的抓取别人网站的信息。

小偷程序”在哪里运行?

小偷程序” 应该在 windows 下的 dos或 linux 下通过 php 命令运行为最佳,因为,网页运行会超时。

比如图(windows 下 dos 为例):

PHP实现小偷程序实例

小偷程序”的实现

这里主要通过一个实例来讲解,我们来抓取下“华强电子网”的资讯信息,请先看观察这个链接 http://www.hqew.com/info-c10.html,当您打开这个页面的时候发现这个页面会发现一些现象:
1、资讯列表有 500 页(2012-01-03);
2、每页的 url 链接都有规律,比如:第1页为http://www.hqew.com/info-c10-1.html;第2页为http://www.hqew.com/info-c10-2.html;……第500页为http://www.hqew.com/info-c10-500.html;

3、由第二点就可以知道,“华强电子网” 的资讯是伪静态或者是生成的静态页面

其实,基本上大部分的网站都有这样的规律,比如:中关村在线、慧聪网、新浪、淘宝……。

这样,我们可以通过这样的思路来实现页面内容的抓取:
1、先获取文章列表页内容;
2、根据文章列表页内容循环获取文章的 url 地址;
3、根据文章的 url 地址获取文章的详细内容

这里,我们主要抓取资讯页里面的:标题(title)、发布如期(date)、作者(author)、来源(source)、内容(content)

“华强电子网”资讯抓取

首先,先建数据表结构,如下所示:

?

1

2

3

4

5

6

7

8
create table `article`.`article` (

`id` mediumint( 8 ) unsigned not null auto_increment primary key ,

`title` varchar( 255 ) character set utf8 collate utf8_general_ci not null ,

`date` varchar( 50 ) not null ,

`author` varchar( 100 ) character set utf8 collate utf8_general_ci not null ,

`source` varchar( 100 ) character set utf8 collate utf8_general_ci not null ,

`content` text not null

) engine = myisam character set utf8 collate utf8_general_ci;

抓取程序:

  1. <?php
  2. /**
  3. *抓取“华强电子网”资讯程序
  4. *authorlee.
  5. *lastmodify$date:2012-1-315:39:35$
  6. */
  7. header('content-type:text/html;charset=utf-8');
  8. $mysqli=newmysqli('localhost','root','1715544','article');#数据库连接,请手动修改您自己的数据库信息
  9. $mysqli->set_charset('utf8');#设置数据库编码
  10. functiondata($url){
  11. global$mysqli;
  12. $result=file_get_contents($url);#$result获取url链接内容(注意:这里是文章列表链接)
  13. $pattern='/<li><spanclass="box_r">.+</span><ahref="([^"]+)"title=".+">.+</a></li>/usi';#取得文章url的匹配正则
  14. preg_match_all($pattern,$result,$arr);#把文章列表url分配给数组$arr(二维数组)
  15. foreach($arr[1]as$val){
  16. $val='http://www.hqew.com'.$val;#真实文章url地址
  17. $re=file_get_contents($val);#$re为文章url的内容
  18. $pa='/<divid="article">s+<h1>(.+)</h1>s+<pid="article_extinfo">s+发布:s+(.+)s+|s+作者:s+(.+)s+|s+来源:s+(.*?)s+<spanstyle="display:none">.+<divid="article_body">s*(.+)s+</div>s+</div><!–articleend–>/usi';#取得文章内容的正则
  19. preg_match_all($pa,$re,$array);#把取到的内容分配到数组$array
  20. $content=trim($array[5][0]);
  21. $con=array(
  22. 'title'=>mysqlstring($array[1][0]),
  23. 'date'=>mysqlstring($array[2][0]),
  24. 'author'=>mysqlstring(stripauthortag($array[3][0])),
  25. 'source'=>mysqlstring($array[4][0]),
  26. 'content'=>mysqlstring(stripcontenttag($content))
  27. );
  28. $sql="insertintoarticle(title,date,author,source,content)values('{$con['title']}','{$con['date']}','{$con['author']}','{$con['source']}','{$con['content']}')";
  29. $row=$mysqli->query($sql);#添加到数据库
  30. if($row){
  31. echo'addsuccess!';
  32. }else{
  33. echo'addfailed!';
  34. }
  35. }
  36. }
  37. /**
  38. *stripofficetag($v)对文章内容进行过滤,比如:去掉文章中的链接,过滤掉没用的html标签……
  39. *@paramstring$v
  40. *@returnstring
  41. */
  42. functionstripcontenttag($v){
  43. $v=str_replace('<p></p>','',$v);
  44. $v=str_replace('<p/>','',$v);
  45. $v=preg_replace('/<ahref=".+"target="_blank"><strong>(.+)</strong></a>/usi','',$v);
  46. $v=preg_replace('%(<spans*[^>]*>(.*)</span>)%usi','',$v);
  47. $v=preg_replace('%(s+class="mso[^"]+")%si','',$v);
  48. $v=preg_replace('%(style="[^"]*mso[^>]*)%si','',$v);
  49. $v=preg_replace('/<b></b>/','',$v);
  50. return$v;
  51. }
  52. /**
  53. *striptitletag($title)对文章标题进行过滤
  54. *@paramstring$v
  55. *@returnstring
  56. */
  57. functionstripauthortag($v){
  58. $v=preg_replace('/<ahref=".+"target="_blank">(.+)</a>/usi','',$v);
  59. return$v;
  60. }
  61. /**
  62. *mysqlstring($str)过滤数据
  63. *@paramstring$str
  64. *@returnstring
  65. */
  66. functionmysqlstring($str){
  67. returnaddslashes(trim($str));
  68. }
  69. /**
  70. *init($min,$max)入口程序方法,从$min页开始取,到$max页结束
  71. *@paramint$min从1开始
  72. *@paramint$max
  73. *@returnstring返回url地址
  74. */
  75. functioninit($min=1,$max){
  76. for($i=$min;$i<=$max;$i++){
  77. data("http://www.hqew.com/info-c10-{$i}.html");
  78. }
  79. }
  80. init(1,500);//程序入口,从第一页开始抓,抓取500页
  81. ?>

通过上面的程序,就可以实现抓取华强电子网的资讯信息。

入口方法 init($min, $max) 如果想抓取 1-500 页面内容,那么 init(1, 500) 即可!这样,用不了多长时间,华强电子网的资讯就会全部抓取到数据库里面了。^_^

执行界面:

PHP实现小偷程序实例

数据库:

PHP实现小偷程序实例

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现小偷程序实例 https://www.kuaiidc.com/96181.html

相关文章

发表评论
暂无评论