抓取后重复数据库中的重复数据[重复]

This question already has an answer here:

I am using simple html dom to crawl data from a site into my database and display on my web page. But every time i run the file, duplicate data is also inserted into database.How can i keep check on whether data is already present in database or not? Here is my file for crawling:

 <?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;


  ?>
</div>
php
dpf5207
dpf5207 所以首先你要将它存储在一个数组中,只是为了将它存储在mysql中..为什么不跳过数组呢?o.O
接近 7 年之前 回复
drsxzut183207938
drsxzut183207938 使用isset。检查是否为节点设置了值。
接近 7 年之前 回复
douke1954
douke1954 这个答案非常相似,可以找出错误意味着什么。
接近 7 年之前 回复
dongzipu7517
dongzipu7517 因为您尝试解析的XML文档中没有任何名为pubDate的标记。
接近 7 年之前 回复
dongxiang7276
dongxiang7276 你的代码应该做什么?你都尝试了些什么?
接近 7 年之前 回复

2个回答

Must check before getting object properties, In your case it is finding empty object

$link = $node->getElementsByTagName('link')->item(0);
if(!empty($link)){
 $nodeValue = $link->nodeValue,
}

'link' => $nodeValue;

Similarly do for all

Assuming 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue is line 11, there seems to be no element with tag pubDate, which is why $node->getElementsByTagName('pubDate')->item(0) returns either null or false.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问