PHP爬取糗事百科首页糗事_PHP教程

WBOY
Release: 2016-07-13 09:53:12
Original
1283 people have browsed it

PHP爬取糗事百科首页糗事

突然想获取一些网上的数据来玩玩,因为有SAE的MySql数据库,让它在那呆着没有什么卵用!于是就开始用PHP编写一个爬取糗事百科首页糗事的小程序,数据都保存在MySql中,岂不是很好玩!

说干就干!首先确定思路

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

没有什么难的

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

2、获取指定URL的HTML源码

这里我用的是curl函数,详细内容参见PHP手册

代码为

 

// 获取对应链接的HTMLCODE
function 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;
}
Copy after login
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';
	}
}
Copy after login
这个代码写的有点纠结,我试了一下不能直接获取子节点的数据,只能从外层一层一层的剥开解析,如果有新的写法,我会更新,也请各位看官看看。

4、创建数据库,将数据插入到数据库中

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

需要注意的就是编码格式,区要在执行语句前加上这样一句话

mysql_query ( 'set names utf8' );
Copy after login
核心代码如下:

			$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 );
Copy after login

这样一来,获取--->解析--->插入就完成了,效果就是运行一次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 );
Copy after login
在文件里加上这样的代码,正好在学校断网前,发布到了SAE上,我没有测试!只能等到第二天来查看结果了!

 

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

\

额滴神!受不鸟了,赶紧把定时器关掉了,写了个按钮触发事件!这样下去,数据库会被挤满的!

 

 

 

 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1004123.htmlTechArticlePHP爬取糗事百科首页糗事 突然想获取一些网上的数据来玩玩,因为有SAE的MySql数据库,让它在那呆着没有什么卵用!于是就开始用PHP编写一...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!