PHP中的Web爬虫无法在mysql数据库中创建记录。如何修复?
我正在为谷歌这样的搜索引擎创建一个网络爬虫。网络爬虫工作得很好,这是我通过终端运行它时看到的,但它没有在mysql数据库中写入任何记录 我已经尝试将所有权限授予web爬虫使用的数据库用户,但没有用。我的服务器是完美的,我可以肯定PHP中的Web爬虫无法在mysql数据库中创建记录。如何修复?,php,Php,我正在为谷歌这样的搜索引擎创建一个网络爬虫。网络爬虫工作得很好,这是我通过终端运行它时看到的,但它没有在mysql数据库中写入任何记录 我已经尝试将所有权限授予web爬虫使用的数据库用户,但没有用。我的服务器是完美的,我可以肯定 <?php $start = "http://localhost/mariophp/test.html"; $already_crawled=array(); $crawling=array(); function get_details($url) {
<?php
$start = "http://localhost/mariophp/test.html";
$already_crawled=array();
$crawling=array();
function get_details($url)
{
$options=array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: ZeroBot/0.2\n"));
$context=stream_context_create($options);
$doc = new DOMDocument();
@$doc->loadHTML(@file_get_contents($url,false,$context));
$title=$doc->getElementsByTagName("title");
$title=$title->item(0)->nodeValue;
$simg=$doc->getElementsByTagName("img");
//$simg=$simg->getAttribute("src");
//$simg=$simg->item(0)->nodeValue;
$description="";
$keywords="";
$metas=$doc->getElementsByTagName("meta");
for($i=0; $i<$metas->length; $i++)
{
$meta=$metas->item($i);
if($meta->getAttribute("name")==strtolower("description"))
$description=$meta->getAttribute("content");
if($meta->getAttribute("name")==strtolower("keywords"))
$keywords=$meta->getAttribute("content");
}
$_con=mysqli_connect("localhost","augustus","password");
mysqli_select_db($_con,"websited");
$title=$_POST["title"];
$url=$_POST["url"];
$keywords=$_POST["keywords"];
$description=$_POST["description"];
$simg=$_POST["simg"];
$sql="insert into websited(stitle,slink,skey,sdesc,simg) values('$title','$url',$keywords',$description','$simg')";
if(!mysqli_query($_con,$sql))
{
echo "Error: mysqli_error($_con))";
}
}
function follow_links($url)
{
global $already_crawled;
global $crawling;
$options=array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: MarioBot/0.1\n"));
$context=stream_context_create($options);
$doc = new DOMDocument();
@$doc->loadHTML(@file_get_contents($url,false,$context));
$linklist = $doc->getElementsByTagName("a");
foreach ($linklist as $link)
{
$l = $link->getAttribute("href");
if(substr($l,0,1)=="/" && substr($l,0,2)!="//")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].$l;
}
else if (substr($l,0,2)=="//")
{
$l=parse_url($url)["scheme"].":".$l;
}
else if(substr($l,0,2)=="./")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].dirname(parse_url($url)["path"]).substr($l,1);
}
else if(substr($l,0,1)=="#")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].parse_url($url)["path"].$l;
}
else if(substr($l,0,3)=="../")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"]."/".$l;
}
else if(substr($l,0,11)=="javascript:")
{
continue;
}
else if(substr($l,0,5)!="https" && substr($l,0,4)!="http")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"]."/".$l;
}
if(!in_array($l,$already_crawled))
{
$already_crawled[]=$l;
$crawling[]=$l;
echo get_details($l)."\n";
//echo $l."\n";
}
}
array_shift($crawling);
foreach ($crawling as $site) {
follow_links($site);
}
}
follow_links($start);
print_r($already_crawled);
?>
去掉这些行:
$title=$_POST["title"];
$url=$_POST["url"];
$keywords=$_POST["keywords"];
$description=$_POST["description"];
$simg=$_POST["simg"];
这些都覆盖了你从网站抓取的变量$\u POST
用于获取从表单或AJAX提交的参数,此处不需要这些参数
对mysqli\u error()
的调用不应在字符串中。改变
if(!mysqli_query($_con,$sql))
{
echo "Error: mysqli_error($_con))";
}
到
在上述代码中,您不会尝试向数据库中插入任何内容$sql
是字符串形式的查询,您不需要对其执行任何操作it@tim我的数据库和其中的表的名称是相同的,即“websited”。您需要使用mysqli_query
ieif(!mysqli_query($\u con,$sql)){echo(“Error:.mysqli_Error($\u con));}
在return
语句之后不会执行任何操作。不要试图通过连接字符串来创建JSON。创建一个数组并使用json\u encode()
。
if(!mysqli_query($_con,$sql))
{
echo "Error: " . mysqli_error($_con));
}