Search code examples
androidweb-scrapingjsoup

How to use JSOUP to get the data from website


I want to get the 'Fixtures' data from this page: [Link] using jsoup but I have no clue of how to get the data.


Solution

    1. Include Jsoup in gradle

      implementation "org.jsoup:jsoup:1.11.3"
      
    2. Connect to page

      Document doc = Jsoup.connect('url').get();
      
    3. Select and get the element by id or xpath...

      Elements el  = doc.getElementsByClass("col");
          for (int i = 0; i < el.size(); i++) {
              if (el.get(i).classNames().contains("col1")) {
                  Log.d("EL", el.html());
              }
              if (el.get(i).classNames().contains("col2")) {
                  Log.d("EL", el.html());
              }
              if (el.get(i).classNames().contains("col3")) {
                  Log.d("EL", el.html());
              }
          }
      

    p.s. You will need to handle asnyc call yourself Jsoup.connect will throw NetworkOnMainThreadException if you call it directly in activity.