doula4096 2017-12-14 13:13
浏览 70
已采纳

我怎么找到这个div? (PHP Simple HTML DOM Parser)

This is my code:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div#ires', 0)->innertext;
    echo $title;
?>

It outputs all result of the Google Search Page under the Search "BA236".

The problem is I dont need all of them and the Information I need is inside a div that has no id or class or anything else.

The div I need is inside the first

<div class="g">

on the Page, so maybe I should try something like this:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div[class=g], 0')->innertext;
    echo $title;
?>

But the Problem of that is, if I load the page it shows me nothing except this:

Notice: Trying to get property of non-object in C:\xampp\htdocs...\simpletest2.php on line 4

So how can i get the div i´m searching for and what am I doing wrong ?

Edit:

Solution:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $e = $html->find("div[class=g]");
    echo $e[0]->innertext;
?>

Or:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div[class=g]')[0]->innertext;
    echo $title;
?>
  • 写回答

2条回答 默认 最新

  • dou8mwz5079 2017-12-14 13:26
    关注

    I made a change to your code where I am searching for the class:

    <?php
        include('simple_html_dom.php');
        $html = file_get_html('http://www.google.com/search?q=BA236',false);
        $e = $html->find("div[class=g]");
    echo $e[0]->innertext;
    ?>
    

    result:

    British Airways Flight 236
    
    Scheduled   departs in 13 hours 13 mins
    
    Departure   DME 5:40 AM     —
    
    Moscow  Dec 15      
    
    Arrival LHR 6:55 AM     Terminal 5
    
    London  Dec 15      
    
    Scheduled   departs in 1 day 13 hours
    
    Departure   DME 5:40 AM     —
    
    Moscow  Dec 16      
    
    Arrival LHR 6:55 AM     Terminal 5
    
    London  Dec 16      
    

    I looked for the div elements with class g then I printed the count of the first element '0'

    $e = $html-> find ("div [class = g]");
    echo $e [0]->innertext;
    

    Your code:

    <?php
        include('simple_html_dom.php');
        $html = file_get_html('http://www.google.com/search?q=BA236',false);
        $title=$html->find('div[class=g]')[0]->innertext;
        echo $title;
    ?>
    

    not ('div[class=g], 0')

    but ('div[class=g]')[0]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化