Php 爬网后在数据库中复制数据
我使用简单的HTMLDOM将数据从网站抓取到数据库中,并显示在我的网页上。但每次我运行该文件时,都会将重复数据插入数据库。我如何能够不断检查数据库中是否已经存在数据?这是我的爬行文件:Php 爬网后在数据库中复制数据,php,Php,我使用简单的HTMLDOM将数据从网站抓取到数据库中,并显示在我的网页上。但每次我运行该文件时,都会将重复数据插入数据库。我如何能够不断检查数据库中是否已经存在数据?这是我的爬行文件: <?php $con=mysqli_connect("localhost","root","","crawling");\ mysql_connect("localhost", "root", "")or die("cannot connect"); mysql_select_db("craw
<?php
$con=mysqli_connect("localhost","root","","crawling");\
mysql_connect("localhost", "root", "")or die("cannot connect");
mysql_select_db("crawling")or die("cannot select DB");
include "domcrawl.php";
$url="http://www.bgr.in/category/reviews/";
$html=file_get_html($url);
//$arr=$html->find('table[class=findList] tbody tr td[class=result_text]');
$m=$html->find('img');
$b=$html->find('a');
$c=$html->find('p');
$imghead = $b[21]->innertext;
$img = $m[3];
$imgtext = $c[0];
$sql = sprintf("INSERT INTO image1
( head, image, text, name)
VALUES
( '%s', '%s', '%s', '%s')",
mysql_real_escape_string($imghead),
mysql_real_escape_string($img),
mysql_real_escape_string($imgtext),
mysql_real_escape_string("gm")
);
mysql_query($sql);
$sql = "SELECT head FROM image1 WHERE name='gm'";
$sql1 = "SELECT image FROM image1 WHERE name='gm'";
$sql2 = "SELECT text FROM image1 WHERE name='gm'";
$result = mysql_query("$sql");
$result1 = mysql_query("$sql1");
$result2 = mysql_query("$sql2");
$head_get= mysql_result($result, 0);
$img_get= mysql_result($result1, 0);
$text_get= mysql_result($result2, 0);
echo "<br><br>";
echo $head_get;
echo "<br><br>";
echo $img_get;
echo $text_get;
?>
假设'date'=>$node->getElementsByTagName'pubDate'->item0->nodeValue是第11行,似乎没有带标记pubDate的元素,这就是为什么$node->getElementsByTagName'pubDate'->item0返回null或false。在获取对象属性之前必须检查,在您的情况下,它会查找空对象
$link = $node->getElementsByTagName('link')->item(0);
if(!empty($link)){
$nodeValue = $link->nodeValue,
}
'link' => $nodeValue;
类似地,对所有人都做您的代码应该做什么?到目前为止,您尝试了什么?因为您尝试解析的XML文档中没有名为pubDate的标记。非常类似于找出该错误的含义。请使用isset。检查是否为节点设置了值。因此,首先要将其存储在数组中,只需将其存储在mysql中即可。。为什么不跳过数组位呢?o、 o