Search code examples
javaandroid-studiojsoup

Jsoup .text method not found


I'm trying to scrape a website for data using jsoup so I can use it in an Android Studio project. When I try to use the .text method to get all the text of a document, it says "cannot resolve method" even though I think I imported all the right things. Is this a problem with my code or is it something else?

My Code:

Document doc = (Document) Jsoup.connect(url).get();
text = doc.text();

Edit: Found error, org.w3c.dom.Document was imported when the correct import is org.jsoup.nodes.Document


Solution

  • You need to import org.jsoup.nodes.Document.

    Demo:

    import java.io.IOException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            Document doc = (Document) Jsoup.connect("https://www.google.com").get();
            String text = doc.text();
            System.out.println(text);
        }
    }
    

    Output:

    Google We've detected you're using an older version of Chrome.Reinstall to stay secure × About Store We've detected you're using an older version of Chrome.Reinstall to stay secure × Gmail Images Sign in Remove Report inappropriate predictions × A privacy reminder from Google Remind me later Review now United KingdomPrivacyTermsSettingsSearch settingsAdvanced searchYour data in SearchHistorySearch helpSend feedbackAdvertisingBusiness How Search works