使用JSoup获取表的数据代码值 [英] Using JSoup to get data-code value of a table

查看:59
本文介绍了使用JSoup获取表的数据代码值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JSoup从表行获取data-code值?

How would I be able to use JSoup to get the data-code value from a table row?

这是我尝试过的内容,但它什么也没打印出来:

Here is what I have tried but it just prints nothing:

Document doc = Jsoup.connect("http://www.example.com").get();
Elements dataCodes = doc.select("table[class=team-list]");

for (Element dataCode : dataCodes)
{
    System.out.println(dataCode.attr("data-code"));
}

HTML代码如下:

<body>
<div id-=""main">
    <div id="inner">
        <div id="table" class="scores-table">
            <table class ="team-list">
                <tbody>

                <tr data-code="1" class="data odd"></tr>
                <tr data-code="2" class="data even"></tr>
                <tr data-code="3" class="data odd"></tr>
                <tr data-code="1" class="data even"></tr>       

                </tbody>
            </table>
        </div>
    </div>
</div>

我希望它在表的每一行上打印出数据代码值(这是团队编号).

I want it to print out the data-code value on each row of the table (which is the team number).

推荐答案

您的选择器应转到tr元素:

Your selector should go down to tr elements:

Elements dataCodes = doc.select("table.team-list tr");


根据注释,这仍然会导致一个空列表-在这种情况下,该表可能是在JavaScript逻辑或单独的AJAX请求的帮助下动态生成的.


According to the comments, this still results into an empty list - in this case, the table is probably dynamically generated with the help of javascript logic or a separate AJAX request.

在这种情况下,一种可能的方法是让真正的浏览器处理动态javascript AJAX部分.尝试 selenium浏览器自动化框架:

In this case, one of the possible approaches would be to have a real browser handle that dynamic javascript, AJAX part. Try selenium browser automation framework:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");

List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list tr"));

for(WebElement element: elements)
{
    System.out.println(element.getAttribute('data-code'));
}

这篇关于使用JSoup获取表的数据代码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