You are here:  » Google Trends


Google Trends

Submitted by support on Fri, 2009-05-08 19:11

Hello Jez,

You're quite correct, it is HTML embedded within an XML field, and since it is CDATA delimited it is not possible to parse down into it as such. However, since the format is uniform, it is straight forward to extract each query with a bit of PHP trickery.

I've just tested it out using explode() on "<li>" to break the CONTENT value down into individual lines, and then using strpos() to find the constant strings that surround the query on each line. Here's the output running on this server:

http://www.magicparser.com/examples/trends.php

Here's the source:

<?php
  header
("Content-Type: text/plain");
  require(
"MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$trends explode("<li>",$record["CONTENT"]);  
    foreach(
$trends as $trend)
    {
      
$posA strpos($trend,"sa=X\">");
      if (
$posA)
      {
        
$posA $posA 6;
        
$posB strpos($trend,"</a>",$posA);
        
$query substr($trend,$posA,($posB-$posA));
        print 
$query;
        print 
"\n";
      }
    }
  }
  
$url "http://www.google.com/trends/hottrends/atom/hourly";
  
MagicParser_parse($url,"myRecordHandler","xml|FEED/ENTRY/CONTENT/");
?>

Of course you can then add to the code above to add each $query value to the database.

Hope this helps!
Cheers,
David.

Submitted by jezza101 on Mon, 2009-05-11 19:19

Thank for the reply David, that looks very helpful and should get me going again :)