利用curl,Regex做的一個php蜘蛛抓取器

來源:互聯網
上載者:User
鳳網fcms內容管理系統
get.php 抓取架構,對網頁內容的分析處理並進行相關替換
std.php 通用正則
news_67_com.php 對http://news.67.com 的抓取分析器
先抓列表,再抓內容頁。
還欠缺監控,統計,錯誤處理功能。個人覺得還是比較好玩。
  1. include_once dirname(__FILE__) . '/std.php';
  2. $site = array(
  3. 'aname' => '中國娛樂網',
  4. 'domain' => 'news.67.com',
  5. 'dirname' => '目錄名稱,用於匹配基於目錄不同的本文',
  6. 'gettype' => 'default',
  7. //擷取主檔案
  8. 'creg' => '/(?si)(.*?)\<\!--文章 end-->/',
  9. 'code' => 'utf-8',
  10. 'sub' => '擷取子目錄正則',
  11. 'content' => 'tag1',
  12. 'img_upload'=> array('tag1' => ''),
  13. //下一頁
  14. 'reg_next' => '/(?is)下一頁\>\><\/a>/',
  15. 'key0' => '/(?is)/',
  16. 'key0_ap' => array(array(',', '|'), ' '),
  17. 'tag0' => '/(?is)

    ([^<^>]*?)<\/h1>/',
  18. 'tag0_arp'=> array(
  19. array(
  20. '/(?is)\(組圖\)/',
  21. '/(?is)\(圖\)/',
  22. '/(?is)\(圖\.\./',
  23. '/(?is)\(組圖\.\./',
  24. '/(?is)\./',
  25. '/(?is)(《|》)/',
  26. ),
  27. array(
  28. '', '', '', '', '', '',
  29. )
  30. ),
  31. 'tag1' => '/(?is)(.*?)/',
  32. 'tag1_brp'=> array(
  33. array(
  34. '/(?is)(.*?)/',
  35. '/(?is)\(.*?\)/',
  36. '/(?is)\s*

    .*?(.*?)<\/p>\s*/',

  37. '/(?is)\s*

    \s*/',

  38. '/(?is)\s*

    \s*/',

  39. '/(?is) /',
  40. '/(?is)
    /',
  41. '/(?is)\s*

    \s*/',

  42. '/(?is)\s*

    \s*/',

  43. '/(?is)\s*\s*/',
  44. '/(?is)\s*<\/center>\s*/',
  45. '/(?is)\s*

    \s*/',

  46. ),
  47. array(
  48. '', '', '

    ', '

    ', '

    ', '', '', '

    ', '

    ', '

    ', '

    ', '

    '

  49. ),
  50. ),
  51. 'tag1_arp'=> array(
  52. array(
  53. '/(?is)

    <\/p>/',

  54. '/(?is)<\/strong>/'
  55. ),
  56. array(
  57. '', ''
  58. ),
  59. ),
  60. 'strip' => array('tag1' => ''),
  61. 'tag2' => '/(?is)導讀:\s*(.*?)\s*<\/div>/',
  62. 'tag2_arp'=> array(
  63. array(
  64. '/(?is) /'
  65. ),
  66. array(
  67. ''
  68. ),
  69. ),
  70. 'tag3' => '/(?is)(中國娛樂網)/',
  71. 'tag4' => '/(?is)日期:(\d+-\d+-\d+ \d+:\d+:\d+).*?<\/div>/',
  72. );
  73. $map = array(
  74. 'tag' => 'key0',
  75. 'title' => 'tag0',
  76. 'content' => 'tag1',
  77. 'summary'=> 'tag2',
  78. 'source' => 'tag3',
  79. 'pub_date' => 'tag4',
  80. );
  81. $site_list = array(
  82. 'aname' => '中國娛樂網',
  83. 'domain' => 'www.67.com',
  84. 'gettype' => 'default',
  85. 'creg' => '/(?si)(.*?)/',
  86. 'code' => 'gbk',
  87. 'reg_next' => '/(?si)
  88. 下一頁<\/a><\/li>/',
  89. //連結
  90. 'tag0' => '/(?is).*?[^<^>]*?<\/a>.*?<\/div>/',
  91. //標題
  92. 'tag1' => '/(?is).*?([^<^>]*?)<\/a>.*?<\/div>/',
  93. 'tag1_arp'=> array(
  94. array(
  95. '/(?is)\(組圖\)/',
  96. '/(?is)\(圖\)/',
  97. '/(?is)\(圖\.\./',
  98. '/(?is)\(組圖\.\./',
  99. '/(?is)\./',
  100. '/(?is)(《|》)/',
  101. ),
  102. array(
  103. '', '', '', '', '', '',
  104. )
  105. ),
  106. );
  107. $list_map = array(
  108. 'url' => 'tag0',
  109. 'title' => 'tag1',
  110. );
  111. $site_list_sub = array();
複製代碼
  1. global $std;
  2. $std = array(
  3. 'url' => '[0-9a-zA-Z\.\:\-\/%_#;&]+',
  4. 'img' => '/(?is)/',
  5. );
