[英]Why is my code jumping out of a loop In php
我正在使用简单的HTML DOM解析器编写有关Web抓取的项目。 我从数据库中抓取网页,提取内容然后存储在数据库中。 该代码可以与第一个URL正常运行,但是在其余URL上就可以脱离循环了。 下面是我的代码。
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();
}
这是我得到的错误代码。
致命错误:未捕获错误:在dom / simple_html_dom.php:1113中调用成员函数find()时为null堆栈跟踪:#0
我该怎么办。?
这行代码不正确:
$site = new simple_html_dom();
您显然不需要根据GitHub https://github.com/samacs/simple_html_dom/tree/master/example中的example目录执行此操作
您想要做的是使用两种方法之一
当包含include_once('dom/simple_html_dom.php');
时,将加载file_get_html
或str_get_html
include_once('dom/simple_html_dom.php');
。
所以你实际上想看看
$site = file_get_html($url); //URL to a site you are parsing ie 'http://www.google.com/'
//OR
$site = str_get_html($str); // String file to some html file
如果您阅读该代码,实际上会创建一个$dom_node
,上面带有find
方法。
您拥有的东西之所以奇怪,是因为您正在创建和对象,并且当您检查if(!empty($site))
,由于存在对象,它返回true。 但是,内部dom_node
的设置不正确。
当您到达此libs文件的1113行而不是您的dom_node
,您就有一个空的dom_node
,其中dom_node
null->find()
会抛出您得到的错误。
您需要为每行替换整个数组,因此只有最后一个URL才会被抓取。
$result_links = array();
while ($row = mysqli_fetch_array($query))
{
array_push($result_links, $row);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.