duanrong0738 2016-08-11 12:39
浏览 47
已采纳

简单的html dom抓住h1标头

I just learning about simple_html_dom.php, I try to get the h1 content with some class.

<h1 class="entry-title">example for the header</h1>

here the raw html file from the website that i want to get the content.

    <header class="entry-header">
    <div class="entry-meta">
        <span class="cat-links"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="category tag">News</a></span>
    </div>
    <h1 class="entry-title">example for the header</h1>
    <div class="entry-meta">
        <span class="entry-date"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="bookmark"><time class="entry-date" datetime="2016-08-11T11:54:07+00:00">11 August 2016</time></a></span> 
        <span class="byline"><span class="author vcard"><a class="url fn n" href="https://xxxxx/2016/08/11/xxxxxxx" rel="author">wndwnrt</a></span></span>          
        <span class="comments-link"><a href="https://xxxxx/2016/08/11/xxxxxxx">1 Comment</a></span>
    </div>
</header>

here my code to get the h1 class="entry-title" content (example for the header)

<?php
 require_once __DIR__.'/simple_html_dom.php';
 $html = new simple_html_dom();
 $html->load_file('https://xxxxx/2016/08/11/xxxxxxx');
 $header_1 = $html->find('h1[class="entry-title"]')->innertext;
?>
<table border="1">
 <thead>
   <tr>
     <th><?php echo $header_1; ?></th>
  </tr>
 </thead>
</table>

when i run the code, the result is error:

Trying to get property of non-object

can anyone tell where is the error? and what i should to do? Thank you very much.

  • 写回答

1条回答 默认 最新

  • douzhangcuo2174 2016-08-11 12:43
    关注

    Yes you see that error because you are passing only one argument to the find function.

    $header_1 = $html->find('h1[class="entry-title"]')->innertext
    

    now try this:

    $header_1 = $html->find('h1[class="entry-title"]',0)->innertext
    

    because you also have to pass the number of the h1 you are trying to get!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题