当前位置:Gxlcms > PHP教程 > 利用curl,正则表达式做的一个php蜘蛛抓取器

利用curl,正则表达式做的一个php蜘蛛抓取器

时间:2021-07-01 10:21:17 帮助过:22人阅读

凤网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)

    (https://img.gxlcms.com/[^<^>]*?)<\/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)(.*?)https://img.gxlcms.com/[^/',
  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. //链接
  89. 'tag0' => '/(?is).*?https://img.gxlcms.com/[^<^>]*?<\/a>.*?<\/div>/',
  90. //标题
  91. 'tag1' => '/(?is).*?(https://img.gxlcms.com/[^<^>]*?)<\/a>.*?<\/div>/',
  92. 'tag1_arp' => array(
  93. array(
  94. '/(?is)\(组图\)/',
  95. '/(?is)\(图\)/',
  96. '/(?is)\(图\.\./',
  97. '/(?is)\(组图\.\./',
  98. '/(?is)\./',
  99. '/(?is)(《|》)/',
  100. ),
  101. array(
  102. '', '', '', '', '', '',
  103. )
  104. ),
  105. );
  106. $list_map = array(
  107. 'url' => 'tag0',
  108. 'title' => 'tag1',
  109. );
  110. $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="(https://img.gxlcms.com/[^"]*?)"/',
  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. '

    https://www.gxlcms.com/$1

  37. https://www.gxlcms.com/$1

  38. $3

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

    (https://img.gxlcms.com/[^<^>]*?)<\/p>/',

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

    https://www.gxlcms.com/$1

    ',
  48. '',
  49. )
  50. ),
  51. 'strip' => array('tag1' => ''),
  52. //网名
  53. 'tag3' => '/(?is)(https://img.gxlcms.com/[^<^>]*?)<\/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. //链接
  71. 'tag0' => '/(?is).*?<\/span>https://img.gxlcms.com/[^<^>]*?<\/a><\/div>/',
  72. 'tag0_brp' => array(
  73. array(
  74. '/(?is)\.htm/',
  75. ),
  76. array(
  77. '.xml'
  78. )
  79. ),
  80. //标题
  81. 'tag1' => '/(?is).*?<\/span>(https://img.gxlcms.com/[^<^>]*?)<\/a><\/div>/',
  82. 'tag1_arp' => array(
  83. array(
  84. '/(?is)\(图\)/',
  85. '/(?is)\"/',
  86. '/(?is)独家:/',
  87. '/(?is)独家:/',
  88. '/(?is)(《|》)/',
  89. ),
  90. array(
  91. '', '', '', '', '',
  92. )
  93. ),
  94. );
  95. $list_map = array(
  96. 'url' => 'tag0',
  97. 'title' => 'tag1',
  98. );
  99. $site_list_sub = array();

人气教程排行