php抓取网页数据插入数据库(用PHP编写一个爬取事百科首页糗事!(图) )

优采云 发布时间: 2022-03-29 01:03

  php抓取网页数据插入数据库(用PHP编写一个爬取事百科首页糗事!(图)

)

  突然想弄点网上的资料玩玩,因为有SAE的MySql数据库,放在那里也没用!于是我开始用PHP写了一个小程序来爬取尴尬事百科首页的尴尬事。数据存储在MySql中,是不是很好玩!

  去做就对了!先确定思路

  获取HTML源代码--->解析HTML--->保存到数据库

  没什么难的

  1、创建 PHP 文件“getDataToDB.php”,

  2、获取指定URL的HTML源代码

  这里我使用了curl函数,详情见PHP手册

  代码是

  // 获取对应链接的HTMLCODEfunction GetHtmlCode($url) { $ch = curl_init (); // 初始化一个cur对象 curl_setopt ( $ch, CURLOPT_URL, $url ); // 设置需要抓取的网页 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); // 设置crul参数,要求结果保存到字符串中还是输出到屏幕上 curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 1000 ); // 设置链接延迟 $HtmlCode = curl_exec ( $ch ); // 运行curl,请求网页 return $HtmlCode;}

  3、引入第三方文件'simple_html_dom.php'解析HTML

  我这里没有使用正则表达式的能力,只是在网上搜索了一下,终于找到了这个,就像Java使用Jsoup一样(使用Jsoup解析滁州大学官网获取新闻列表),详情见BLOG

  代码显示如下

  function getFmlDataToDB() { $link = mysql_connect ( SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS ); // 获取源码 $html = str_get_html ( GetHtmlCode ( "http://www.qiushibaike.com/" ) ); if ($link) { mysql_select_db ( SAE_MYSQL_DB, $link ); mysql_query ( 'set names utf8' ); // class="article block untagged mb15" foreach ( $html->find ( 'div[class=article block untagged mb15]' ) as $per ) { $z = null; $t = null; $w = null; $d = null; $p = null; $ds = null; $ps = null; // //作者 $author = $per->find ( 'div[class=author]' ); if ($author != null) { $a = $author [0]->find ( 'a' ); $z = $a [1]->innertext; } else { $z = 'no author'; } // 头像链接 if ($author != null) { $icon = $author [0]->find ( 'a' ); $t = $icon [0]->src->innertext; } else { $t = '...............'; } // 文章内容 $content = $per->find ( 'div[class=content]' ); $w = $content [0]->innertext; // 点赞数 $vote1 = $per->find ( 'div[class=stats]' ); $vote2 = $vote1 [0]->find ( 'span[class=stats-vote]' ); $vote3 = $vote2 [0]->find ( 'i[class=number]' ); $d = $vote3 [0]->innertext; // 评论数 $comments1 = $vote1 [0]->find ( 'span[class=stats-comments]' ); $comments2 = $comments1 [0]->find ( 'a[class=qiushi_comments]' ); $comments3 = $comments2 [0]->find ( 'i[class=number]' ); $p = $comments3 [0]->innertext; // 顶 数 $up_down = $per->find ( 'div[class=stats-buttons bar clearfix]' ); $up_down1 = $up_down [0]->find ( 'ul' ); $li = $up_down1 [0]->find ( 'li' ); $up = $li [0]->find ( 'span[class=number hidden]' ); $ds = $up [0]->innertext; // 拍 数 $down = $li [1]->find ( 'span[class=number hidden]' ); $ps = $down [0]->innertext; } } else { echo '数据库链接KO'; }}

  这段代码写起来有点纠结。我试过了,不能直接获取子节点的数据。我只能从外层一层一层剥离解析。如果有新的写法,我会更新的,敬请关注。.

  4、创建数据库,向数据库中插入数据

  这里我在 SAE 中使用 MySQL。具体的连接方,请参见使用 PHP 连接 SAE 中的 MySql 数据库

  需要注意编码格式,区域要在执行语句前加这么一句

  mysql_query ( 'set names utf8' );

  核心代码如下:

   $sql = "INSERT INTO `app_bmhjqs`.`db_fml` (`id`, `author`, `icon_url`, `content`, `vote`, `comments`, `up`, `down`) VALUES (NULL, '$z', '$t', '$w', '$d', '$p', '$ds', '$ps');"; // 解决乱码 mysql_query ( 'set names utf8' ); $result = mysql_query ( $sql );

  这样,获取--->解析--->插入就完成了,效果就是运行PHP文件一次,数据库就会在尴尬事百科首页添加尴尬事!我想知道是否可以编写一个计时器来每隔一定时间运行代码。我可以在java中做到这一点,但我不能在php中。毕竟是没有毛的鸟!百度一下。. . 找到这个拼写

  // 定时器// ignore_user_abort (); // run script. in background// set_time_limit ( 0 ); // run script. forever// $interval = 30; // do every 15 minutes..// do {// echo date ( 'Y-m-d H:i:s', time () );// echo '写入数据库';// //getFmlDataToDB (); // } while ( true );

  文件中加了这么一段代码,就在学校断网之前,在SAE上发表过,我没有测试!只需要等到第二天才能看到结果!

  今天早上,我迫不及待地打开电脑,打开了SAE数据库。情况如下:

  

  额头神!实在受不了了,赶紧关掉定时器,写了个按钮触发事件!如果这样下去,数据库会很拥挤!

  好了,PHP爬虫的尴尬事百科首页的尴尬事都搞定了

  如果你觉得这个博客对你有帮助,请给它一个赞!

  

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线