curl的一个采集某小说站的实例

来源:互联网 发布:win10如何修改mac地址 编辑:程序博客网 时间:2024/05/17 06:40
  1. <?php
  2. function insert($href='',$pattern='',$novel='',$author='')
  3. {
  4. $href = "http://www.kenshuge.com/Html/Book/4/4410/Index.shtm";
  5. $hrefb = strrev($href);
  6. $hrefb = strstr($hrefb,'/');
  7. $hrefb = strrev($hrefb);
  8. $pattern = '/\<a\s+href="([^"]+)"\s*title="([^"]+)"\s*\>(.+?)\<\/a\>/i';
  9. $url = curl_init($href);
  10. curl_setopt($url,CURLOPT_RETURNTRANSFER,true);
  11. try{
  12. $pdo = New PDO('mysql:location=localhost;dbname=test','testuser','mytest');
  13. $pdo->query('set names utf8');
  14. }catch( PDOException $e ){
  15. echo $e->getMessage();
  16. }
  17. $string = curl_exec($url);
  18. $string = iconv('gbk', 'utf-8', $string);
  19. preg_match_all( $pattern, $string, $array );
  20. $stmt = $pdo->prepare("insert into novel (`novelName`,`author`,`chapterName`,`chapterinfo`,`chapterLink`) values(?,?,?,?,?) ;");
  21. $j = count($array[1]);
  22. echo '<table border="1" width="1250">';
  23. $novel = '异界全职业大师';
  24. $author = '庄毕凡';
  25. for ($i = 0; $i < $j; $i++) {
  26. $link = $hrefb.$array[1][$i];
  27. $info = $array[2][$i];
  28. $title = $array[3][$i];
  29. $stmt->bindParam(1, $novel) ;
  30. $stmt->bindParam(2, $author);
  31. $stmt->bindParam(3, $title);
  32. $stmt->bindParam(4, $info);
  33. $stmt->bindParam(5, $link);
  34. $rst = $stmt->execute();
  35. $color = $rst ? 'green' : 'red';
  36. echo '<tr><td>';
  37. echo '<font color="'.$color.'">';
  38. echo $link;
  39. echo '</font>';
  40. echo '</td><td>';
  41. echo '<font color="'.$color.'">';
  42. echo $info;
  43. echo '</font>';
  44. echo '</td><td>';
  45. echo '<font color="'.$color.'">';
  46. echo $title;
  47. echo '</font>';
  48. echo '</td></tr>';
  49. if (strstr($title,'大结局')!==false) {
  50. break;
  51. }
  52. }
  53. echo '</table>';
  54. curl_close($url);
  55. }
  56. insert();



看到CURL采集就写了个采集某站的小说列表的函数,带插入数据库
应该还行,结合Linux再扩展一下就可以做个类似的booksky.org那样的站了

视频地址:http://bbs.lampbrother.net/read-htm-tid-122008.html

阅读(107) | 评论(0) | 转发(0) |
0

上一篇:倾情巨献呕血力作:通过smarty谈学习方法,附送江湖绝学smarty3速成秘籍

下一篇:分享个小框架---MioPHP

相关热门文章
  • 欲说还休梦已阑
  • QTZ125塔机顶升液压油缸活塞断...
  • 找1.76传奇私服发布网她是在等...
  • 亿民乐2X
  • 出售!同款网分E5100A AGILENT...
  • IP Sec VPN与NAT破镜重圆
  • 网站导航
  • GoAgent图文设置教程
  • UT2.0正式版下载
  • tomcat6.0配置(含配置视频下载...
  • 大家都是用什么来管理hadoop集...
  • 网站被人挂了吗,添加了些程序...
  • Nginx如何保证不走宕机的那个...
  • 大家谈谈MYSQL客户端和服务器...
  • 以下代码运行后为何会输出5?...
给主人留下些什么吧!~~
评论热议