parsing the google trends atom feed

Short and sweet : how to grab the google trends atom feed and parse out the links,
in 15 lines.

  1. $feed = simplexml_load_file('http://www.google.com/trends/hottrends/atom/hourly');
  2. $children =  $feed->children('http://www.w3.org/2005/Atom');
  3. $parts = $children->entry;
  4. foreach ($parts as $entry) {
  5.    $details = $entry->children('http://www.w3.org/2005/Atom');
  6.    $dom = new domDocument();
  7.    @$dom->loadHTML($details->content);
  8.    $anchors = $dom->getElementsByTagName('a');
  9.   foreach ($anchors as $anchor) {
  10.     $url = $anchor->getAttribute('href');
  11.     $urltext = $anchor->nodeValue;
  12.     echo 'Link: <a href="' . $url . '" title="' . $urltext . '">' . $urltext . '</a> ';
  13.   }
  14. }

Requires php with simplexml and dom xml. You could use it for a blogfarm script but that’s about all I can think of.

edited 18-12-08 :
$dom->loadHTML((string) $details->content);
to
@$dom->loadHTML($details->content);

This entry was posted in google, php and tagged , , . Bookmark the permalink.

10 Responses to parsing the google trends atom feed

  1. Herman says:

    Hi,

    Can you explain how to install this script step by step.

    I tried to using this script in my site but i found warning like this

    Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ‘;’ in Entity, line: 2 in /home/username/public_html/gtrends.php on line 8

    Whats wrong?

    Thanks for your time to read this comment.

  2. juust says:

    Hi Herman, I encountered the same problem. Adding @ to suppress errors solves it, I just forgot to add it to this snippet. Sorry for the inconvenience, and thanks for the feedback.

  3. Herman says:

    Wow fasted replay two thumb up for you. Problem solved now many thanks for you juust.

    Hi just i`m newbie in php (less kowledge about PHP) and i want ask do you know how to display result google trends details still open in my site?

    Example case i have a site www+examplesite+com and at this time (moment writing this comment) the hottest keyword in google trends is Bob Bowersox. How to made it become www+examplesite+com/bob_bowersox. So we not must visit google+com/trends to know details what the hottest topic/keyword today because all has provide in my site. I found some site can do it such as trendfo+com or utube+smashits+com/hot-trends-from-world+html

    I enjoy with your site because in this place i can learn more about PHP and can found more php script that very usefull. Thanks for share juust and keep posted :)

    Best Regards

  4. M.A.Dhavan says:

    Thank you Sir…

    I was searching on the net the whole day for a solution to parse the google hot trends.
    I tried several scripts and failed.

    Finally I got your script. Its really cool, simple and efficient, which works pretty nice.

    Thank you once again.

    Keep writing useful scripts.

  5. Neo says:

    Thanks Dear
    You saved my life

  6. SudoBash says:

    I made a google trends parser with C++ but it does a few extra things and runs on any Linux/Unix/BSD platform with ease. It uses wget to download and separates the keywords for you into two files a plain text and a single line file that always contains the latest keywords of the hour and it even makes pages that recognize certain words like video and episode… Check the completely automated output at:
    http://www.nflspot.com/hot/HotSearches.html
    or
    http://www.theviruscure.com/HotSearches.html

  7. Sbobet says:

    Thank you. Few confused with simplexml and dom xml. But it solved now.

  8. Thanks for the share and i think that i have an idea from your post. Thanks a lot

  9. FLVtube says:

    Thanks alot, your solution helped me out alot. Is there a simple addin to grab the class attribute from the ?

    Trending video search integration:
    http://www.FLVtube.com

  10. FLVtube says:

    class attribute from the span tag – it delete from my last post :(

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>