Получение содержимого таблицы, которая скрыта с помощью javascript кнопки onclick с помощью JSoup - PullRequest
0 голосов
/ 30 января 2019

Я создаю веб-скребинг для личного использования в играх.Это веб-сайт, который я собираюсь очистить: http://forum.toribash.com/clan_war.php?clanid=139

И я хочу посчитать частоту имени, которое появляется на «показывает детали».

Я прочитал это Получить контент из javascript onClick по гиперссылке , не зная, что это действительно то, что я ищу.Я сомневаюсь, что это не то, что я ищу, но, несмотря на это, я не пробовал ответить на эти вопросы, так как не знаю, как сделать это 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

и так далее

1 Ответ

0 голосов
/ 30 января 2019

Эта страница не содержит информацию, которую вы хотите очистить.Результаты загружаются AJAX (Javascript) после нажатия кнопки.Вы можете использовать отладчик вашего веб-браузера, чтобы посмотреть на вкладке Сеть, чтобы увидеть, что происходит, когда вы нажимаете кнопку.Нажатие кнопки

<button id="buttonwarid19557"  ... >

загружает таблицу с URL:

http://forum.toribash.com/clan_war_ajax.php?warid=19557&clanid=139

Обратите внимание на тот же идентификационный номер.

Что вы должны сделатьчтобы получить идентификатор каждой кнопки, затем получить другой документ для каждой из этих кнопок и проанализировать его одну за другой.Вот что делает ваш веб-браузер.

        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());
                }
            }
        }
...