Jsoup prasring标题h2

时间:2014-02-24 11:11:40

标签: java html parsing dom jsoup

大家好,我正在尝试解析此网站http://feed43.com/alytus_06.xml的标题文字。

html代码:

<html>
<head></head>
<body onload="doLoad()">
    <div class="body feed-preview">
        <table class="main" style="margin-top:10px">...</table>
        <div class ="main">
            <h1>ALYTUS_06</h1>
            <p>ALYTUS_06</p>
            <p>...</p>
            <p class="date-preview" style="margin-bottom:0px;">Last Updated: Mon, 24 Feb 2014 10:01:54 GMT</p>
        </div>
        <div class ="main">
            <h2>
                <span class="bullet">&nbsp;</span>
                "-1"
            </h2>
        <div class="p" id="item_1">...</div>
        <h2>..</h2>
        <div class="p" id="item_2">...</div>
        </div>
        <div class="main footer">       
              Feed43 v. 1.3. Copyright © 2006–2011 A.I.Studio. All rights reserved.
        </div>
    </div>
</body>

    public static void main( String[] args ) throws IOException
{
    Document doc = Jsoup.parse("UTF-8", "http://feed43.com/alytus_06.xml");
        for (Element e : doc.select("h2")) {
            System.out.println(e.text());
        }
}

我无法从此代码中的div中从div.class main中提取温度“-1”,也许有人可以帮助我?

1 个答案:

答案 0 :(得分:1)

要获得第一个元素,请更改您的代码

Document doc = Jsoup.connect("http://feed43.com/alytus_06.xml").get();
Element ele=doc.select("item title").first();
System.out.println(ele.text());

在项目

中获取带有标题标签的所有元素
Document doc = Jsoup.connect("http://feed43.com/alytus_06.xml").get();
Elements ele=doc.select("item title");
for(Element element:ele)
{
   System.out.println(element.text());
}