6

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 Answers 2

6

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]

Sign up to request clarification or add additional context in comments.

4 Comments

It would be helpful to others if you added some explanation of what you've done to solve the problem.
Thank you for the explanation !
@Kroe I'm sorry if I can not give you more details ^^.
Question closed
0

there is no need for simple_html_dom here, it's easy to do with the builtins DOMDocument and DOMXPath.

<?php
$html = file_get_contents('http://www.google.com/search?q=BA236');
echo (new DOMXPath ( (@DOMDocument::loadHTML ( $html )) ))->query ( '//div[@class="g"]' )->item ( 0 )->textContent;

in my opinion, DOMDocument + DOMXPath makes simple_html_dom.php rather pointless. the former 2 can do pretty much everything simple_html_dom can do, and are built-in native php functions, which is likely to be maintained as long as PHP itself is maintained, and the latter is a 3rd party project which seems nearly dead by the looks of it (last commit was in 2014, there was only 1 commit in all of 2014, and 2 commits in all of 2013 )

1 Comment

may be true but that's absolutely not the answer to my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.