I am doing some web scraping and come across several tables of data that I want to query against. Currently I'm up to:
$url = 'http://finance.yahoo.com/q/op?s=QQQQ&m=2012-04';
$html = @DOMDocument::loadHTMLFile($url);
$xml = simplexml_import_dom($html);
$results = $xml->xpath('//table[@class="yfnc_datamodoutline1"]');
var_dump($results);
Produces results: http://pastebin.com/6p3L2Kcc
This is well-ordered HTML table data, with TH and TD and everything. I'd like to use it like this:
$sql = 'SELECT Last,Open_Int FROM TABLE1 WHERE Last>25 AND Symbol LIKE "%C%"';
$results = $xmltable->sql($sql);
while($result = $results->fetch_assoc())
echo $result['Last'] . " -- " . $result['Open_Int'] . "\n";
Without any creativity, I can write classes to parse that HTML table, take the first row, create a table in sqlite, select other rows and turn them into insert statements. But, do you know a better way to do this, or is there some powerful PHP function that I'm not seeing?
Update: Perhaps the scope here is too big. I'd be happy with a link to a library or advice on getting an HTML table in to a (proper) XML table.
simplexml_load_file()orsimplexml_load_string(). For your actual question, if you really need full SQL ability, then you need to store it in a database. However, do you really need all that SQL provides? If not, you may do well to write your own function to do the queries using XPath.