Tano
Tano

Reputation: 619

Java HTML parsing (storing as String)

I am parsing a web page, but I got some problem, the page consists a lot of such elements:

<div class="tweet">
        <a href="https://twitter.com/Sweden" target="_blank" class="tweet__link">@sweden</a>
        <span class="tweet__timestamp"><a href="https://twitter.com/sweden/status/694285861026926594" target="_blank" class="tweet__permalink">Feb. 1, 2016, 11:27 p.m.</a></span>
        <p class="tweet__content"><a href='http://twitter.com/UnbatedFlunky' target='_blank'>@UnbatedFlunky</a> Good to know. :)</p>
    </div>

    <div class="tweet">
        <a href="https://twitter.com/Sweden" target="_blank" class="tweet__link">@sweden</a>
        <span class="tweet__timestamp"><a href="https://twitter.com/sweden/status/694285696140513280" target="_blank" class="tweet__permalink">Feb. 1, 2016, 11:26 p.m.</a></span>
        <p class="tweet__content">RT <a href='http://twitter.com/UnbatedFlunky' target='_blank'>@UnbatedFlunky</a>: .<a href='http://twitter.com/sweden' target='_blank'>@sweden</a> exactly the kind of content I'd want representing my country. 10/10</p>
    </div>

I want to put the content in each tweet class in separate string, I have so far this code:

Document doc = Jsoup.connect("http://curatorsofsweden.com/curator/aleksandra-boscanin/").get();
        Element e = doc.select("div").first();
            String text = doc.getElementsByClass("tweet").text();

but in this way I am storing all the content in one single string, but my question is how I can put them separately for example lets say String array :/ Maybe its a stupid question but I could not make it work :/

Upvotes: 0

Views: 54

Answers (1)

Dropout
Dropout

Reputation: 13866

doc.getElementsByClass("tweet") returns an array over which you should iterate and create an array entry for each of the tweet elements. For example

List<String> stringList = new ArrayList<>();
List<Element> tweets = doc.getElementsByClass("tweet");
for(Element tweet : tweets){
    stringList.add(tweet.text()); 
}

the texts will be in the stringList list.

Upvotes: 2

Related Questions