我到处找了一遍,但一直找不到。我只想用通配符从html中选择元素。例如,对于我正在抓取的页面,这个选择器在Jquery的控制台中工作得很好:
$("tr[id^='informal_']")
换句话说,获取id以'informal_'开头的所有行。我试过xpath,但没有成功。xpath是XML独有的吗?不管怎样,如果有人有任何解决方案,我将不胜感激
编辑
我使用的xpath:
$doc = new DOMDocument($html);
$doc->strictErrorChecking = false;
$xpath = new DOMXPath($doc);
$table_rows = $xpath->query("//*tr[starts-with(@id, 'informal_')]");
解决方案我决定选择:http://code.google.com/p/phpquery/
这是代码:
require('phpQuery/phpQuery.php');
$doc = phpQuery::newDocumentHTML($html);;
$table_rows = $doc->find("tbody tr[id^='informal_']");
等价于jQuery选择器
tr[id^='informal_']
在XPath中,是
//tr[starts-with(@id, 'informal_')]
你非常接近答案,只是*
挡住了去路。
*tr
是无效的XPath,因为您将通配符与文本节点名混合使用。
您只需要*
,即*[starts-with...