【发布时间】:2014-04-14 14:58:18
【问题描述】:
我要定位的 HTML 页面的 divs 如下所示:
<div class="white-row1">
<div class="results">
<div class="profile">
<a href="hrefThatIWant.com" class>
<img src = "http://imgsource.jpg" border="0" width="150" height="150 alt>
</a>
</div>
</div>
</div>
<div class="white-row2">
// same content as the div above
</div>
我想废弃收集列表中每个 div 中的 href。
这是我当前的代码:
List<HtmlAnchor> profileDivLinks = (List)htmlPage.getByXPath("//div[@class='profile']//@href");
for(HtmlAnchor link:profileDivLinks)
{
System.out.println(link.getHrefAttribute());
}
这是我收到的错误(出现在 for 语句的第一行):
Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.DomAttr cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlAnchor
你认为问题是什么?
【问题讨论】:
标签: html xpath web-scraping href htmlunit