recherche
Maisonphp教程php手册PHP爬取糗事百科首页糗事

PHP爬取糗事百科首页糗事

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

说干就干!首先确定思路

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

没有什么难的

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

2、获取指定URL的HTML源码

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

代码为

 

<span new="" style="font-family:Times">// 获取对应链接的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;
}</span>
3、引入第三方文件’simple_html_dom.php‘来解析HTML

 

这里我没有能力使用正则表达式,就在网上海搜,终于找到这个,就像Java使用Jsoup(使用Jsoup解析滁州学院官网获取新闻列表)一样,具体参见BLOG

代码如下

 

<span new="" style="font-family:Times">function getFmlDataToDB() {
	$link = mysql_connect ( SAE_MYSQL_HOST_M . &#39;:&#39; . 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 ( &#39;set names utf8&#39; );
		// class=article block untagged mb15
		foreach ( $html->find ( &#39;div[class=article block untagged mb15]&#39; ) as $per ) {
			
			$z = null;
			$t = null;
			$w = null;
			$d = null;
			$p = null;
			$ds = null;
			$ps = null;
			
			// //作者
			$author = $per->find ( &#39;div[class=author]&#39; );
			if ($author != null) {
				$a = $author [0]->find ( &#39;a&#39; );
				$z = $a [1]->innertext;
			} else {
				$z = &#39;no author&#39;;
			}
			
			// 头像链接
			
			if ($author != null) {
				$icon = $author [0]->find ( &#39;a&#39; );
				$t = $icon [0]->src->innertext;
			} else {
				$t = &#39;...............&#39;;
			}
			
			// 文章内容
			$content = $per->find ( &#39;div[class=content]&#39; );
			$w = $content [0]->innertext;
			
			// 点赞数
			$vote1 = $per->find ( &#39;div[class=stats]&#39; );
			$vote2 = $vote1 [0]->find ( &#39;span[class=stats-vote]&#39; );
			$vote3 = $vote2 [0]->find ( &#39;i[class=number]&#39; );
			
			$d = $vote3 [0]->innertext;
			// 评论数
			$comments1 = $vote1 [0]->find ( &#39;span[class=stats-comments]&#39; );
			$comments2 = $comments1 [0]->find ( &#39;a[class=qiushi_comments]&#39; );
			$comments3 = $comments2 [0]->find ( &#39;i[class=number]&#39; );
			$p = $comments3 [0]->innertext;
			// 顶 数
			$up_down = $per->find ( &#39;div[class=stats-buttons bar clearfix]&#39; );
			
			$up_down1 = $up_down [0]->find ( &#39;ul&#39; );
			$li = $up_down1 [0]->find ( &#39;li&#39; );
			$up = $li [0]->find ( &#39;span[class=number hidden]&#39; );
			$ds = $up [0]->innertext;
			// 拍 数
			$down = $li [1]->find ( &#39;span[class=number hidden]&#39; );
			$ps = $down [0]->innertext;

		}
	} else {
		echo &#39;数据库链接KO&#39;;
	}
}</span>
这个代码写的有点纠结,我试了一下不能直接获取子节点的数据,只能从外层一层一层的剥开解析,如果有新的写法,我会更新,也请各位看官看看。

 

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

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

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

 

<span style="font-family:Microsoft">mysql_query ( &#39;set names utf8&#39; );</span>
核心代码如下:

 

 

<span style="font-family:Microsoft">			$sql = INSERT INTO `app_bmhjqs`.`db_fml` (`id`, `author`, `icon_url`, `content`, `vote`, `comments`, `up`, `down`) VALUES (NULL, &#39;$z&#39;, &#39;$t&#39;, &#39;$w&#39;, &#39;$d&#39;, &#39;$p&#39;, &#39;$ds&#39;, &#39;$ps&#39;);;
			// 解决乱码
			mysql_query ( &#39;set names utf8&#39; );
			$result = mysql_query ( $sql );</span>

这样一来,获取--->解析--->插入就完成了,效果就是运行一次PHP文件,数据库就添加了糗事百科首页上的糗事!我想可不可以写个定时器,每隔一定时间就运行一次代码,这一点在java我可以实现,在php我不会,毕竟是个没长毛的小鸟!百度吧。。。搜到这样的写法

 

 

<span new="" style="font-family:Times">// 定时器
// ignore_user_abort (); // run script. in background
// set_time_limit ( 0 ); // run script. forever
// $interval = 30; // do every 15 minutes..

// do {
// 	echo date ( &#39;Y-m-d H:i:s&#39;, time () );
// 	echo &#39;写入数据库&#39;;
// 	//getFmlDataToDB ();
	
// } while ( true );</span>
在文件里加上这样的代码,正好在学校断网前,发布到了SAE上,我没有测试!只能等到第二天来查看结果了!

 

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

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

 

 

 

 


Déclaration
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Vous avez un jeu croisé?
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Dreamweaver Mac

Dreamweaver Mac

Outils de développement Web visuel

PhpStorm version Mac

PhpStorm version Mac

Le dernier (2018.2.1) outil de développement intégré PHP professionnel

MantisBT

MantisBT

Mantis est un outil Web de suivi des défauts facile à déployer, conçu pour faciliter le suivi des défauts des produits. Cela nécessite PHP, MySQL et un serveur Web. Découvrez nos services de démonstration et d'hébergement.

Adaptateur de serveur SAP NetWeaver pour Eclipse

Adaptateur de serveur SAP NetWeaver pour Eclipse

Intégrez Eclipse au serveur d'applications SAP NetWeaver.

Version Mac de WebStorm

Version Mac de WebStorm

Outils de développement JavaScript utiles