複製代碼
  1. /**
  2. * test.php
  3. *
  4. * @author xzfred
  5. * @copyright 2009 fengone.com
  6. * @created 2010-12-07 .
  7. * @version $Id: php.php 3 2008-10-10 07:49:21Z fred $
  8. * SVNPath $HeadURL: http://192.168.0.16/svn/vim/skeletons/php.php $
  9. */
  10. /*
  11. include_once "std.php";
  12. include_once "lady_163_com.php";
  13. */
  14. include_once $GLOBALS['g_dir_core'] . "get.php";
  15. //================================================================================
  16. include_once DIR_HOST_TAG . '/tuku_ent_china_com.php';
  17. $obj = new FcHtmlParse($site);
  18. $c = $obj->parse(file_get_contents("http://tuku.ent.china.com/fun/html/2011-08-23/181703.xml"));
  19. echo "\n\n\n ===================\n";
  20. echo $c['field']['tag1'][0];
  21. echo "\n\n\n ===================\n";
  22. var_dump($c);
  23. exit();
  24. //列表測試
  25. $obj = new FcHtmlParse($site_list);
  26. $c = $obj->parse(file_get_contents("http://tuku.ent.china.com/fun/html/3569_1.html"));
  27. var_dump($c);
  28. exit();
  29. /*
  30. $obj = new FcHtmlGet($site);
  31. $c = $obj->getPage('http://star.pclady.com.cn/entertainment/ss/1106/703240.html');
  32. var_dump($c);
  33. $obj = new FcHtmlGet($site);
  34. $c = $obj->getPage('http://star.pclady.com.cn/entertainment/ss/1106/703240.html');
  35. var_dump($c);
  36. $obj = new FcHtmlParse($site);
  37. $img_obj = new FcHtmlImgUpload($site);
  38. $data = file_get_contents("e:/b.html");
  39. $c = $obj->parse($data);
  40. $ic = $img_obj->upload($c['tag']['tag1'][0]);
  41. var_dump($ic);
  42. $data = file_get_contents("e:/a.html");
  43. $c = $obj->parse($data);
  44. $ic = $img_obj->upload($c['tag']['tag1'][0]);
  45. var_dump($ic);
  46. */
  47. //var_dump($c['tag']['tag1']);
複製代碼
  1. include_once $GLOBALS['g_dir_core'] . 'host/std.php';
  2. $site = array(
  3. 'aname' => '中華網娛樂圖庫',
  4. 'domain' => 'tuku.ent.china.com',
  5. 'dirname' => '目錄名稱,用於匹配基於目錄不同的本文',
  6. 'gettype' => 'default',
  7. //擷取主檔案
  8. 'creg' => '/(?si)(.*?<\/list>)/',
  9. 'code' => 'utf-8',
  10. 'sub' => '擷取子目錄正則',
  11. 'content' => 'tag1',
  12. 'img_upload'=> array('tag1' => ''),
  13. //下一頁
  14. 'reg_next' => '/(?is)下一頁<\/a>/',
  15. 'key0' => '/(?is)/',
  16. 'key0_ap' => array(array(',', '|'), ' '),
  17. 'tag0' => '/(?is)title="([^"]*?)"/',
  18. 'tag0_arp' => array(
  19. array(
  20. '/(?is)\(圖\)/',
  21. '/(?is)\"/',
  22. '/(?is)獨家:/',
  23. '/(?is)獨家:/',
  24. '/(?is)(《|》)/',
  25. ),
  26. array(
  27. '', '', '', '', '',
  28. )
  29. ),
  30. 'tag1' => '/(?is)(.*?)<\/list>/',
  31. 'tag1_brp'=> array(
  32. array(
  33. '/(?is)\s*\s*/'
  34. ),
  35. array(
  36. '

  37. $1

  38. $3

    '
  39. )
  40. ),
  41. 'tag1_arp'=> array(
  42. array(
  43. '/(?is)

    ([^<^>]*?)<\/p>/',

  44. '/(?is)\<br\/\>/',
  45. ),
  46. array(
  47. '

    $1

    ',
  48. '',
  49. )
  50. ),
  51. 'strip' => array('tag1' => ''),
  52. //網名
  53. 'tag3' => '/(?is)([^<^>]*?)<\/span>/',
  54. 'tag4' => '/(?is)(中華網)/'
  55. );
  56. $map = array(
  57. 'tag' => 'key0',
  58. 'title' => 'tag0',
  59. 'content' => 'tag1',
  60. 'author' => 'tag3',
  61. 'source' => 'tag4'
  62. );
  63. $site_list = array(
  64. 'aname' => '中華網娛樂圖庫',
  65. 'domain' => 'tuku.ent.china.com',
  66. 'gettype' => 'default',
  67. 'creg' => '/(?si)(.*?)<\/div>/',
  68. 'code' => 'utf-8',
  69. 'reg_next' => '/(?si)
  70. 下一頁<\/a><\/li>/',
  71. //連結
  72. 'tag0' => '/(?is).*?<\/span>[^<^>]*?<\/a><\/div>/',
  73. 'tag0_brp'=> array(
  74. array(
  75. '/(?is)\.htm/',
  76. ),
  77. array(
  78. '.xml'
  79. )
  80. ),
  81. //標題
  82. 'tag1' => '/(?is).*?<\/span>([^<^>]*?)<\/a><\/div>/',
  83. 'tag1_arp' => array(
  84. array(
  85. '/(?is)\(圖\)/',
  86. '/(?is)\"/',
  87. '/(?is)獨家:/',
  88. '/(?is)獨家:/',
  89. '/(?is)(《|》)/',
  90. ),
  91. array(
  92. '', '', '', '', '',
  93. )
  94. ),
  95. );
  96. $list_map = array(
  97. 'url' => 'tag0',
  98. 'title' => 'tag1',
  99. );
  100. $site_list_sub = array();
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.