Pick CSS class from other sites using PHP HTML DOM Parser

Asked

Viewed 422 times

1

I’m trying to get a class from a news site, but I’m not getting it.

$titulo = $html->find('feed-post-body-title gui-color-primary gui-color-hover', 0)
               ->innertext;

echo $titulo;

You’re making that mistake:

Notice: Trying to get Property of non-object

How do I get classes or ids?

  • Friend, the variable $html is getting what exactly?

  • $html = file_get_html('url_do_site');

  • Ta getting the site url I want to get the class

  • Friend, try as this example foreach($html->find('img') as $element) &#xA; echo $element->src . '<br>'; However, maybe your return of file_get_html is giving error, or the variable $html is as an array, you have to find out exactly what is the return of the function and in what state or what content exists inside the variable.

  • Get the normal HTML objects I can... The problem is getting some object with a certain ID or Class css

  • 1

    Take a look at the answer to that question http://stackoverflow.com/questions/15761115/find-div-with-class-using-php-simple-html-dom-parser I think that’s exactly what you should do.

  • Thanks friend! It helped a lot!

Show 2 more comments

1 answer

1

You need to specify the element you are looking for, if it is a div, one p and etc...

If I were a div for example, I’d be like this:

//Using "." (dot) to specify that it is a class

$titulo = $html->find('div.feed-post-body-title', 0);

or

$titulo = $html->find('div[class=feed-post-body-title]', 0);

//If you were looking for an element with id instead of class:

//Using "#"to specify that it is an id

$titulo = $html->find('div#feed-post-body-title', 0);

or

$titulo = $html->find('div[id=feed-post-body-title]', 0);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.