2013-11-25 3 views
0

я схожу с ума просто за то, чтобы извлечь текст из этого исходного кода:Как поймать этот текст с помощью Jsoup?

<tr class="even"> <!-- Title --> <td class="title riot" title="Summoners, We will be performing Live Maintenance on the 26/11 at 04:00 AM, where we will need to bring the EUW Platform offline. Following up...">

Я пробовал много комбинаций конструкторов, но я не могу сделать это без каких-либо рекомендаций ... Мне нужно поймать текст между «после заголовка ...

Пожалуйста, обратите внимание, что есть аналогичный класс, называемый« нечетным », который имеет тот же синтаксис первого, и это он:

<tr class="odd"> 
<!-- Title --> 
<td class="title riot" title="Summoners, welcome to the Service Status forum! Here you can come to see information regarding ongoing issues or events that we are currently working..."> 

Итак, мне нужно что-то, что может поймать текст, написанный на обоих классах ...

Спасибо за помощь.

EDIT: Вот мой код, где я соединяюсь и поймать несколько ссылок:

Document doc = Jsoup.connect("http://forums.euw.leagueoflegends.com/board/forumdisplay.php?f=10") 
            .userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22") 
            .timeout(30000).get(); 
        Elements links = doc.select("a[href*=thread]"); 
        for (Element link : links){ 
         if(link.attr("href").contains("board")||link.attr("href").contains("page")||link.text().matches("1")){} 
         else{ 
          titles.add((String) link.text()); 

          //descriptions.add((String) DEFAULT_FORUM_URL + link.attr("href")); 
          descriptions.add((String) doc.select("[title*=a]").toString()); 
         } 
        } 

Закомментированный линия пишет на каждом втором ряду в ListView, ссылка нити, но мне нужно написать есть краткое описание, которое находится между этими тегами «td class =» title riot «title =», из каждого класса.

Естественно, эта линия

descriptions.add((String) doc.select("[title*=a]").toString()); 

не работает.

ответ

1

Как об этом:

Document doc = Jsoup.connect("http://forums.euw.leagueoflegends.com/board/forumdisplay.php?f=10").get(); 

for (Element element : doc.select("tr.odd > td, tr.even > td")) { 
    System.out.println(element.attr("title")); 
} 

Что будет:

Summoners, welcome to the Service Status forum! Here you can come to see information regarding ongoing issues or events that we are currently working... 




Summoners, 

We will be performing a maintenance on 26/11 at 04:00 AM, where we will need to bring the EUW Platform offline. 

Following up on the... 
+0

Я только что сделал это сам! Ну, что-то вроде! :) Ваш код более ясный, чем мой! :) Спасибо за ответ в любом случае, я изменю свой код с вашей версией! :) – Pipodi

+0

Еще лучше, если вы сами это выяснили для себя :) Рад помочь в любом случае. – ashatte

0

Вот пример:

public static final String text = "" + 
    "<table><tr class=\"even\"> <!-- Title -->\n" + 
    " <td class=\"title riot\"\n" + 
    "  title=\"Summoners, We will be performing Live Maintenance on the 26/11 at 04:00 AM, where we will need to bring the EUW Platform offline. Following up...\">\n" + 
    " </td>\n" + 
    "</tr>\n" + 
    "<tr class=\"odd\">\n" + 
    " <!-- Title -->\n" + 
    " <td class=\"title riot\"\n" + 
    "  title=\"Summoners, welcome to the Service Status forum! Here you can come to see information regarding ongoing issues or events that we are currently working...\">\n" + 
    " </td>\n" + 
    "</tr></table>"; 

public static void main(String[] args) throws IOException { 
    Document doc = Jsoup.parse(text); 

    //System.out.println("your doc:" + doc); 

    for (Element element : doc.select("tr > td")) { 
     System.out.println(element.attr("title")); 
    } 
} 

Печать:

Summoners, We will be performing Live Maintenance on the 26/11 at 04:00 AM, where we will need to bring the EUW Platform offline. Following up... 
Summoners, welcome to the Service Status forum! Here you can come to see information regarding ongoing issues or events that we are currently working... 
+0

Спасибо за ответ, но исходная страница - это веб-страница, поэтому мне нужно подключиться к ней. Более того, после этого кода есть другой код, включенный в один класс. – Pipodi

Смежные вопросы