0

So I'm trying to scrape some data from a WebPage, but unable to do so. I tried doing it using substring() but that's very inefficient. Here's part of the code which I've written :

           Elements links;

           Element link;

           String url = "https://www.premierleague.com/tables";

           Document document = Jsoup.connect(url).get();

           links = document.select("table");

           org.jsoup.nodes.Element table = document.select("table").get(0); 

           Elements rows = table.select("tr");

           org.jsoup.nodes.Element row = rows.get(1);

           Elements cols = row.select("td");

Can anyone help me by giving a few examples from the same link ?

1
  • Say, for example, position, team and points of the team ranked #1 Commented Dec 26, 2016 at 20:11

1 Answer 1

3
    String url = "https://www.premierleague.com/tables";
    Document doc = Jsoup.connect(url).get();
    Element table = doc.select("table").first();
    Iterator<Element> team = table.select("td[class=team]").iterator();
    Iterator<Element> rank = table.select("td[id=tooltip]").iterator();
    Iterator<Element> points = table.select("td[class=points]").iterator();
    System.out.println(team.next().text());
    System.out.println(rank.next().text()); 
    System.out.println(points.next().text());

output:

ChelseaCHE
1 Previous Position 1
46

Edit: to respond to your question:

        System.out.println(team.next().text());
        System.out.println(rank.next().text());
        System.out.println(points.next().text());
        team.next();
        team.next();
        team.next();

        rank.next();
        rank.next();
        rank.next();

        points.next();
        points.next();
        points.next();

        System.out.println(team.next().text());
        System.out.println(rank.next().text());
        System.out.println(points.next().text());

output:

ChelseaCHE
1 Previous Position 1
46
Tottenham HotspurTOT
5 Previous Position 5
33
Sign up to request clarification or add additional context in comments.

4 Comments

Khalil, but what should I do if I need the data separately for.. say, 5th, then 9th ranked team and so on...
I did.. also Im not able to scrape other fields which don't have a class like won,draw,lost,GD etc. can you provide some insight into that as well?
@CodePlorer in the code you should change td or tr and add anything specific to it be reconize , like this you 'll be able to scarpe it
Example please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.