gpt4 book ai didi

java - JSOUP 计数 Map 标签中的总元素数

转载 作者:行者123 更新时间:2023-12-02 10:39:39 25 4
gpt4 key购买 nike

您好,我正在尝试使用 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: Unresolved 编译问题: 只能迭代数组或 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)

最佳答案

您正在尝试迭代 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();
}

关于java - JSOUP 计数 Map 标签中的总元素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com