亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 1796 | 回復(fù): 1
打印 上一主題 下一主題

php實(shí)現(xiàn)的web采集神器 [復(fù)制鏈接]

論壇徽章:
1
程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-07-10 22:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-07-09 11:14 |只看該作者 |倒序?yàn)g覽
php實(shí)現(xiàn)的web采集神器,只需要通過(guò)簡(jiǎn)單配置,就可以采集任意沒(méi)有嚴(yán)格校驗(yàn)的站點(diǎn)
可以擴(kuò)展IP代理功能以及偽原創(chuàng)功能

[PHP]代碼
  1. <?php
  2. /**
  3. *  可以靈活配置使用的采集器
  4. *  作者:Rain
  5. *  創(chuàng)建時(shí)間:2015-02-03 15:17:30
  6. *  版本信息:V1.0
  7. */

  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. //數(shù)據(jù)庫(kù)的相關(guān)配置信息,請(qǐng)根據(jù)您的數(shù)據(jù)庫(kù)信息進(jìn)行配置
  10. define('DB_HOST', 'localhost');
  11. define('DB_USER', 'root');
  12. define('DB_PWD', 'test123456');
  13. define('DB_NAME', 'test_dbname');
  14. define('DB_CHARSET', 'utf8');
  15. define('TABLE_NAME', 'tb_book');
  16. //end

  17. //網(wǎng)站信息相關(guān)的配置,請(qǐng)根據(jù)具體需要采集的網(wǎng)站內(nèi)容信息進(jìn)行配置
  18. define('WEB_CHARSET', 'gbk');
  19. //變動(dòng)的參數(shù),使用%d進(jìn)行替換,只支持?jǐn)?shù)值形式的變動(dòng)
  20. define('WEB_LIST_URL', 'http://www.pcbookcn.com/book/1_%d.htm');
  21. //分頁(yè)的條數(shù)
  22. define('PAGE_COUNT', 14);
  23. //從哪個(gè)頁(yè)面開(kāi)始抓取
  24. define('PAGE_START', 1);
  25. //內(nèi)容頁(yè)的URL,使用正則模式,必須包含/,例如:/\/xuefu2008\/article\/details\/(\d)+/i
  26. define('WEB_CONTENT_URL_REG', '/\/book\/(\d)+\.htm/i');
  27. //網(wǎng)站域名HOST信息,不包含末尾的/,例如:http://blog.csdn.net
  28. define('WEB_HOST', 'http://www.pcbookcn.com');
  29. //列表頁(yè)內(nèi)容的精準(zhǔn)定位,用來(lái)大致抓取一個(gè)列表頁(yè)的內(nèi)容顯示模塊位置,使用正則進(jìn)行定位
  30. define('WEB_LIST_POSTION', '/book_name\.gif(.*?)<td\swidth="15\%"\snowrap>/i');
  31. //end

  32. //微調(diào)參數(shù),通常不修改也不會(huì)影響您的正常使用
  33. define('SLEEP_TIME', 1);
  34. define('IS_DEBUG', false);
  35. define('INSERT_DB', true);
  36. //內(nèi)容的輸出速度,單位:秒
  37. define('OUTPUT_SPEED', 1);
  38. //end

  39. //需要過(guò)濾刪除的文字,根據(jù)采集的網(wǎng)站類型進(jìn)行設(shè)置,不區(qū)分大小寫(xiě)
  40. $text_filter = array(
  41.     '- 中華電腦書(shū)庫(kù)' => '',
  42.     '_電腦電子書(shū)' => '',
  43.     '_電腦書(shū)籍' => '',
  44.     '下載' => '',
  45. );

  46. //表結(jié)構(gòu)映射的配置
  47. $table_mapping = array(
  48. //表字段名稱 => 獲取該字段的正則表達(dá)式,非空字段都必須在此設(shè)置映射關(guān)系,常量值請(qǐng)直接填寫(xiě)具體對(duì)應(yīng)的值,無(wú)需使用正則
  49.     'size' => '/軟件大小.*?000000>(.*?)<\/font>/i',
  50.     'logo' => 'http://www.94cto.com/index/uploads/images/20150105/0b8461910de101cc51a07684cdab797e.jpg',
  51.     'field1' => '/<title>(.*?)<\/title>/i',
  52.     'field2' => '/軟件簡(jiǎn)介.*?000000>(.*?)<\/font>/i',
  53.     'field3' => '1',
  54.     'field4' => '1',
  55.     'field5' => '1',
  56.     'field6' => '電子書(shū),計(jì)算機(jī),圖像,圖形',
  57.     'platform' => 'window/Linux',
  58.     'ishot' => '1',
  59.     'agreement' => '免費(fèi)',
  60.     'downurl' => '/(\/down\.asp\?id=.*?)"/i',
  61.     'istop' => '1',
  62. );
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  64. $ga = new Gather();
  65. $ga->run();

  66. class Gather
  67. {
  68.     public function __construct()
  69.     {
  70.         $this->init_check();
  71.     }

  72.     public function run()
  73.     {
  74.         global $table_mapping, $text_filter;

  75.         for ($page = PAGE_START; $page <= PAGE_COUNT; $page++)
  76.         {

  77.             $this->write('開(kāi)始采集列表第'.$page.'頁(yè)的內(nèi)容...');
  78.             $list_content = $this->get(sprintf(WEB_LIST_URL, $page));
  79.             if (empty($list_content))
  80.             {
  81.                 $this->write('抓取的列表頁(yè)的內(nèi)容為空,所以過(guò)濾掉');
  82.                 continue;
  83.             }

  84.             $list_content = str_replace("\r", '', $list_content);
  85.             $list_content = str_replace("\n", '', $list_content);

  86.             //精準(zhǔn)定位要抓取的模塊內(nèi)容
  87.             if (!preg_match(WEB_LIST_POSTION, $list_content, $list_search))
  88.             {
  89.                 $this->write('精準(zhǔn)匹配列表頁(yè)的內(nèi)容失敗,所以過(guò)濾掉');
  90.                 continue;
  91.             }
  92.             if (isset($list_search[1]))
  93.                 $list_content = $list_search[1];
  94.             else
  95.                 $list_content = $list_search[0];
  96.             //end

  97.             preg_match_all(WEB_CONTENT_URL_REG, $list_content, $match);
  98.             if (is_array($match[0]) && !empty($match[0]))
  99.             {
  100.                 $this->write('當(dāng)前的列表頁(yè)面,總共匹配到:'.count($match[0]).'個(gè)內(nèi)容頁(yè)');
  101.                 foreach ($match[0] as $val)
  102.                 {
  103.                     if (strpos($val, 'http:') === false)
  104.                     {
  105.                         if (substr($val, 0, 1) == '/')
  106.                             $val = WEB_HOST.$val;
  107.                         else
  108.                             $val = WEB_HOST.'/'.$val;
  109.                     }
  110.                     $web_content = $this->get($val);
  111.                     if (empty($web_content))
  112.                     {
  113.                         $this->write('抓取的內(nèi)容頁(yè)為空,所以過(guò)濾掉');
  114.                         continue;
  115.                     }

  116.                     $web_content = str_replace("\r", '', $web_content);
  117.                     $web_content = str_replace("\n", '【】', $web_content);

  118.                     $sql = "INSERT INTO ".TABLE_NAME."(".implode(', ', array_keys($table_mapping)).")VALUES(";
  119.                     foreach ($table_mapping as $field => $reg)
  120.                         $sql .= ':'.$field.',';
  121.                     $sql = substr($sql ,0, -1);
  122.                     $sql .= ')';

  123.                     if (IS_DEBUG)
  124.                         $this->write('執(zhí)行SQL '.$sql);

  125.                     $dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST;
  126.                     try {
  127.                         $dbh = new PDO($dsn, DB_USER, DB_PWD);
  128.                     } catch (PDOException $e) {
  129.                         $this->write( 'Connection failed: ' . $e->getMessage(), true);
  130.                     }
  131.                     $dbh->query("set names 'utf8'");
  132.                     $sth = $dbh->prepare($sql);

  133.                     foreach ($table_mapping as $field => $reg)
  134.                     {
  135.                         if (substr($reg, 0, 1) !=  '/')
  136.                         {
  137.                             $field = $reg;
  138.                         }
  139.                         else
  140.                         {
  141.                             if (!preg_match($reg, $web_content, $tmp_match))
  142.                             {
  143.                                 $this->write('對(duì)不起,匹配字段:'.$field.'失敗,過(guò)濾此記錄');
  144.                                 continue 2;
  145.                             }

  146.                             $field = $tmp_match[1];
  147.                             $field = $this->closetags($field);
  148.                              
  149.                             //刪除javascript腳本
  150.                             $field = preg_replace('/<script(.*?)>(.*?)<\/script>/i', '', $field);

  151.                             //將鏈接刪除
  152.                             $field = preg_replace('/<a(.*?)>(.*?)<\/a>/i', '${2}', $field);

  153.                             //圖片鏈接地址絕對(duì)地址化
  154.                             preg_match_all('/<img.*?src=("|\')+(.*?)("|\')+.*?>/i', $field, $img_match);
  155.                             if (isset($img_match[2]) && is_array($img_match[2]) && !empty($img_match[2]))
  156.                             {
  157.                                 foreach ($img_match[2] as $img_val)
  158.                                 {
  159.                                     if (strpos($img_val, 'http:') === false)
  160.                                     {
  161.                                         $new_val = $img_val;
  162.                                         if (substr($new_val, 0, 1) != '/')
  163.                                             $new_val = '/'.$img_val;
  164.                                         $new_val = WEB_HOST.$new_val;
  165.                                         $field = str_replace($img_val, $new_val, $field);
  166.                                     }
  167.                                 }
  168.                             }
  169.                             //end

  170.                             //針對(duì)HTML里面的pre的換行先做一個(gè)特殊處理
  171.                             $field = preg_replace('/<pre.*?>(.*?)<\/pre>/i', '<pre class="prettyprint">${1}</pre>', $field);
  172.                             preg_match_all('/<pre>(.*?)<\/pre>/i', $field, $pre_match);
  173.                             if (isset($pre_match[1]) && is_array($pre_match[1]) && !empty($pre_match[1]))
  174.                             {
  175.                                 foreach ($pre_match[1] as $pre_val)
  176.                                     $field = str_replace($pre_val, str_replace("【】", "\r\n", $pre_val), $field);
  177.                             }
  178.                             //end
  179.                         }

  180.                         //入庫(kù)之前,將對(duì)應(yīng)的換行符號(hào)都還原回來(lái)
  181.                         $field = str_replace('【】', "\r\n", $field);
  182.                         //文本的過(guò)濾和替換操作
  183.                         if (is_array($text_filter) && !empty($text_filter))
  184.                         {
  185.                             foreach ($text_filter as $tk => $tv)
  186.                                 $field = str_ireplace($tk, $tv, $field);
  187.                         }

  188.                         if (IS_DEBUG)
  189.                             $this->write('*'."\t".'字段:'.$field.'  值:'."\n****************************************************\n".$field."\n****************************************************");
  190.                         if ('downurl' == $field && stripos($field, 'http:') === false)
  191.                             if (substr($field, 0, 1) == '/')
  192.                                 $field = WEB_HOST.trim($field);
  193.                             else
  194.                                 $field = WEB_HOST.'/'.trim($field);
  195.                         $sth->bindValue(':'.$field, trim($field));
  196.                     }
  197.                     if (INSERT_DB)
  198.                         $sth->execute();
  199.                     $sth->closeCursor();

  200.                     $this->write( '休息,暫停'.SLEEP_TIME.'秒后繼續(xù)抓取...');
  201.                     sleep(SLEEP_TIME);
  202.                 }
  203.             }
  204.             else
  205.             {
  206.                 $this->write('列表頁(yè)面沒(méi)有抓取到內(nèi)容,所以過(guò)濾掉');
  207.             }
  208.         }
  209.         $this->write('', true);
  210.     }

  211.     protected function closetags($html)
  212.     {
  213.         // 不需要補(bǔ)全的標(biāo)簽
  214.         $arr_single_tags = array('meta', 'img', 'br', 'link', 'area');
  215.         // 匹配開(kāi)始標(biāo)簽
  216.         preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  217.         $openedtags = $result[1];
  218.         // 匹配關(guān)閉標(biāo)簽
  219.         preg_match_all('#</([a-z]+)>#iU', $html, $result);
  220.         $closedtags = $result[1];
  221.         // 計(jì)算關(guān)閉開(kāi)啟標(biāo)簽數(shù)量,如果相同就返回html數(shù)據(jù)
  222.         $len_opened = count($openedtags);
  223.         if (count($closedtags) == $len_opened) {
  224.         return $html;
  225.         }
  226.         // 把排序數(shù)組,將最后一個(gè)開(kāi)啟的標(biāo)簽放在最前面
  227.         $openedtags = array_reverse($openedtags);
  228.         // 遍歷開(kāi)啟標(biāo)簽數(shù)組
  229.         for ($i = 0; $i < $len_opened; $i++) {
  230.         // 如果需要補(bǔ)全的標(biāo)簽
  231.         if (!in_array($openedtags[$i], $arr_single_tags)) {
  232.         // 如果這個(gè)標(biāo)簽不在關(guān)閉的標(biāo)簽中
  233.         if (!in_array($openedtags[$i], $closedtags)) {
  234.         // 直接補(bǔ)全閉合標(biāo)簽
  235.         $html .= '</' . $openedtags[$i] . '>';
  236.         } else {
  237.         unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  238.         }
  239.         }
  240.         }
  241.     return $html;
  242.     }

  243.     protected function init_check()
  244.     {
  245.         if (!$this->check_curl_support())
  246.             $this->write('對(duì)不起,請(qǐng)先開(kāi)啟CURL的類庫(kù)的支持,否則無(wú)法執(zhí)行', true);
  247.         $this->check_mysql_connect();
  248.         $this->write('程序初始化檢查通過(guò),執(zhí)行后續(xù)的流程...');
  249.     }

  250.     private function get($url, $data = array())
  251.     {
  252.         $this->write('開(kāi)始執(zhí)行抓取: '.$url);
  253.         $ch = curl_init();
  254.         curl_setopt($ch, CURLOPT_URL, $url);
  255.         //curl_setopt($ch, CURLOPT_USERAGENT, "Baiduspider+(+http://www.baidu.com/search/spider.htm)");
  256.         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  257.         curl_setopt($ch, CURLOPT_HEADER, 0);
  258.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  259.         curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
  260.         $ret = curl_exec($ch);
  261.         $error = curl_error($ch);
  262.         curl_close($ch);
  263.         unset($ch);
  264.         if (!empty($error))
  265.         {
  266.             $this->write('程序抓取URL: '.$url.'發(fā)生錯(cuò)誤,錯(cuò)誤信息: '.$error);
  267.             return false;
  268.         }
  269.         if (WEB_CHARSET != 'utf-8')
  270.             $ret = iconv(WEB_CHARSET, 'utf-8', $ret);
  271.         return $ret;
  272.     }

  273.     //when check finish,mysql connect will auto close
  274.     private function check_mysql_connect()
  275.     {
  276.         $con = mysql_connect(DB_HOST, DB_USER, DB_PWD);
  277.         if (!is_resource($con))
  278.             $this->write('程序無(wú)法成功鏈接到數(shù)據(jù)庫(kù),具體的錯(cuò)誤信息:'.mysql_error(), true);
  279.         if (!mysql_select_db(DB_NAME, $con))
  280.             $this->write('程序無(wú)法鏈接到數(shù)據(jù)庫(kù): '.DB_NAME.',具體的錯(cuò)誤信息: '.mysql_error(), true);
  281.         mysql_close($con);
  282.     }

  283.     private function check_curl_support()
  284.     {
  285.         if (!extension_loaded('curl') || !function_exists('curl_init'))
  286.             return false;
  287.         return true;
  288.     }

  289.     private function write($str, $end = false)
  290.     {
  291.         if (PATH_SEPARATOR == ':')
  292.             echo $str,PHP_EOL,PHP_EOL;
  293.         else
  294.             echo iconv('UTF-8', 'GBK', $str),PHP_EOL,PHP_EOL;

  295.         if ($end)
  296.             die("program exit");

  297.         sleep(OUTPUT_SPEED);
  298.     }
  299. }
復(fù)制代碼

論壇徽章:
59
2015七夕節(jié)徽章
日期:2015-08-24 11:17:25ChinaUnix專家徽章
日期:2015-07-20 09:19:30每周論壇發(fā)貼之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38榮譽(yù)版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年紀(jì)念徽章
日期:2015-07-20 11:05:27IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-07-20 11:05:34操作系統(tǒng)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:36程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:40數(shù)據(jù)庫(kù)技術(shù)版塊每日發(fā)帖之星
日期:2015-07-20 11:05:432015年辭舊歲徽章
日期:2015-07-20 11:05:44
2 [報(bào)告]
發(fā)表于 2015-07-10 09:53 |只看該作者
這個(gè)到底是要采集什么。樓主只貼上代碼。為什么不把功能說(shuō)明白呢。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP