简体   繁体   English

数据提取中的成员函数错误

[英]member function error in data extraction

I am trying get data from a external source but i can't get the data and i am facing this error. 我正在尝试从外部来源获取数据,但是我无法获取数据,而且我正面临此错误。

Notice: Trying to get property of non-object in E:\\xampp\\htdocs\\test\\merit-list.php on line 38 Fatal error: Call to a member function find() on null in E:\\xampp\\htdocs\\test\\merit-list.php on line 39 注意:试图在第38行的E:\\ xampp \\ htdocs \\ test \\ merit-list.php中获取非对象的属性致命错误:调用E:\\ xampp \\ htdocs \\ test中null的成员函数find() \\ merit-list.php,第39行

Here is my code 这是我的代码

<?php
            require('resources/inc/simple_html_dom.php');
            $linksrc = 'http://58.65.172.36/Portal/WebSiteUpdates/Achievements.aspx';
            $curl = curl_init();
            curl_setopt_array($curl, array(
            CURLOPT_URL => $linksrc,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 3000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
            ));
            $file = curl_exec($curl);
            $error = curl_error($curl);
            curl_close($curl);
            $dom = new simple_html_dom();
            $dom->load($file);
            $doctorDivs = $dom->find("table#Farooq", 0)->children();
            $doctors = array();
            foreach($doctorDivs as $div){
                $doctor = array();

                // line 38
                $image = $doctor["image"] = $div->find('img', 0)->src; 
                $details = $div->find('tr', 0)->find("td");
                $name = $doctor["name"] = trim($details[1]->plaintext);
                $spec = $doctor["desc"] = trim($details[2]->plaintext);

                $doctors[] = $doctor;
                echo $image;
                echo $name;
                echo $spec;
            }       

        ?>

The problem is that the first row of the table doesn't have an <img> , because it's the row of headings, so $div->find("img", 0) returns null . 问题在于表的第一行没有<img> ,因为它是标题行,因此$div->find("img", 0)返回null You get the first error when you try to access null->src . 当您尝试访问null->src时,您会遇到第一个错误。

The second error is because $div is the <tr> element. 第二个错误是因为$div<tr>元素。 $div->find("tr") searches the children of $div , it doesn't include $div itself, so it always returns null . $div->find("tr")搜索$div ,它不包括$div本身,因此始终返回null Also, this code won't work in the heading row, either, because it contains <th> rather than <tr> elements. 另外,此代码也不会在标题行中运行,因为它包含<th>而不是<tr>元素。

You could just skip over the heading row by putting: 您可以通过输入以下内容跳过标题行:

array_shift($doctorDivs);

before the foreach loop. foreach循环之前。 This will remove the first element of the array. 这将删除数组的第一个元素。

And change $details to : 并将$details更改为:

$details = $div->find("td");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM