简体   繁体   English

JSoup从网页获取特定数据

[英]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. 我一直在尝试从以下网址获取数据: http : //www.betvictor.com/sports/en/to-lead-anytime ,我想使用JSoup获取比赛清单。

For example: Caen v AS Saint Etienne Celtic v Rangers 例如: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. 一旦弄清楚如何获取一个数据项,就将其放入for循环中以遍历整个表,但是首先我需要获取一个数据项。

Currently, the output is "match: " so it looks like the "match" variable is empty. 当前,输出为“ match:”,因此看起来“ match”变量为空。

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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM