试图从AEC网站提取一些信息(例如http://apps.aec.gov.au/eSearch/LocalitySearchResults.aspx?filter=3977&filterby=Postcode)。我正在运行的XPath查询是“//x:tbody/x:tr/x:td[4]/x:a
”,我已经在XPath Checker(Firefox扩展)中进行了测试,并且它提取了相关的本地数据。通过PHP中的XPath提取信息
我然后使用PHP来加载页面,执行查询,然后遍历结果。
$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;
}
我然后让:
Warning: Invalid argument supplied for foreach() in /home/givesh5/public_html/dig/electoratesearch.php on line 41
看来,查询返回某种布尔而不是查询匹配列表?
相关标记如下:
<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>
'DOMXpath'如果*表达式的格式不正确或contextnode无效* – adeneo 2015-04-04 09:19:33
能否请您提供标记的相关部分返回false你正在解析。从Firefox派生的XPath来自可包含隐含标记的实时DOM。所以以这种方式得到它们是不可靠的。此外,你究竟想要获取什么? – Gordon 2015-04-04 09:30:59
用标记更新了OP,谢谢。在这种情况下,试图获取本地链接文本(例如文本)。例如,在前两个单元中,这将是“弗林德斯”。 –
Edward
2015-04-04 09:35:16