【发布时间】:2019-02-27 21:34:07
【问题描述】:
网址:https://stats.nba.com/player/1628381/defense-dash/
尝试获取:
`<table>
<tbody>
<!----><tr data-ng-repeat="(i, row) in page" index="0">
<td class="player">Overall</td>
<td>45</td>
<td>45</td>
<td>5.7</td>
<td>12.3</td>
<td>46.6</td>
<td>100%</td>
<td>46.7</td>
<td>-0.1</td>
</tr><!---->
</tbody>
</table> `
我的编码:
public static void getData(String url, String Name, int ID) throws
IOException
{
String html = Jsoup.connect(url).execute().body();
html = html.replaceAll("<!---->", "");
html = html.replaceAll("<!--", "");
html = html.replaceAll("-->", "");
Document doc = Jsoup.parse(html);
Elements tableElements = doc.select("table");
System.out.println("Elements " + tableElements);
for (Element tableElement : tableElements)
{
String tableId = tableElement.id();
if (tableId.isEmpty()) {
continue;
}
String fileName = "table" + Name + tableId + ID + ".csv";
System.out.println(fileName);
FileWriter writer = new FileWriter(new File("C:\\Users\\noman\\eclipse-workspace\\Senior Project\\src\\", fileName));
//System.out.println(doc);
Elements tableRowElements = tableElement.select(":not(thead) tr td");
for (int i = 0; i < tableRowElements.size(); i++) {
Element row = tableRowElements.get(i);
Elements rowItems = row.select("td");
for (int j = 0; j < rowItems.size(); j++) {
writer.append(rowItems.get(j).text());
if (j != rowItems.size() - 1) {
writer.append(',');
}
}
writer.append('\n');
}
问题是没有找到任何元素。相同的代码在另一个站点上完美运行,(似乎)它们存储数据的方式没有区别
这个网站在防止网络抓取方面有什么不同吗?还是有细微的差别?
请注意提供的 HTML 代码是一个缩短版本
【问题讨论】:
-
内容可能是由 javascript 添加的。查看 jsoup 检索到的 html,看看是否存在。
-
那些是javascript还是?
-
那些是您要查找的表格元素
-
实际上没有看到它们。文档很长,可能我错过了
-
我在这些数据上找到了标签:
- 整体防守投篮数
标签: java web-scraping jsoup