问题描述
如何使用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获取表的数据代码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!