使用 JSoup 从 HTML 中提取数据

Extract Data from HTML using JSoup

我正在编写脚本以从 HTML 文档中提取数据。这是文档的一部分。

<div class="info">
<div id="info_box" class="inf_clear">
    <div id="restaurant_info_box_left">
        <table id="rest_logo">
            <tr>
                <td>
                    <a itemprop="url" title="XYZ" href="XYZ.com">
                        <img src="/files/logo/26721.jpg" alt="XYZ" title="XYZ" width="100" />
                    </a>
                </td>
            </tr>
        </table>
        <h1 id="Name"><a class="fn org url" rel="Order Online" href="XYZ.com" title="XYZ" itemprop="name">XYZ</a></h1>

        <div class="rest_data" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">

            <span itemprop="telephone">(305) 535-1379</span> | <b>
            <span itemprop="streetAddress">1755 Alton Rd</span>,
            <span itemprop="addressLocality">Miami Beach</span>,
            <span itemprop="addressRegion">FL</span>
            <span itemprop="postalCode">33139</span></b>
        </div>
        <div class="geo">
            <span class="latitude" title="25.792588"></span>
            <span class="longitude" title="-80.141214"></span>
        </div>
        <div class="rest_data">Estimated delivery time: <b>45-60 min</b></div>
    </div>

</div>

我正在使用 Jsoup,但不太确定如何实现这一点。

文档中有很多div标签,我尝试匹配它们的独特属性。 将具有 class 属性值的 div 标签说成 "info"

   Elements divs = doc.select("div");

        for (Element div : divs) {
            String divClass = div.attr("class").toString();
            if (divClass.equalsIgnoreCase("rest_info")) {
}

如果匹配,我必须在 div 标签内获取带有 id "rest_logo" 的 table

当使用 doc.select("table") 时,看起来解析器会搜索整个文档。

我需要实现的是,如果 div 标签属性匹配,我需要获取匹配的 div 标签内的 elementsattributes

Expected Output: 

Name : XYZ

telephone:(305) 535-1379

streetAddress:1755 Alton Rd

addressLocality:Miami Beach

addressRegion:FL

postalCode:33139

latitude:25.792588

longitude:-80.141214

Estimated delivery time:45-60 min

有什么想法吗?

    for (Element e : doc.select("div.info")) {
        System.out.println("Name: " + e.select("a.fn").text());
        System.out.println("telephone: " + e.select("span[itemprop=telephone]").text());
        System.out.println("streetAddress: " + e.select("span[itemprop=streetAddress]").text());
        // .....
    }

我会这样做:

Document doc = Jsoup. parse(myHtml);

Elements elements = doc.select("div.info")
    .select(”a[itemprop=url], span[itemprop=telephone], span[itemprop=streetAddress], span[itemprop=addressLocality], span[itemprop=addressRegion], span[itemprop=postalCode], span.longitude, span.latitude”);
elements.add(doc.select("div.info > div.rest_data").last());

for (Element e:elements) {
   if (e.hasAttr("itemprop”)) {
       System.out.println(e.attr("itemprop") + e.text());
    }
    if (e.hasAttr("itemprop”) && e.attr("itemprop").equals ("url")) {
        System.out.println("name: " + e.attr("title"));
    }

    if (e.attr("class").equals("longitude") || e.attr("class").equals("latitude")) {
        System.out. println(e.attr("class") + e.attr("title"));
    }

    if (e.attr("class").equals("rest_data")) {
        System.out.println(e.text());
    }
}

(注意:我在 phone 上写了这个,所以未经测试,但它应该可以工作,也可能包含拼写错误)

稍微解释一下:首先通过doc.select(...)获取所有需要的元素,然后从每个元素中提取需要的数据。

如果有效请告诉我。

可能要实现的主要事情是可以直接选择具有 id 的元素 - 无需遍历元素集合来搜索它。

我没有使用过 JSoup,我的 Java 已经很生锈了,但是现在...

// 1. Select elements from document
Element container = doc.select("#restaurant_info_box_left"); // find element in document with id="restaurant_info_box_left"
Element h1 = container.select("h1"); // find h1 element in container
Elements restData = container.select(".rest_data"); //find all divs in container with class="rest_data"
Element restData_0 = restData.get(0); // find first rest_data div
Element restData_1 = restData.get(1); // find second rest_data div
Elements restData_0_spans = restData_0.select("span"); // find first rest_data div's spans
Elements geos = container.select(".geo"); // find all divs in container with class="geo"
Element geo = geos.get(0); // find first .geo div
Elements geo_spans = geo.select("span"); // find first .geo div's spans

// 2. Compose output

// h1 text
String text = "Name: " + h1.text();
// output text >>>

// restData_0_spans text
for (Element span : restData_0_spans) {
    String text = span.attr("itemprop").toString() + ": " + span.text();
    // output text >>>
}

// geo data
for (Element span : geo_spans) {
    String text = span.attr("class").toString() + ": " + span.attr("title").toString();
    // output text >>>
}

// restData_1 text
String text = restData_1.text();
// output text >>>

对于习惯JavaScript/jQuery的人来说,这一切都显得很吃力。如果运气好,它可能会有所简化。