本文主要是介绍使用CURL构建爬虫,抓取百度百科内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?phprequire 'func.curl.php';require 'func.mysql.php';//CURLif (isset($_GET['action'])){$_key=$_POST['key'];$_ch=curl_init();curl_setopt($_ch,CURLOPT_URL,"http://baike.baidu.com/search?word=".$_key);curl_setopt($_ch,CURLOPT_RETURNTRANSFER,1);$_output=curl_exec($_ch);curl_close($_ch);if(preg_match_all('/(http\:\/\/baike\.baidu\.com.*)\"\s/',$_output,$_url)){$_res=curl($_url[1]);$_title=array();$_content=array();for ($_i=0;$_i<count($_res);$_i++){if (preg_match_all('/lemmaTitleH1\"\>(.*?)\s*\<\/div\>/',$_res[$_i],$_arr)){//标题$_title[$_i]=strip_tags($_arr[1][0]);if (preg_match_all('/para\"\>(.*?)\s*\<\/div\>/',$_res[$_i],$_arr)){//内容$_content[$_i][0]=strip_tags($_arr[1][0]);$_content[$_i][1]=strip_tags($_arr[1][1]);//插入数据库inserInfo($_title[$_i],$_content[$_i][0],$_content[$_i][1],$_key);}}}}else{exit('无匹配结果');}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>模拟搜索引擎</title>
<link type="text/css" rel="stylesheet" href="curl.css"/>
</head>
<body><div id="header"><form method="post" action="?action=search"><input type="text" name="key" value="请输入关键字" class="text"/><input type="submit" name="submit" value="搜索" class="submit"/></form></div><div id="content"><table><?php for ($i=0;$i<count($_title);$i++){?><tr><td class="title"><a href="<?php echo $_url[1][$i];?>" target="_blank"><?php echo $_title[$i];?></a></td></tr><tr><td class="content"><?php echo $_content[$i][0]?></td></tr><?php }?></table></div>
<script type="text/javascript">var key=document.getElementsByName('key')[0];key.οnfοcus=function(){this.value='';};key.οnblur=function(){if(this.value==""){this.value="请输入关键字";}};
</script>
</body>
</html>
func.curl.php的代码:
<?php
//curl批处理函数
function curl($connomains){$mh = curl_multi_init();foreach ($connomains as $i => $url) {$conn[$i] = curl_init("$url");curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);curl_multi_add_handle ($mh,$conn[$i]);}// start performing the requestdo {$mrc = curl_multi_exec($mh, $active);} while ($mrc == CURLM_CALL_MULTI_PERFORM);while ($active and $mrc == CURLM_OK) {// wait for networkif (curl_multi_select($mh) != -1) {// pull in any new data, or at least handle timeoutsdo {$mrc = curl_multi_exec($mh, $active);} while ($mrc == CURLM_CALL_MULTI_PERFORM);}}// retrieve dataforeach ($connomains as $i => $url) {if (($err = curl_error($conn[$i])) == '') {$res[$i]=curl_multi_getcontent($conn[$i]);} else {print "Curl error on handle $i: $err\n";}curl_multi_remove_handle($mh,$conn[$i]);curl_close($conn[$i]);}curl_multi_close($mh);return $res;}
?>
func.mysql.php的代码为:
<?php
function inserInfo($_title,$_content,$_content_extra,$_keywords){$_mysql=mysql_connect('localhost','root','123') or die('数据库连接错误');mysql_select_db('baike',$_mysql);mysql_query('set names utf8');mysql_query("INSERT INTO info (title,content,content_extra,keywords)VALUES('$_title','$_content','$_content_extra','$_keywords')"); mysql_close();
}
?>
这篇关于使用CURL构建爬虫,抓取百度百科内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!