解析特定类之前的某个标记的数据

时间:2018-04-23 13:56:21

标签: java parsing jsoup

我需要通过标签(“p”)从网页解析数据。我试着这样:

Elements content = document.getElementsByTag("p");
 for(Element el : content) {
                System.out.println(el.text());

        }

它的工作正常。但我得到了多余的数据。 例如:

<div class="DicCellTerm">
   <h1>Impossible</h1>
   <div class=des>
      <p class=par2><span class=hint><em>smth</em></span></p>
      <p class=par2>1) (<em>with</em>) all, do</p>
      <p class=par2>2) <span class=hint><em>text</em></span> some words</p>
      <p class=par3>it is impossible</p>
   </div>
</div>
</div><!--DicCell end-->
<div align="center" class="AdContent" id="adcontentnoprint">
<div class=SharedItems>
   <div class=DicCellParent>
      <span class=LinkOtherDic>+ dictionary <strong>impossible</strong> - translate</span>
      <div class=DicCellOther id=diccellothershow>
         <h2><a href='/aljl-ktatlr/liotry-rssian-caar-csr-tdm-799.htm' title="impossible | impossible translate | dictionary">impossible</a></h2>
         <div class=des>
            <p class=par1>1) important, is</p>
            <p class=par1>what</p>
            <p class=par1>2) true, false</p>
         </div>
      </div>
      <!--DicCellOther end-->
   </div>
   <!--DicCellParent end-->
   <div class=DicCellParent>
      <span class=LinkOtherDic>+ translate <strong>important</strong> - dictionary</span>
      <div class=DicCellOther id=diccellothershow>
         <h2><a href='/all-cvdr/dicghnary-rhhan-chtar-ht-hm-800.htm' title="translate">importnant</a></h2>
         <div class=des>
            <p class=par1>1) müim, emiyetli; emiyet bar</p>
            <p class=par1>it is very important - bu pek müimdir, bunıñ büyük emiyeti bar</p>
            <p class=par1>2)  qopayıp, qabarıp</p>
         </div>
      </div>
      <!--DicCellOther end-->
   </div>
   <!--DicCellParent end-->
</div>
<!--SharedItems end-->

我需要在类SharedItems之前通过标记“p”获取数据。 我尝试通过类“DicCellTerm”解析数据并获得正确的数据。并且所有数据都写在一行中,但我需要在网页上获取数据。

1 个答案:

答案 0 :(得分:2)

Elements elements = document.select(".DicCellTerm p");

这会抓取.DicCellTerm类中的所有p,然后您可以迭代元素。
这是jsoup中所有可能选择器的链接,这是我得到大部分帮助的地方=)
https://jsoup.org/apidocs/index.html?org/jsoup/select/Selector.html