简体   繁体   中英

JSoup get specific data from webpage

I've been trying to get data from: http://www.betvictor.com/sports/en/to-lead-anytime , where I would like to get the list of matches using JSoup.

For example: Caen v AS Saint Etienne Celtic v Rangers

and so on...

My current code is:

String couponPage = "http://www.betvictor.com/sports/en/to-lead-anytime";
Document doc1 = Jsoup.connect(couponPage).get();


    String match = doc1.select("#coupon_143751140 > table:nth-child(3) > tbody > tr:nth-child(2) > td.event_description").text();
    System.out.println("match:" + match);

Once I can figure out how to get one item of data, I will put it in a for loop to loop through the whole table, but first I need to get one item of data.

Currently, the output is "match: " so it looks like the "match" variable is empty.

Any help is most appreciated,

I have worked out how to answer my question after a few hours of experimenting. Turns out the page didn't load properly straight away, and had to implement the "timeout" method.

Document doc;
try {

    // need http protocol
    doc = Jsoup.connect("http://www.betvictor.com/sports/en/football/coupons/100/0/0/43438/0/100/0/0/0/0/1").timeout(10000).get();

    // get all links
    Elements matches = doc.select("td.event_description a");
    Elements odds = doc.select("td.event_description a");
    for (Element match : matches) {

        // get the value from href attribute

        String matchEvent = match.text();
        String[] parts = matchEvent.split(" v ");
        String team1 = parts[0];
        String team2 = parts[1];
        System.out.println("text : " + team1 + " v " + team2);

    }

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