【问题标题】:Extract Data from HTML using JSoup使用 JSoup 从 HTML 中提取数据
【发布时间】:2016-01-17 08:19:08
【问题描述】:

我正在编写一个脚本来从 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 标签,我尝试匹配它们的唯一属性。 说div标签,class属性值为“信息”

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

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

如果匹配,我必须在 divtag 中获取带有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

有什么想法吗?

【问题讨论】:

    标签: java jquery html css jsoup


    【解决方案1】:
        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());
            // .....
        }
    

    【讨论】:

      【解决方案2】:

      我会这样做:

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

      (注意:这是我在手机上写的,未经测试,但应该可以,也可能包含拼写错误)

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

      让我知道它是否有效。

      【讨论】:

        【解决方案3】:

        可能要实现的主要事情是可以直接选择具有 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 的人来说,这一切似乎都很费力。运气好的话,它可能会简化一些。

        【讨论】:

          猜你喜欢
          • 2020-10-29
          • 1970-01-01
          • 1970-01-01
          • 2011-07-14
          • 2019-09-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多