我正在使用HTML Parser從網頁獲取鏈接。我需要將URL,鏈接文本和URL存儲到包含鏈接的父頁面。我設法獲取鏈接網址以及父網址。HTML解析器獲取鏈接文本
我仍然需要獲取鏈接文本。
<a href="url">link text</a>
不幸的是我很難搞清楚,任何幫助將不勝感激。
public static List<LinkContainer> findUrls(String resource) {
String[] tagNames = {"A", "AREA"};
List<LinkContainer> urls = new ArrayList<LinkContainer>();
Tag tag;
String url;
String sourceUrl;
try {
for (String tagName : tagNames) {
Parser parser = new Parser(resource);
NodeList nodes = parser.parse(new TagNameFilter(tagName));
NodeIterator i = nodes.elements();
while (i.hasMoreNodes()) {
tag = (Tag) i.nextNode();
url = tag.getAttribute("href");
sourceUrl = tag.getPage().getUrl();
if (RegexUtil.verifyUrl(url)) {
urls.add(new LinkContainer(url, null, sourceUrl));
}
}
}
} catch (ParserException pe) {
pe.printStackTrace();
}
return urls;
}
如果'.getAttribute()'工作,我認爲'.innerHTML'或'.innerText'會工作...或'.nodeValue'? – scunliffe 2010-09-12 20:16:40