简体   繁体   中英

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. I am a beginner and couldn't make the file export properly as a 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:

    <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. Maybe it helps you as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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