1

我正在尝试访问一个 url,从中获取 html 并使用 xpaths 从中获取某些值。我得到的 html 很好,Jtidy 似乎正在适当地清理它。但是,当我尝试使用 xpaths 获取所需的值时,我得到一个空的 NodeList。我知道我的 xpath 表达式是正确的;我已经以其他方式对其进行了测试。这段代码有什么问题。谢谢您的帮助。

String url_string = base_url + countries[c];
URL url = new URL(url_string);

Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXHTML(true);
tidy.setMakeClean(true);
Document doc = tidy.parseDOM(url.openStream(), null);
//tidy.pprint(doc, System.out);

String xpath_string = "id('catlisting')//a";
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpath_string);

NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
System.out.println("size="+nodes.getLength());
for (int r=0; r<nodes.getLength(); r++) {
    System.out.println(nodes.item(r).getNodeValue()); 
}
4

1 回答 1

2

试试“//div[@id='catlisting']//a”

于 2009-07-31T08:40:09.280 回答