【问题标题】:Why is my code jumping out of a loop In php为什么我的代码在php中跳出循环
【发布时间】:2017-06-07 02:27:16
【问题描述】:

我正在使用简单的 HTML DOM 解析器编写一个关于网络抓取的项目。我从我的数据库中抓取网页,提取内容然后存储在数据库中。该代码在第一个 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 中调用 null 上的成员函数 find() 堆栈跟踪:#0

我该怎么办?

【问题讨论】:

  • 确保您的 $image $brand $price $link 和 $name 在 dom/simple_html_dom.php 文件中没有设置为空
  • 它们没有设置为空。它第一次使用第一个 URL 运行,但无法再次使用第二个 URL 运行
  • 我无法通过空支票重现它,但该错误是说 $sitenullnull 没有 find() 方法。它是如何通过空 IDK 的。这是simple_html_dom 文件吗?
  • @nerdlyist 是的,它是一个 simple_html_dom 解析器..
  • 这里的哪一行是数字1113?关于问题所在,错误非常明显。鉴于上面的代码,它不会抛出该错误,因为对find 的唯一调用来自$site,并且您正在检查!empty($site)。所以我会假设这不是整个文件(显然不是因为我没有看到 1100 行代码)并且错误在您的代码中的其他位置,或者您有一段时间没有保存文件并且您正在运行的代码已过期。

标签: php mysqli simple-html-dom


【解决方案1】:

这行代码不正确:

$site = new simple_html_dom();

您显然不需要根据 GitHub https://github.com/samacs/simple_html_dom/tree/master/example 中的示例目录执行此操作

你想要做的是使用两种方法之一

file_get_htmlstr_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 null-&gt;find() 会抛出你得到的错误。

【讨论】:

  • 请在我拿到笔记本电脑并尝试给出的答案后立即投票。
  • 这是我在处理您的建议后遇到的新错误消息警告:file_get_contents():流不支持在第 75 行的 dom/simple_html_dom.php 中查找警告:file_get_contents():查找失败在第 75 行的 dom/simple_html_dom.php 中的流中定位 -1
  • @RoseMba 你能var_dump($cImg)吗?我不确定该 var 中的内容,但根据错误,它是 dom 对象而不是文件路径。
  • 脚本没有执行到那个点 (var_dump($cImg))。从 simple_html_dom change_log.txt 我看到了这个更新: [正在进行] 1. “file_get_contents()”的错误将作为异常抛出。 2. 在调用“file_put_contents()”时添加标志:LOCK_EX。 3.修正“token_blank_t”的拼写错误。
  • 我检查了var_dump($site),它显示boolean(false) ....请问我该怎么办?
【解决方案2】:

您为每一行替换整个数组,因此只有最后一个 URL 将被抓取。

$result_links = array();
while ($row = mysqli_fetch_array($query))
{ 
    array_push($result_links, $row);
} 

【讨论】:

  • []表示它是一个数组,所以你说的不是真的。
  • @RST 我知道它是一个数组,但代码没有附加到它替换数组的数组。
  • $array[] = "whatever"; 会将whatever 附加到$array 的末尾。 [] 表示追加到与array_push 完全相同的数组。
  • @JonathanBartlett - 如果您实际上分配了这样的数组。 $result_links[] = "str" ;它实际上附加到现有数组。
猜你喜欢
  • 1970-01-01
  • 2019-05-10
  • 2021-04-30
  • 2019-05-10
  • 2021-06-21
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多