php - PHP 中的网络爬虫无法在 mysql 数据库中创建记录。怎么修?
问题描述
我正在为像谷歌这样的搜索引擎创建一个网络爬虫。网络爬虫运行良好,这是我通过终端运行时看到的,但它没有在 mysql 数据库中写入任何记录。
我已经尝试将所有权限授予网络爬虫使用的数据库用户,但没有用。我的服务器完美无瑕,我可以肯定。
<?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);
?>
注意:代码中提到的 test.html 文件是一个包含不同网站链接的简单文件。您可能需要先设置服务器才能成功运行此代码。我现在得到以下输出。[augustoandro@Augustus zerophp]$ php crawle2.php PHP 注意:未定义索引:第 30 行 /srv/http/zerophp/crawle2.php 中的标题 PHP 注意:未定义索引:/srv/http/zerophp/crawle2.php 中的 url第 31 行 PHP 注意:未定义索引:/srv/http/zerophp/crawle2.php 中的关键字 第 32 行 PHP 注意:未定义索引:第 33 行 /srv/http/zerophp/crawle2.php 中的描述 PHP 注意:未定义索引: simg in /srv/http/zerophp/crawle2.php on line 34 PHP 可恢复的致命错误:无法将类 mysqli 的对象转换为 /srv/http/zerophp/crawle2 中的字符串。
请帮忙。
解决方案
摆脱这些行:
$title=$_POST["title"];
$url=$_POST["url"];
$keywords=$_POST["keywords"];
$description=$_POST["description"];
$simg=$_POST["simg"];
这些正在覆盖您从抓取网站中获得的变量。$_POST
用于获取从表单或 AJAX 提交的参数,这里不需要它们。
调用mysqli_error()
不应在字符串内。改变
if(!mysqli_query($_con,$sql))
{
echo "Error: mysqli_error($_con))";
}
至
if(!mysqli_query($_con,$sql))
{
echo "Error: " . mysqli_error($_con));
}
推荐阅读
- scala - 没有结果的类型测试:Option[akka.actor.ActorSystem] 类型的值不能也是 akka.actor.ActorSystem
- react-native - 路由 'AuthLogin' 的组件必须是 React 组件
- node.js - 无法从对话流代理调用部署在我的服务器上的 webhook
- .net - 如何在没有 Fiddler 的情况下查看来自 .NET 库的 https 请求?
- asp.net-core - 为什么 ASP.NET MVC 标签助手不能创建正确的链接?
- python - python:从字典中提取多个键
- python - 如果满足条件,则将图形添加到数据表单元格
- java - 为什么lucene不返回结果
- javascript - 无法获取要更新的 ReactJS 组件
- python - 在 csv 中写入函数的结果