嗨,我正在尝试使用Java制作网络爬虫,为此,我想计算当前页面上的部分总数。这些部分位于Map标记中的某些区域标记中。我正在使用Jsoup来获取标签,但仍然没有运气。它一直显示
我的代码:
int totalSections=0;
/*Page address for searching*/
Document doc = Jsoup.connect("sitename").get();
/*Getting anchor tags*/
Element area = doc.getElementsByTag("map");
int total = area.childNodeSize();
System.out.println(total);
/*Looping through the page for total pages*/
for(Element aTag: area){
System.out.println("inside");
totalSections++;
System.out.println(aTag.val());
}
System.out.println(totalSections);
产生错误:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.Error: Unresolved compilation problem:
Can only iterate over an array or an instance of java.lang.Iterable
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1053)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)root cause
java.lang.Error:未解决的编译问题: 只能迭代java.lang.Iterable的数组或实例
com.controller.CrawlerController.GetTotalSections(CrawlerController.java:126)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:1)
您正在尝试遍历Element
而不是Elements
。
替换:
Element area = doc.getElementsByTag("map");
int total = area.childNodeSize();
具有:
Elements area = doc.getElementsByTag("map");
int total = 0;
for (Element singleArea : area) {
total += singleArea.childNodeSize();
}