【问题标题】:How to catch this text with Jsoup?如何用 Jsoup 捕捉这段文字?
【发布时间】:2013-11-25 22:52:49
【问题描述】:

我只是想从这个源代码中提取一些文本而发疯:

<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...">

我尝试了很多构造函数的组合,但如果没有任何建议,我真的无法做到这一点...我需要在标题之后的 " 之间捕捉文本...

请注意,有一个类似的类,称为“odd”,与第一个具有相同的语法,就是这样:

<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...">

所以,我需要一些能捕捉到这两个类上写的文字的东西……

感谢您的帮助。

编辑:这是我的代码,我在其中连接并捕获一些链接:

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

没用。

【问题讨论】:

    标签: java html css parsing jsoup


    【解决方案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...
    

    【讨论】:

    • 我自己刚刚完成的!嗯,有点! :) 你的代码比我的更清楚! :) 无论如何感谢您的回答,我会用您的版本更改我的代码! :)
    • 如果您先自己弄清楚,那就更好了 :) 很高兴能提供帮助。
    【解决方案2】:

    这是一个示例:

    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...
    

    【讨论】:

    • 感谢您的回答,但是源页面是网页,所以我需要连接到它。此外,在该代码之后,还有其他代码,包含在同一类中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2018-04-26
    • 1970-01-01
    • 2013-07-17
    • 2014-12-03
    • 2014-07-04
    相关资源
    最近更新 更多