我正在编写一个使用简单的HTML DOM解析器进行网页抓取的项目。我从我的数据库中抓取网页,提取内容并将其存储在数据库中。该代码与第一个URL一起工作正常,但在剩余的URL上它只是跳出循环。以下是我的代码。为什么我的代码跳出一个循环在php
include_once('Connections/elecom_connect.php');
include_once('dom/simple_html_dom.php');
mysqli_select_db($elecom_connect,$database_elecom_connect);
$sql = "SELECT * FROM link_data";
$result_links = array();
$result_cates = '';
$result_subs = '';
$result_names = '';
$num = -1;
$count = 0;
$img = '.image-wrapper img';
$brand = 'h2.title span.brand';
$name = 'h2.title span.name';
$price = 'span.price-box';
$link = 'section.products a.link';
$site = new simple_html_dom();
$query = mysqli_query($elecom_connect,$sql);
if (!$query){
echo 'Database error: ' . mysqli_error($elecom_connect);
}
while ($row = mysqli_fetch_array($query)) {
$result_links[] = $row;
}
foreach($result_links as $link){
$var = $link['link'];
if (!empty($var)) {
var_dump($var);
$site->load_file($var);
if (!empty($site)) {
$currentImg = $site->find($img);
$currentBrand = $site->find($brand);
$currentName = $site->find($name);
$currentPrice = $site->find($price);
$currentLink = $site->find($link);
$rid = $link['id'];
$rcates = $link['link_category'];
$rsubs = $link['link_subcategory'];
$rnames = $link['link_name'];
if (!empty($currentImg)) {
foreach($currentImg as $im){
$count++;
if($count % 2 == 0 && $count < 40){
$num++;
$cImg = $im->src;
$cBrand = "<p>".$currentBrand[$num]->plaintext."</p>";
$cName = "<p>".$currentName[$num]->plaintext."</p>";
$cPrice = "<p>".$currentPrice[$num]->plaintext."</p>";
//$cLink = $currentLink[$num]->href;
$content = file_get_contents($cImg);
//Store in the filesystem.
$save_path = "cachedPages/$rid.$num.jpg";
file_put_contents($save_path,$content);
$insertSQL = "INSERT INTO item_detail (item_name, item_brand, item_price, item_img, item_cate, item_sub_cate,filter_by) VALUES ('$cName', '$cBrand', '$cPrice','$save_path','$rcates','$rsubs','$rnames')";
mysqli_select_db($elecom_connect,$database_elecom_connect);
$Result1 = mysqli_query($elecom_connect,$insertSQL) or die(mysqli_error( $elecom_connect));
echo 'Success';
}
}
}
}
}
$site->clear();
}
这是我得到的错误代码。
Fatal error: Uncaught Error: Call to a member function find() on null in dom/simple_html_dom.php:1113 Stack trace: #0
我该怎么办?
确保您的$ image $ brand $ price $ link和$ name在dom/simple_html_dom.php文件中未设置为空 – AMH
它们未设置为null。它第一次运行第一个URL,但不能再运行第二个URL –
我不能用空检查重现它,但该错误是说'$ site'是'null'和'null'没有一个'find()'方法。它是如何越过空的IDK。这是'simple_html_dom'文件吗? – nerdlyist