doula4096 2017-12-14 05: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 05: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 MC9S12XS128单片机开发板实验,
  • ¥15 C#多线程假死或卡死问题
  • ¥15 关于#tcp/ip#的问题:苹果电脑M1,easyconnect登录成功,显示虚拟 IP 地址
  • ¥15 客户端发现不了OPC服务器
  • ¥35 spaceclaim脚本
  • ¥500 寻找华为新款路由器开telnet方法
  • ¥20 运行pointnerf模型遇到了pycuda的错误,如何解决?(相关搜索:测试代码|自动驾驶|数据集)
  • ¥15 失败的github程序安装
  • ¥15 WSL上下载的joern在windows怎么用?
  • ¥15 jetson nano4GB
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部