我正在创建一个用于游戏的网络抓取工具。 这是我要抓取的网站:http://forum.toribash.com/clan_war.php?clanid=139
我想计算出现在“显示详细信息”上的名称的出现频率。
我已经读过此Get content from javascript onClick hyperlink,但不知道这实际上是我要搜索的内容。我怀疑这不是我要寻找的东西,但是不管我没有尝试回答这些问题,因为我不知道如何使此https://stackoverflow.com/a/12268561/10467473适应我的需求。
BufferedReader month = new BufferedReader(new InputStreamReader(System.in));
String mth = month.readLine();
//Accessing the website
Document docs = Jsoup.connect("http://forum.toribash.com/clan_war.php?clanid=139").get();
//Taking every entry of war history
Elements collection = docs.getElementsByClass("war_history_entry");
//Itterate every collection
for(Element e : collection){
//if the info is on the exact month that are being searched we will use the e
if(e.getElementsByClass("war_info").text().split(" ")[1].equalsIgnoreCase(mth)){
//supposedly it holds every element that has player as it class inside of the button onclick
//But it doesn't work
Elements cek = e.getElementsByClass("player");
for(Element c : cek){
System.out.println(c.text());
}
}
目前,我希望至少能在显示详细信息表上找到该名称
Kaito
Chax
Draku
以此类推
答案 0 :(得分:1)
此页面不包含您要抓取的信息。单击按钮后,结果将由AJAX(Javascript)加载。 您可以使用Web浏览器的调试器在“网络”选项卡上查看,以查看单击按钮后会发生什么。 点击按钮
<button id="buttonwarid19557" ... >
从URL加载表:
http://forum.toribash.com/clan_war_ajax.php?warid=19557&clanid=139
注意相同的ID号。
您要做的是从每个按钮获取ID,然后为每个按钮获取另一个文档并逐个解析。无论如何,这就是您的网络浏览器所做的。
BufferedReader month = new BufferedReader(new InputStreamReader(System.in));
String mth = month.readLine();
//Accessing the website
Document docs = Jsoup.connect("http://forum.toribash.com/clan_war.php?clanid=139").get();
//Taking every entry of war history
Elements collection = docs.getElementsByClass("war_history_entry");
//Itterate every collection
for(Element e : collection){
//if the info is on the exact month that are being searched we will use the e
if(e.getElementsByClass("war_info").text().split(" ")[1].equalsIgnoreCase(mth)){
// selecting button
Element button = e.selectFirst("button");
// getting warid from button id
String buttonId = button.attr("id");
// removing text because we need only number
String warId = buttonId.replace("buttonwarid", "");
System.out.println("downloading results for " + e.getElementsByClass("war_info").text());
// downloading and parsing subpage containing table with info about single war
// adding referrer to make the request look more like it comes from the real web browser to avoid possible hotlinking protection
Document table = Jsoup.connect("http://forum.toribash.com/clan_war_ajax.php?warid=" + warId + "&clanid=139").referrer("http://forum.toribash.com/clan_war.php?clanid=139").get();
// get every <td class="player"> ... </td>
Elements players = table.select(".player");
for(Element player : players){
System.out.println(player.text());
}
}
}