Java 如何在Jsoup中提高解析速度

Java 如何在Jsoup中提高解析速度,java,parsing,jsoup,Java,Parsing,Jsoup,我正在使用Jsoup对一些站点进行解析, 但除此之外,我还需要访问大约20000个站点, 我不知道这个代码是不是设计优化 我还没有测试,但我担心处理速度太慢。。。 如果慢,我想改进更好的设计, 为了获得更好的性能,我必须从这段代码中修改哪些内容?一些修复: public static void main(String[] args) throws MalformedURLException, IOException{ Document doc = Jsoup.connect("

我正在使用Jsoup对一些站点进行解析, 但除此之外,我还需要访问大约20000个站点, 我不知道这个代码是不是设计优化 我还没有测试,但我担心处理速度太慢。。。 如果慢,我想改进更好的设计, 为了获得更好的性能,我必须从这段代码中修改哪些内容?

一些修复:

public static void main(String[] args) throws MalformedURLException, IOException{

        Document doc = Jsoup.connect("MY_URL").userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36").get(); 

        int i = 1;
        for(Element table : doc.select("tbody")){       

             for (Element row : table.select("tr")) {

                for (Element sale1 : row.select("td.sale_type.bottomline div.inner.pl4")){
                     System.out.print(i + " : " + sale1.text() + " / ");
                }
                for (Element sale2 : row.select("td.sale_type2.bottomline div.inner")){
                     System.out.print(sale2.text() + " / ");
                }
                for (Element date : row.select("td.bottomline div.inner.inner_mark span.mark4")){
                     System.out.print(date.text() + " / ");
                }
                for (Element add : row.select("td.align_l.name div.inner ")){
                     System.out.print(add.text() + " / ");
                }
                for (Element size : row.select("td.num div.inner ")){
                     System.out.print(size.text() + " / ");                 
                }
                for (Element floor : row.select("td.num2 div.inner ")){
                     System.out.print(floor.text()+ " / ");                 
                }
                for (Element price : row.select("td.num.align_r div.inner ")){
                     System.out.print(price.text()+ " / ");                 
                }
                for (Element cont : row.select("td.contact.bottomline div.inner ")){
                     System.out.println(cont.text());   i++;                
                }


             }
        }

    }


请格式化你的代码你的代码看起来不错。如果你还没有测试,那么我就不会担心速度了。当您发现处理速度太慢或出现错误时,开始担心它。如果您担心性能,您应该编写一个简单的性能测试,尽可能模拟您在生产中看到的行为类型。在您看到性能问题之前优化代码是没有意义的,无论是在生产环境中还是在类似于生产环境的性能测试中。啊。。。谢谢大家!我从很多网站上看到了很多种类的源代码,但是与我的代码相比,它看起来太简单了,bcz就是因为这个原因,我只是担心,然后就不需要格式了,对吧若你们问代码的质量,我想你们应该问
public static string getTableData(Document doc){
    StringBuilder sb = new StringBuilder();
    for(Element table : doc.select("tbody")){       
         int i = 0;
         for (Element row : table.select("tr")) {
            i++;
            for (Element sale1 : row.select("td.sale_type.bottomline div.inner.pl4")){
                 sb.Append(i + " : " + sale1.text() + " / ");
            }
            for (Element sale2 : row.select("td.sale_type2.bottomline div.inner")){
                 sb.Append(sale2.text() + " / ");
            }
            for (Element date : row.select("td.bottomline div.inner.inner_mark span.mark4")){
                 sb.Append(date.text() + " / ");
            }
            for (Element add : row.select("td.align_l.name div.inner ")){
                 sb.Append(add.text() + " / ");
            }
            for (Element size : row.select("td.num div.inner ")){
                 sb.Append(size.text() + " / ");                 
            }
            for (Element floor : row.select("td.num2 div.inner ")){
                 sb.Append(floor.text()+ " / ");                 
            }
            for (Element price : row.select("td.num.align_r div.inner ")){
                 sb.Append(price.text()+ " / ");                 
            }
            for (Element cont : row.select("td.contact.bottomline div.inner ")){
                 sb.Append(cont.text());                   
            }
         }
    }
    return sb.toString();
}
public static void main(String[] args) throws MalformedURLException, IOException{
    Document doc = Jsoup.connect("MY_URL").userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36").get(); 
    string result = getTableData(doc);
    System.out.println(result); 
}