简体   繁体   English

如何将我的 html 解析数据保存为 CSV 文件?

[英]How can I save my html parsed data as a CSV file?

I have made all the necessary imports ( Jsoup Elements and Element ) and the code runs well.我已经进行了所有必要的导入( Jsoup Elements and Element )并且代码运行良好。 I am a beginner and couldn't make the file export properly as a CSV.我是初学者,无法将文件正确导出为 CSV。 The data either stays on the same row or the same column.数据要么位于同一行,要么位于同一列。

Document document = Jsoup.connect("https://www.investing.com/markets/united-states").get();
Elements tables = document.select("tr");
                            
tables.stream().forEach(e ->

                {
                    String values = e.text();                               
                    String csValues = String.join(",", values);
                    System.out.println(csValues);               
                    
                });
                    }
}

Looking for help.寻求帮助。 Thank you.谢谢你。

As I do not know your exact structure that you get when the scraping is finished I cannot recommend a concrete piece of code, but I can recommend a library that I had a lot of success with.由于我不知道你在抓取完成后得到的确切结构,所以我不能推荐一段具体的代码,但我可以推荐一个我取得了很大成功的库。 That is the OpenCSV library:那是 OpenCSV 库:

    <dependency>
      <groupId>com.opencsv</groupId>
      <artifactId>opencsv</artifactId>
      <version>5.3</version>
    </dependency>

There is an excellent Baeldung article which shows how to use it in certain scenarios: Baeldung: Introduction to OpenCSV It helped me a lot and I found it to be much easier than apache-commons-csv.有一篇优秀的 Baeldung 文章展示了如何在某些场景中使用它: Baeldung: Introduction to OpenCSV它对我帮助很大,我发现它比 apache-commons-csv 容易得多。 Maybe it helps you as well.也许它对你也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM