scraping使用jsoup

4dbbbstv  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(271)

我正在用jsoup抓取一个电子商务网站。在这我想得到标签,如,和价格。在jsoup.parse()之后,我无法得到这个。

<div id="ctl00_ContentPlaceHolder1_ctl00_ctl03_Showcase">
 <div class="controlcontent_r">
  <div class="bucketgroup">
   <div class="prod_viewsparent">
    <div class="bucket" style="width: 175px; height: 280px;">
     <div class="bucket_left">
      <a href="/Products/Buy-Online-Electronics-Cameras-Digital-Cameras/Nikon/Nikon-Coolpix-L27-Point--Shoot/pid-2849731.aspx">
       <img class="mtb-img" style="width: 150px; height: 150px;" src="http://resources-images.martjackhosting.com/s3/martjack-resources/5d4b3aa1-119a-4d82-b9bb-1b6bdbd62002/Images/ProductImages/Source/NikonL27-BLK.jpg;width=150;height=150;scale=canvas" alt="Nikon Coolpix L27 Point & Shoot" title="Digital Cameras, Nikon, Nikon Coolpix L27 Point & Shoot"></a>
      <div id="2849731" class="btn_quick_view" style="display:none">
      <a rel="2849731,0,2466375,5d4b3aa1-119a-4d82-b9bb-1b6bdbd62002" href="#">Quick View</a></div>
   <h4 class="mtb-title">Nikon Coolpix L27 Point & Shoot</h4>
    <div class="mtb-desc">
      <span class="mtb-price">
        <label class="mtb-mrp">
        <b class="lb1"> MRP </b>
        <span class="WebRupee">Rs. </span>
          4,990
       </label>
        <label class="mtb-ofr">
        <b class="lb2"> Now At </b>
        <span class="WebRupee">Rs. </span>
          4,700
       </label>
        </span>
           <span class="offer_block">
          <a class="mtb-more" href="/Products/Buy-Online-Electronics-Cameras-Digital-Cameras/Nikon/Nikon-Coolpix-L27-Point--Shoot/pid-2849731.aspx" title="Click for more details"></div>

解析后,我看不到“div class=“bucket”标记。
我怎么处理?

hpcdzsge

hpcdzsge1#

请出示您的密码好吗?
顺便说一句,如果你想解析一个网站,最好使用 connect() 而不是 parse() .
下面是一个如何 <div class="controlcontent_r">...</div> 标签:

final String url = "http://www.jabraat.com/categories/Buy-Digital-Cameras-Online/cid-CU00084377.aspx";
Document doc = Jsoup.connect(url).get();

for( Element element : doc.select("div.controlcontent_r") )
{
    System.out.println(element);
    System.out.println();
}

此代码打印三个元素(用空行分隔):

<div class="controlcontent_r">
 <div class="mtc-menu">
  <ul class="mtc-cat">
   <li class="mtc-block"><a class="mtc-a mtc-selected" title="Go To Digital Cameras" href="http://www.jabraat.com/categories/Buy-Digital-Cameras-Online/cid-CU00084377.aspx">Digital Cameras</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go To Camcoders" href="http://www.jabraat.com/categories/Buy-Camcorders-Online/cid-CU00084380.aspx">Camcoders</a></li>
   <li class="mtc-block1"><a class="mtc-a" title="Go To Camera Accessories" href="http://www.jabraat.com/categories/Buy-Camera-Accessories-Online/cid-CU00084381.aspx">Camera Accessories</a></li>
  </ul>
 </div>
</div>

<div class="controlcontent_r">
 <div class="mtc-menu">
  <ul class="mtc-cat">
   <li class="mtc-block"><a class="mtc-a" title="Go To Camera" href="http://www.jabraat.com/categories/Buy-Cameras-Online/cid-CU00084376.aspx">Camera</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go To Digital Photo Frames" href="http://www.jabraat.com/categories/Buy-Digital-Photo-Frames-Online/cid-CU00084382.aspx">Digital Photo Frames</a></li>
   <li class="mtc-block1"><a class="mtc-a" title="Go To Mobiles" href="http://www.jabraat.com/categories/Buy-Mobiles-Online/cid-CU00084383.aspx">Mobiles</a></li>
  </ul>
 </div>
</div>

<div class="controlcontent_r"> 
 <div class="mtc-menu"> 
  <ul class="mtc-cat">
   <li class="mtc-block"><a class="mtc-a" title="Go to Watches" href="http://www.jabraat.com/categories/Buy-Watches-Online/cid-CU00084370.aspx">Watches</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Clothing" href="http://www.jabraat.com/categories/Buy-Online-Clothing/cid-CU00084420.aspx">Clothing</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Mobiles" href="http://www.jabraat.com/categories/Buy-Mobiles-Online/cid-CU00084383.aspx">Mobiles</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Cameras" href="http://www.jabraat.com/categories/Buy-Cameras-Online/cid-CU00084376.aspx">Cameras</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Home &amp; Kitchen" href="http://www.jabraat.com/categories/Buy-Home-Kitchen-Appliances-Online/cid-CU00084391.aspx">Home &amp; Kitchen</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Personal Care" href="http://www.jabraat.com/categories/Buy-Online-Personal-Care/cid-CU00084413.aspx">Personal Care</a></li>
   <li class="mtc-block"><a class="mtc-a" title="Go to Jewellery" href="http://www.jabraat.com/categories/Buy-Online-Jewellery/cid-CU00084429.aspx">Jewellery</a></li>
   <li class="mtc-block1"><a class="mtc-a" title="Go to Footwear" href="http://www.jabraat.com/categories/Buy-Online-Footwear/cid-CK00101771.aspx">Footwear</a></li>
  </ul> 
 </div> 
</div>

编辑:

正如评论中提到的,事情变得更加复杂 <div class='bucket'> 标签。而您可以轻松地解析 controlcontent_r 标签使用jsoup bucket 看起来是由脚本生成的。
你可以做一个简单的测试:

final String url = "http://www.jabraat.com/categories/Buy-Digital-Cameras-Online/cid-CU00084377.aspx";
Document doc = Jsoup.connect(url).get(); // Connect an parse the document (as above)

System.out.println(doc); // Output the document (= how jsoup "see"'s the website)

没有 bucket 标记在那里,意味着您无法检索它(使用jsoup)-解决方案是使用另一个库来执行脚本。
方便的是,我已经在这里发布了一个简短的列表:试图解析javascript隐藏的html

gajydyqb

gajydyqb2#

要与javascript交互,请使用selenium框架(googleit)。然后可以将元素解析为jsoup元素。 selenium 很容易。我很快就学会了。

相关问题