簡體   English   中英

使用jsoup提取html代碼-兩個彼此相鄰的span標簽

[英]extract html code with jsoup - two span tags next to each other

我的html代碼如下所示:

<div class="cloud recommended">
    <div id="bigcloud" class="eventwithfoto">
        <h1>Artist Name</h1>
        <div id="eventphoto">
            <a href="http://linkToPhoto.jpg" target="_top" rel="lightbox"><img src="http://linkToPhoto.jpg" height="150"></a>
        </div>

        <div id="eventmain" style="margin-top: 12px;">
            <p id="eventwhere"><span><b>Name of place<br></b></span><span>Address of place</span>
            <br> tel.: +48 111 222 111 <br><a href="http://www.linktoplace.com" target="_blank">http://www.linktoplace.com</a> </p>
            <p id="eventdate">2017-04-20 godz. 20:00</p>


            <div id="eventadmission">
                120 zł
            </div>

        </div>

        <div class="clear"></div>
        <div id="eventdesc">
            Here is some descr<br/>Some other descr 
            <div class="clear"></div>

            <br>
                <a href="http://link.com" target="_blank">link to event</a>
        </div>
    </div>
</div>

現在我想用JSoup解析它以獲得必要的信息。

我創建了以下方法:

System.out.println(address);
Document doc = Jsoup.connect(address).timeout(10*1000).get();


String placePhoneNo = doc.select("p#eventwhere > br").text();
String placeAddress = doc.getElementsByTag("#eventwhere").text();

但是每個字符串都是空的。 我在這里做錯了什么? 如何解析這種特定的html格式以獲取那些變量?

編寫了一個小樣本進行演示:

package sandbox.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsoupMain {
    private final String HTML = "<div class=\"cloud recommended\">\n" +
            "    <div id=\"bigcloud\" class=\"eventwithfoto\">\n" +
            "        <h1>Artist Name</h1>\n" +
            "        <div id=\"eventphoto\">\n" +
            "            <a href=\"http://linkToPhoto.jpg\" target=\"_top\" rel=\"lightbox\"><img src=\"http://linkToPhoto.jpg\" height=\"150\"></a>\n" +
            "        </div>\n" +
            "\n" +
            "        <div id=\"eventmain\" style=\"margin-top: 12px;\">\n" +
            "            <p id=\"eventwhere\"><span><b>Name of place<br></b></span><span>Address of place</span>\n" +
            "            <br> tel.: +48 111 222 111 <br><a href=\"http://www.linktoplace.com\" target=\"_blank\">http://www.linktoplace.com</a> </p>\n" +
            "            <p id=\"eventdate\">2017-04-20 godz. 20:00</p>\n" +
            "\n" +
            "\n" +
            "            <div id=\"eventadmission\">\n" +
            "                120 zł\n" +
            "            </div>\n" +
            "\n" +
            "        </div>\n" +
            "\n" +
            "        <div class=\"clear\"></div>\n" +
            "        <div id=\"eventdesc\">\n" +
            "            Here is some descr<br/>Some other descr \n" +
            "            <div class=\"clear\"></div>\n" +
            "\n" +
            "            <br>\n" +
            "                <a href=\"http://link.com\" target=\"_blank\">link to event</a>\n" +
            "        </div>\n" +
            "    </div>\n" +
            "</div>";

    public static void main(String[] args) {
        new JsoupMain().findTwoSpans();
    }

    private void findTwoSpans() {
        Document doc = Jsoup.parse(HTML);
        Element eventWhere = doc.getElementById("eventwhere");
        Elements spans = eventWhere.select("span");
        System.out.println("span[0]="+spans.get(0).text());
        Element spanTwo = spans.get(1);
        System.out.println("span[1]="+spanTwo.text());

        // Get phone
        Element eventMain = doc.getElementById("eventmain");
        String textMain = eventMain.after(spanTwo).after("br").text();

        int beginPos = textMain.indexOf("tel.: ");
        int endPos = textMain.indexOf(" http://");
        if (beginPos>0 && endPos>0) {
            String phone = textMain.substring(beginPos+6, endPos);
            System.out.println("Found phone: "+phone);
        }
        else {
            System.out.println("Phone not found: "+textMain);
        }
    }
}

輸出量

span[0]=Name of place
span[1]=Address of place
Found phone: +48 111 222 111

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM