我是个新手,正在尝试获取"title data-original-title“属性的属性值,但是得到了一个空字符串。我想要价值
Jul-30-2015 03:26:13 PM
<table class="table table-hover">
<thead>
<tr style="border-color: #E1E1E1; border-width: 1px; background-color: #F9F9F9; border-top-style: solid;">
<th>Height</th>
<th>Age</th>
<th>txn</th>
<th>Uncles</th>
<th>Miner</th>
<th>GasUsed</th>
<th>GasLimit</th>
<th>Avg.GasPrice</th>
<th>Reward</th>
</tr>
</thead>
<tbody>
<tr><td></td>
<td>
**<span rel="tooltip" data-placement="bottom" title="" data-original-title="Jul-30-2015 03:26:13 PM">1149 days 18 hrs ago</span>**
</td>
我的代码是
for (int i = total_pages; i >= 1; i--) {
System.out.println("\nDisplaying blocks on page " + i);
String newString = "https://etherscan.io/blocks?p=" + i;
Document d3 = Jsoup.connect(newString).get();
Elements e = d3.select("table.table-hover > tbody");
Elements r = e.get(0).select("tr");
for (Element cr : r) {
Elements test = d3.select("span");
System.out.println(test.attr("data-original-title"));
}
}
任何帮助都将不胜感激。我修改了属性值以获得数据放置值,并且可以正确地检索到它。但是data-original-title仍然返回空字符串。
发布于 2018-09-24 07:04:17
数据属性是一种特殊的属性,因此访问它们有点不同,但仍然非常容易。而不是
System.out.println(test.attr("data-original-title"));
使用:
System.out.println(test.first().dataset().get("original-title"));
发布于 2018-09-22 18:16:19
您可以尝试查看此方法是否有效:
d3.select("span[data-original-title]").get(0).attr("data-original-title")
解释:
这将查找包含属性"data-original-title“的第一个span,并获取该属性的值。
https://stackoverflow.com/questions/52455578
复制相似问题