我到处找这个,但没找到。我只是想使用通配符从 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_')]
您非常接近答案,只是 *
妨碍了您。
关于PHP DOM 属性通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017167/