-1

好的,所以我一直在研究这个小脚本,它基本上是从 ted.com 中删除一个页面,一切都按我的意愿工作(这意味着我可以打印出我感兴趣的所有值),问题是出于某种原因我运行刮板时收到这些警告,但不确定为什么通知/警告发生的值被正确打印出来

 PHP Warning:  dom_import_simplexml() expects parameter 1 to be object, null given in /var/www/ted/import_ted.php on line 23
 PHP Notice:  Trying to get property of non-object in /var/www/ted/import_ted.php on line 23
 PHP Notice:  Undefined offset: 1 in /var/www/ted/import_ted.php on line 25
 PHP Notice:  Trying to get property of non-object in /var/www/ted/import_ted.php on line 27

这实际上是我的 php 脚本(我已经注释了警告和通知的行)

<?php
    $mysqli = mysqli_connect("localhost", "user", "password", "database");
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

 $html = file_get_contents('http://www.ted.com/talks/quick-list?sort=date&order=desc');
 $doc = new DOMDocument();
 $doc->loadHTML($html);
 $sxml = simplexml_import_dom($doc);
 $rows = $sxml->xpath('//tr');
 $description="not_available";
 $ted_link="none";
 $i=0;
 //$stmt = $mysqli->prepare("INSERT INTO `ted` VALUES( ?, ?, ?, ?, ?, ?, ?)");

 foreach($rows as $row) {
    $video = Array();
    $video['pub_date']=  $row->td[0];
    $video['event'] = $row->td[1];
    $sec_temp = explode(":" , dom_import_simplexml($row->td[2])->textContent );//line23
    $video['speaker'] = $sec_temp[0];
    $video['title'] = $sec_temp[1]; //line 25
    $video['duration'] = $row->td[3];
    $video['link'] = $row->td[4]->a[2]['href']; //line27
    print( "\n  line Number: " . $i . "title: " . $video['title']);
    print ("link: " .$video['link']);
   if($i != 0){
      //      $stmt->bind_param("sssssss", $video['event'], $video['speaker'], $video['title'], $description, $ted_link, $video['link'], $description, $video['pub_date'] );
 //        $stmt->execute();
   }
  $i++;
}

// $stmt->close();

?>

好的,就像我说的那样,一切都会打印出我期望的内容,包括$video['title']由于某种原因会生成未定义的偏移量。问题是直到我可以使这些变量“对象”我不能将它们作为参数绑定到 mysqli 查询。但是我似乎无法弄清楚如何做到这一点?

同样,这里的相关是表格行的片段,以防这是问题(我认为不是)

<tr>
    <td>Jun 2013</td>
<td>TEDGlobal 2013</td>
<td><a href="/talks/manal_al_sharif_a_saudi_woman_who_dared_to_drive.html">Manalal-Sharif: A Saudi woman who dared to drive</a> </td>
<td>14:16</td>
<td><a href="http://download.ted.com/talks/ManalAlSharif_2013G-light.mp4?apikey=TEDDOWNLOAD">Low</a> | <a href="http://download.ted.com/talks/ManalAlSharif_2013G.mp4?apikey=TEDDOWNLOAD">Regular</a> | <a href="http://download.ted.com/talks/ManalAlSharif_2013G-480p.mp4?apikey=TEDDOWNLOAD">High</a></td>
</tr>

另请注意,我尝试使用 settype($var, "object") 在没有任何运气的情况下进行绑定(尽管返回为真)

无论如何,任何有关我如何使它工作的帮助将不胜感激!

4

1 回答 1

1
<?php 

 $html = file_get_contents('http://www.ted.com/talks/quick-list?sort=date&order=desc');
 $doc = new DOMDocument();
 $doc->loadHTML($html);
 $sxml = simplexml_import_dom($doc);
 $rows = $sxml->xpath('//tr');
 /* print_r($rows);
 die(); */


 $description="not_available";
 $ted_link="none";
 $i=0;
 //$stmt = $mysqli->prepare("INSERT INTO `ted` VALUES( ?, ?, ?, ?, ?, ?, ?)");

 foreach($rows as $row) {

    //first object is th not an td
    if(isset($row->th))
    {
        echo $row->th[1]->a;
        echo $row->th[2]->a;        
        echo $row->th[3]->a;        
        echo $row->th[4];       

    }else{

        $video['pub_date']=  $row->td[0];
        $video['event'] = $row->td[1];
        $sec_temp = explode(":" , $row->td[2]->a);//line23
        $video['speaker'] = $sec_temp[0];
        $video['title'] = $sec_temp[1]; //line 25
        $video['duration'] = $row->td[3];
        $video['link'] = $row->td[4]->a[2]['href']; //line27
        print( "\n  line Number: " . $i . "title: " . $video['title']);
        print ("link: " .$video['link']); 

        if($i != 0){
            //      $stmt->bind_param("sssssss", $video['event'], $video['speaker'], $video['title'], $description, $ted_link, $video['link'], $description, $video['pub_date'] );
            //        $stmt->execute();
        }
        $i++;
    }

}
于 2013-06-19T05:27:54.243 回答