Just trying to extract some information from the AEC website (e.g. http://ift.tt/1DIBYib). The XPath query I'm running is '//x:tbody/x:tr/x:td[4]/x:a', which I've tested in XPath Checker (the Firefox extension) and it pulls up the relevant locality data.
I'm then using PHP to load the page, execute the query and then iterate through the results.
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
# Create a DOM parser object
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$elements = $xpath->query( '//tbody/tr/td[4]/a');
foreach ($elements as $element) {
echo $element;
}
I'm then getting:
Warning: Invalid argument supplied for foreach() in /home/givesh5/public_html/dig/electoratesearch.php on line 41
It seems that the query is returning some sort of boolean rather than a list of matches for the query?
Relevant markup as follows:
<table cellspacing="0" rules="all" border="1" id="ContentPlaceHolderBody_gridViewLocalities" style="border-collapse:collapse;">
<tr class="headingLink">
<th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolderBody$gridViewLocalities','Sort$StateAb')">State</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolderBody$gridViewLocalities','Sort$LocalityNm')">Locality/Suburb</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolderBody$gridViewLocalities','Sort$Postcode')">Postcode</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolderBody$gridViewLocalities','Sort$DivisionNm')">Electorate</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolderBody$gridViewLocalities','Sort$DivisionNmRedistributed')">Redistributed Electorate</a></th><th scope="col">Other Locality(s)</th>
</tr><tr>
<td>VIC</td><td>BOTANIC RIDGE</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CANNONS CREEK</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Holt&filterby=Electorate&divid=216">Holt</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE EAST</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE EAST</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Holt&filterby=Electorate&divid=216">Holt</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE NORTH</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Holt&filterby=Electorate&divid=216">Holt</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE SOUTH</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>CRANBOURNE WEST</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Holt&filterby=Electorate&divid=216">Holt</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>DEVON MEADOWS</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>FIVEWAYS</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td><a href="LocalitySearchResults.aspx?filter=DEVON+MEADOWS&filterby=LocalityorSuburb&state=VIC">DEVON MEADOWS</a></td>
</tr><tr>
<td>VIC</td><td>JUNCTION VILLAGE</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Flinders&filterby=Electorate&divid=211">Flinders</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>SANDHURST</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Isaacs&filterby=Electorate&divid=219">Isaacs</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>SKYE</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Dunkley&filterby=Electorate&divid=210">Dunkley</a></td><td></td><td> </td>
</tr><tr>
<td>VIC</td><td>SKYE</td><td><a href="LocalitySearchResults.aspx?filter=3977&filterby=Postcode">3977</a></td><td><a href="LocalitySearchResults.aspx?filter=Isaacs&filterby=Electorate&divid=219">Isaacs</a></td><td></td><td> </td>
</tr>
</table>
No comments:
Post a Comment