search
HomeBackend DevelopmentPHP TutorialPHP implementation of thief program example
PHP implementation of thief program exampleMay 31, 2018 am 09:35 AM
phpExampleprogram

本篇文章主要介绍了PHP实现小偷程序实例,实现了抓取网页咨询和商品信息的功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

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

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

“小偷程序”在哪里运行?

“小偷程序” 应该在 Windows 下的 DOS或 Linux 下通过 PHP 命令运行为最佳,因为,网页运行会超时。

比如图(Windows 下 DOS 为例):

“小偷程序”的实现

这里主要通过一个实例来讲解,我们来抓取下“华强电子网”的资讯信息,请先看观察这个链接 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)

“华强电子网”资讯抓取

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

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;

 抓取程序:
 

<?php 
/** 
* 抓取“华强电子网”资讯程序 
* author Lee. 
* Last modify $Date: 2012-1-3 15:39:35 $ 
*/ 
header(&#39;Content-Type:text/html;Charset=utf-8&#39;); 
$mysqli = new mysqli(&#39;localhost&#39;, &#39;root&#39;, &#39;1715544&#39;, &#39;article&#39;); # 数据库连接,请手动修改您自己的数据库信息 
$mysqli->set_charset(&#39;UTF8&#39;); # 设置数据库编码 
function data($url) { 
  global $mysqli; 
  $result = file_get_contents($url); # $result 获取 url 链接内容(注意:这里是文章列表链接) 
  $pattern = &#39;/<li><span class="box_r">.+<\/span><a href="([^"]+)" title=".+" >.+<\/a><\/li>/Usi&#39;; # 取得文章 url 的匹配正则 
  preg_match_all($pattern, $result, $arr); # 把文章列表 url 分配给数组$arr(二维数组) 
  foreach ($arr[1] as $val) { 
    $val = &#39;http://www.hqew.com&#39; . $val; # 真实文章 url 地址 
    $re = file_get_contents($val); # $re 为文章 url 的内容 
    $pa = &#39;/<p id="article">\s+<h1>(.+)<\/h1>\s+<p id="article\_extinfo">\s+发布:\s+(.+)\s+\|\s+作者:\s+(.+)\s+\|\s+来源:\s+(.*?)\s+<span style="display:none" >.+<p id="article_body">\s*(.+)\s+<\/p>\s+<\/p><!--article end-->/Usi&#39;; # 取得文章内容的正则 
    preg_match_all($pa, $re, $array); # 把取到的内容分配到数组 $array 
    $content = trim($array[5][0]);  
    $con = array( 
        &#39;title&#39;=>mysqlString($array[1][0]), 
        &#39;date&#39;=>mysqlString($array[2][0]),  
        &#39;author&#39;=>mysqlString(stripAuthorTag($array[3][0])), 
        &#39;source&#39;=>mysqlString($array[4][0]),  
        &#39;content&#39;=>mysqlString(stripContentTag($content)) 
      ); 
    $sql = "INSERT INTO article(title,date,author,source,content) VALUES (&#39;{$con[&#39;title&#39;]}&#39;,&#39;{$con[&#39;date&#39;]}&#39;,&#39;{$con[&#39;author&#39;]}&#39;,&#39;{$con[&#39;source&#39;]}&#39;,&#39;{$con[&#39;content&#39;]}&#39;)"; 
    $row = $mysqli->query($sql); # 添加到数据库 
    if ($row) { 
      echo &#39;add success!&#39;; 
    } else { 
      echo &#39;add failed!&#39;; 
    } 
  } 
} 
/** 
 * stripOfficeTag($v) 对文章内容进行过滤,比如:去掉文章中的链接,过滤掉没用的 HTML 标签…… 
 * @param string $v 
 * @return string 
 */ 
function stripContentTag($v){ 
  $v = str_replace(&#39;<p> </p>&#39;, &#39;&#39;, $v); 
  $v = str_replace(&#39;<p />&#39;, &#39;&#39;, $v); 
  $v = preg_replace(&#39;/<a href=".+" target="\_blank"><strong>(.+)<\/strong><\/a>/Usi&#39;, &#39;\1&#39;, $v); 
  $v = preg_replace(&#39;%(<span\s*[^>]*>(.*)</span>)%Usi&#39;, &#39;\2&#39;, $v); 
  $v = preg_replace(&#39;%(\s+class="Mso[^"]+")%si&#39;, &#39;&#39;, $v); 
  $v = preg_replace(&#39;%( style="[^"]*mso[^>]*)%si&#39;, &#39;&#39;, $v); 
  $v = preg_replace(&#39;/<b><\/b>/&#39;, &#39;&#39;, $v); 
  return $v; 
} 
 
/** 
 * stripTitleTag($title) 对文章标题进行过滤 
 * @param string $v 
 * @return string 
 */ 
function stripAuthorTag($v) { 
  $v = preg_replace(&#39;/<a href=".+" target="\_blank">(.+)<\/a>/Usi&#39;, &#39;\1&#39;, $v); 
  return $v; 
} 
 
/** 
 * mysqlString($str) 过滤数据 
 * @param string $str 
 * @return string 
 */ 
function mysqlString($str) { 
  return addslashes(trim($str)); 
} 
 
/** 
 * init($min, $max) 入口程序方法,从 $min 页开始取,到 $max 页结束 
 * @param int $min 从 1 开始 
 * @param int $max 
 * @return string 返回 URL 地址 
 */ 
function init($min=1, $max) { 
  for ($i=$min; $i<=$max; $i++) { 
    data("http://www.hqew.com/info-c10-{$i}.html"); 
  } 
} 
init(1, 500); // 程序入口,从第一页开始抓,抓取500页 
?>

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

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

执行界面:

数据库:

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHPmysqli批量执行多条语句的方法

如何实现php删除固定路径下文件夹与文件

PHP unlink与rmdir删除目录内文件如何实现

The above is the detailed content of PHP implementation of thief program example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools