页面网址:https://www.basketball-reference.com/players/c/collijo01.html
表格位置:<tr id="per_game.2019 class="full_table">
我想要的元素:<td class="right" data-stat="fg3a_per_g">2.5</td>
2.5 就是我想要的。我曾经抓取整个表,然后循环获取这些信息,但我发现这比尝试根据数据统计名称进行抓取需要更多工作。但到目前为止我失败了
问题是:jsoup可以根据data-stat部分抓取信息吗?
代码:
public static void getDataTest(String url) throws IOException
{
String html = Jsoup.connect(url).execute().body();
html = html.replaceAll("<!--", "");
html = html.replaceAll("-->", "");
Document doc = Jsoup.parse(html);
Element tableElements = doc.getElementById("table#fg3a_per_g");
System.out.print("Element found was: " + tableElements);
}
注意我也尝试过“fg3a_per_g”本身没有表
最佳答案
Can jsoup grab info based on data-stat section?
当然:
Element element = doc.select("td[data-stat=fg3a_per_g]").first();
你所说的section
实际上叫做attribute
。另请参阅更多 CSS 选择器。您不能将所有这些都与 Jsoup 一起使用,但基本的都受支持并且非常有用:
https://www.w3schools.com/cssref/css_selectors.asp
关于java - 如何根据 data-stat 从表中获取 1 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249473/