空指针Exception JSOUP解析

时间:2017-11-03 15:59:05

标签: java jsoup

我试图打印user-news col-section list div中的所有div,但它总是返回NULL。我已从页面中删除了html部分,但如果您愿意,我可以提供完整的代码。

我已从页面中删除了html部分,但如果您愿意,我可以提供完整的代码。

感谢您的帮助。

   Element itemlist = doc.select("div.user-news col-section list").first();
        Elements items = itemlist.getAllElements();

        for(Element e : items){
            System.out.println(e.text());

        }



<html>
 <head>
 <title> note </title>
  <meta charset="utf-8">
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
  <meta content="YzU6jJ3yJ0My_t6b2CmL_z-yjo9gN2QUO2MmQHjAgM0" name="google-site-verification">
  <meta content="09278921fdde" name="bitly-verification">
  <meta content="width=device-width, initial-scale=1" name="viewport">
 
  <meta content="authenticity_token" name="csrf-param"> 
  <meta content="EdoDpiYEuVH0NtErL3ok2BUJDFGXoRQDGdmTu8aIYTI=" name="csrf-token">
  
 </head>
 <body class="one-col small v3">
  <div class="body-con">
   <div class="v3-header" style="font-family: &quot;Roboto&quot;,&quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif">
    <header>
     
   

       <ul class="account-info ">
        <li class="dressing-rooms"><a class="user-action" data-pa-attr-location="header" data-pa-name="dressing_room_list" href="/dressing_rooms"><i class="icon double-hanger-gray"></i><span class="subtitle">Dressing Rooms</span></a></li>
        <li class="likes-con"><a class="user-action" data-pa-attr-location="header" data-pa-name="my_likes" href="/likes"><i class="icon large-heart"></i><span class="subtitle">My Likes</span></a></li>
        <li class="news"><a class="user-action active" data-pa-attr-location="header" data-pa-name="news" href="/news"><i class="icon notification"></i><span class="notification-count"></span><span class="subtitle">News</span></a></li>
        <li class="account d">
         <div class="dropdown">
          <div aria-expanded="false" aria-haspopup="true" class="dropdown-toggle" data-toggle="dropdown">
           <i class="user-pic-bck profile"></i>
          </div>
          <div class="dropdown-menu dropdown-menu-right caret">
           <div class="dropdown-item">
            <a href="/closet/tashabui"><span>My Closet</span></a>
           </div>
           <div class="dropdown-item">
            <a href="/likes"><span>My Likes</span></a>
           </div>
           <div class="dropdown-item">
            <a data-ajax-modal="true" data-dismiss="modal" data-toggle="ajax-modal" href="/user/my-size" target="#edit-my-size"><span>My Size</span></a>
           </div>
           <div class="dropdown-item">
            <a data-pa-attr-location="header" data-pa-name="dressing_room_list" href="/dressing_rooms"><span>My Dressing Rooms</span></a>
           </div>
       
           <div class="dropdown-item">
            <a href="/logout"><span>Logout</span></a>
           </div>
          </div>
         </div></li>
       </ul>
      </div>
     </nav>
     <nav class="scrollable ">
      <div class="nav-content">
       <ul>
        <li class="link"><a data-pa-attr-location="nav" data-pa-name="feed" href="/feed">Feed</a></li>
        <li>
         <div aria-expanded="false" aria-haspopup="true" class="dropdown title">
          <a href="/category/Women"><span>Women's Clothing</span></a>
 
           <div class="section col-x6 col-l4">
            <div class="section-header">
             Trending Styles
            </div>
  
         
   <div class="modal hide" data-keyboard="false" id="signup-popup-connect-con"></div>
   <div class="flash-message-con v3">
    <span class="flash-message"></span>
   </div>
   <div class="main-con v3">
    <div class="user-news col-section list">
     <h3 class="title">News</h3>
     <div class="news-tabs news-tabs-v1-offers">
      <span><a class="tab btn rounded grey active" href="/news" track_action="news_all_tab_click" track_category="user" track_label="55e88b96c2523186d101b68b">All</a></span>
      <span><a class="tab btn rounded grey" href="/news/filtered" track_action="news_comments_tab_click" track_category="user" track_label="55e88b96c2523186d101b68b">Comments</a></span>
      <span><a class="tab btn rounded grey" href="/news/offers" track_action="news_offers_tab_click" track_category="user" track_label="55e88b96c2523186d101b68b">Offers</a></span>
     </div>
     <div class="notification dev-action-item unread">
      <a class="user_development_notification" data-action-type="pK49_060_L44_i19" data-command="{&quot;url&quot;:&quot;https://poshmark.com/getapp&quot;,&quot;type&quot;:&quot;new_tab&quot;}" data-pa-banner_name="pK49_060_L44_i19" data-pa-content_type="udn" data-pa-udn_type="edu" data-user-id="55e88b96c2523186d101b68b" href="https://poshmark.com/getapp" pac="udn" target="_blank">
       <div class="dev-action-gradient purple">
        <i class="sprite info"></i>
       </div>Want to sell your stuff? Download Poshmark on your mobile device!
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div></a>
     </div>
     <div actor_id="51f06d19911d8705f60000ac" class="notification read" data-uname="hsshall" notification_id="6271" user_id="51f06d19911d8705f60000ac">
      <a class="action follow-action" data-fb-if-href="/mapp/find_friends/follow_back?username=hsshall&amp;variation_id=fb_fb_v01" data-fi-if-href="/mapp/find_friends/follow_back?username=hsshall&amp;variation_id=if_fb_v01" data-pa-content_type="follow" href="/closet/hsshall" pac="news_item"><img alt="T_58b464edb0c5fca22ca6bea2" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/02/27/51f06d19911d8705f60000ac/t_58b464edb0c5fca22ca6bea2.jpg">
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-follow icon"></i>
        <span class="user-name">hsshall</span>
        <span>&nbsp;is following you</span>
        <div class="when">
         39 minutes ago
        </div>
       </div></a>
     </div>
     <div actor_id="5571c9785a7f971337031670" banner_name="share_back" class="notification read" data-uname="edwards13" dest_user_id="5571c9785a7f971337031670" dest_user_name="edwards13" left_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/icon-share-back-white-91b22b2211615533df9bb6a84a841e2b.png" notification_id="6270" post_id="59f0e6a6291a35194301f619" redirect_protocol="poshmark" right_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/right-arrow-white-7a7520da1a87341fc00d2e14cd1ffe98.png" show_listing_banner="show_listing_banner">
      <a class="action show-post-details" data-pa-content_type="share" href="/listing/59f0e6a6291a35194301f619?sharer_id=5571c9785a7f971337031670&amp;sharer_username=edwards13" pac="news_item"><img alt="T_59e78f9b8a3f3e7551a24879" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/10/18/5571c9785a7f971337031670/t_59e78f9b8a3f3e7551a24879.jpg">
       <div class="post-details">
        <img alt="M_59f0e724bcd4a762b501fa8b" class="post-pic" src="https://dtpmhvbsmffsz.cloudfront.net/posts/2017/10/25/59f0e6a6291a35194301f619/m_59f0e724bcd4a762b501fa8b.jpg">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-shared icon"></i>
        <span class="user-name">edwards13</span>
        <span>&nbsp;shared your listing</span>
        <div class="when">
         58 minutes ago
        </div>
       </div></a>
     </div>
     <div actor_id="575ca2188f005385af0471ae" class="notification read" data-uname="dames_doll" notification_id="6269" user_id="575ca2188f005385af0471ae">
      <a class="action follow-action" data-fb-if-href="/mapp/find_friends/follow_back?username=dames_doll&amp;variation_id=fb_fb_v01" data-fi-if-href="/mapp/find_friends/follow_back?username=dames_doll&amp;variation_id=if_fb_v01" data-pa-content_type="follow" href="/closet/dames_doll" pac="news_item"><img alt="T_59f7d8d41bd3af36a467f6ee" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/10/30/575ca2188f005385af0471ae/t_59f7d8d41bd3af36a467f6ee.jpg">
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-follow icon"></i>
        <span class="user-name">dames_doll</span>
        <span>&nbsp;is following you</span>
        <div class="when">
         an hour ago
        </div>
       </div></a>
     </div>
     <div actor_id="532a42d814b1e0666c021bd2" class="notification read" data-uname="beyondblessed11" notification_id="6268" user_id="532a42d814b1e0666c021bd2">
      <a class="action follow-action" data-fb-if-href="/mapp/find_friends/follow_back?username=beyondblessed11&amp;variation_id=fb_fb_v01" data-fi-if-href="/mapp/find_friends/follow_back?username=beyondblessed11&amp;variation_id=if_fb_v01" data-pa-content_type="follow" href="/closet/beyondblessed11" pac="news_item"><img alt="T_532a42e214b1e0666c021be3" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2014/03/19/532a42d814b1e0666c021bd2/t_532a42e214b1e0666c021be3.jpg">
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-follow icon"></i>
        <span class="user-name">beyondblessed11</span>
        <span>&nbsp;is following you</span>
        <div class="when">
         an hour ago
        </div>
       </div></a>
     </div>
     <div actor_id="4ff4b7956056d502fb03d0c9" banner_name="share_back" class="notification read" data-uname="masi911" dest_user_id="4ff4b7956056d502fb03d0c9" dest_user_name="masi911" left_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/icon-share-back-white-91b22b2211615533df9bb6a84a841e2b.png" notification_id="6267" post_id="59fc2176bf6df539100308da" redirect_protocol="poshmark" right_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/right-arrow-white-7a7520da1a87341fc00d2e14cd1ffe98.png" show_listing_banner="show_listing_banner">
      <a class="action show-post-details" data-pa-content_type="share" href="/listing/59fc2176bf6df539100308da?sharer_id=4ff4b7956056d502fb03d0c9&amp;sharer_username=masi911" pac="news_item"><img alt="T_59e3dc4d1e08ad3ac8364a6d" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/10/15/4ff4b7956056d502fb03d0c9/t_59e3dc4d1e08ad3ac8364a6d.jpg">
       <div class="post-details">
        <img alt="M_59fc2183680278edaa02fc9c" class="post-pic" src="https://dtpmhvbsmffsz.cloudfront.net/posts/2017/11/03/59fc2176bf6df539100308da/m_59fc2183680278edaa02fc9c.jpg">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-shared icon"></i>
        <span class="user-name">masi911</span>
        <span>&nbsp;shared your listing</span>
        <div class="when">
         an hour ago
        </div>
       </div></a>
     </div>
     <div actor_id="4ff4b7956056d502fb03d0c9" banner_name="share_back" class="notification read" data-uname="masi911" dest_user_id="4ff4b7956056d502fb03d0c9" dest_user_name="masi911" left_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/icon-share-back-white-91b22b2211615533df9bb6a84a841e2b.png" notification_id="6266" post_id="59fbd44bbcd4a79e2d02223b" redirect_protocol="poshmark" right_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/right-arrow-white-7a7520da1a87341fc00d2e14cd1ffe98.png" show_listing_banner="show_listing_banner">
      <a class="action show-post-details" data-pa-content_type="share" href="/listing/59fbd44bbcd4a79e2d02223b?sharer_id=4ff4b7956056d502fb03d0c9&amp;sharer_username=masi911" pac="news_item"><img alt="T_59e3dc4d1e08ad3ac8364a6d" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/10/15/4ff4b7956056d502fb03d0c9/t_59e3dc4d1e08ad3ac8364a6d.jpg">
       <div class="post-details">
        <img alt="M_59fbd49b5a49d0dc06022365" class="post-pic" src="https://dtpmhvbsmffsz.cloudfront.net/posts/2017/11/02/59fbd44bbcd4a79e2d02223b/m_59fbd49b5a49d0dc06022365.jpg">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-shared icon"></i>
        <span class="user-name">masi911</span>
        <span>&nbsp;shared your listing</span>
        <div class="when">
         an hour ago
        </div>
       </div></a>
     </div>
     <div actor_id="50c7b57782fe067bbf04afad" class="notification read" data-uname="cigi36" notification_id="6265" user_id="50c7b57782fe067bbf04afad">
      <a class="action follow-action" data-fb-if-href="/mapp/find_friends/follow_back?username=cigi36&amp;variation_id=fb_fb_v01" data-fi-if-href="/mapp/find_friends/follow_back?username=cigi36&amp;variation_id=if_fb_v01" data-pa-content_type="follow" href="/closet/cigi36" pac="news_item"><img alt="T_59ce69057ce76f3f2b0f5704" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/09/29/50c7b57782fe067bbf04afad/t_59ce69057ce76f3f2b0f5704.jpg">
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-follow icon"></i>
        <span class="user-name">cigi36</span>
        <span>&nbsp;is following you</span>
        <div class="when">
         an hour ago
        </div>
       </div></a>
     </div>
    
     <div actor_id="57951c9402e917f8d26eb523" class="notification read" data-uname="collectionbyk" notification_id="6253" user_id="57951c9402e917f8d26eb523">
      <a class="action follow-action" data-fb-if-href="/mapp/find_friends/follow_back?username=collectionbyk&amp;variation_id=fb_fb_v01" data-fi-if-href="/mapp/find_friends/follow_back?username=collectionbyk&amp;variation_id=if_fb_v01" data-pa-content_type="follow" href="/closet/collectionbyk" pac="news_item"><img alt="T_57951c9502e917f8d26eb52a" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2016/07/24/57951c9402e917f8d26eb523/t_57951c9502e917f8d26eb52a.jpg">
       <div class="notification-arrow">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-follow icon"></i>
        <span class="user-name">collectionbyk</span>
        <span>&nbsp;is following you</span>
        <div class="when">
         4 hours ago
        </div>
       </div></a>
     </div>
     <div actor_id="598fb04f9d78f83ba5008182" banner_name="share_back" class="notification read" data-uname="iheartcats97" dest_user_id="598fb04f9d78f83ba5008182" dest_user_name="iheartcats97" left_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/icon-share-back-white-91b22b2211615533df9bb6a84a841e2b.png" notification_id="6252" post_id="59fc14f19c6fcf6a8f02fdea" redirect_protocol="poshmark" right_image_url="https://d2zlsagv0ouax1.cloudfront.net/assets/v2/icon/right-arrow-white-7a7520da1a87341fc00d2e14cd1ffe98.png" show_listing_banner="show_listing_banner">
      <a class="action show-post-details" data-pa-content_type="share" href="/listing/59fc14f19c6fcf6a8f02fdea?sharer_id=598fb04f9d78f83ba5008182&amp;sharer_username=iheartcats97" pac="news_item"><img alt="T_59cc3094aba09e30bb0276df" class="profile" src="https://dtpmhvbsmffsz.cloudfront.net/users/2017/09/27/598fb04f9d78f83ba5008182/t_59cc3094aba09e30bb0276df.jpg">
       <div class="post-details">
        <img alt="M_59fc15022ba50ae5fd02feb4" class="post-pic" src="https://dtpmhvbsmffsz.cloudfront.net/posts/2017/11/03/59fc14f19c6fcf6a8f02fdea/m_59fc15022ba50ae5fd02feb4.jpg">
        <i class="sprite read-unread arrow"></i>
       </div>
       <div class="content">
        <i class="sprite news-shared icon"></i>
        <span class="user-name">iheartcats97</span>
        <span>&nbsp;shared your listing</span>
        <div class="when">
         5 hours ago
        </div>
       </div></a>
     </div>
    </div>
    <div id="load-more">
     <img alt="Loading" class="logo" src="//d2zlsagv0ouax1.cloudfront.net/assets/loading-84ea1d46fc13b7f69ce36f8f84e1e8ca.gif">
     <span>Loading more..</span>
    </div>
   </div>
   <div class="v3-header" style="font-family: &quot;Roboto&quot;,&quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif">
    <footer class="footer-bg">
     <div class="ftr-3-0">
      <div class="footer-content-links">
       <div class="col-l6 col-m8">
        <ul>
         <li><h4>Shop Categories</h4></li>
         <li><a href="/category/Women" data-pa-attr-location="footer" data-pa-name="category">Women</a></li>
         <li><a href="/category/Men" data-pa-attr-location="footer" data-pa-name="category">Men</a></li>
         <li><a href="/category/Kids" data-pa-attr-location="footer" data-pa-name="category">Kids</a></li>
         <li><a href="/category/Women-Handbags" data-pa-attr-location="footer" data-pa-name="category">Handbags</a></li>
         <li><a href="/category/Women-Shoes" data-pa-attr-location="footer" data-pa-name="category">Shoes</a></li>
         <li><a href="/category/Women-Jewelry" data-pa-attr-location="footer" data-pa-name="category">Jewelry &amp; Accessories</a></li>
         <li><a href="/category/Women-Makeup" data-pa-attr-location="footer" data-pa-name="category">Makeup</a></li>
         <li><a href="/category/Women-Dresses" data-pa-attr-location="footer" data-pa-name="category">Dresses</a></li>
        </ul>
       </div>
    
     
       <div class="col-l6 col-m24">
        <div class="col-l24 col-m15">
         <div class="connect-group">
          
  <div class="modal hide" data-backdrop="static" data-keyboard="false" id="page-modal-error">
   <div class="modal-header">
    <h5 class="popup-title">Error!</h5>
   </div>
   <div class="modal-body"></div>
   <div class="modal-footer">
    <input class="flat-btn blue primary" onclick="javascript:$(this).parent().parent().modal('hide');" value="OK" type="button">
   </div>
  </div>
  <div id="fb-root" class=" fb_reset">
   <div style="position: absolute; top: -10000px; height: 0px; width: 0px;">
    <div>

    </div>
   </div>
   <div style="position: absolute; top: -10000px; height: 0px; width: 0px;">
    <div>
  
    </div>
   </div>
  </div>
 </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您对此div感兴趣:

<div class="user-news col-section list">

该div使用三个类:

  • user-news
  • col-section
  • 列表

如果必须选择使用所有这三个类的第一个div元素,请执行以下操作:

Element itemlist = doc.select("div.user-news.col-section.list").first();
Elements items = itemlist.getAllElements();

for(Element e : items){
    System.out.println(e.text());

}

将班级名称分隔为.而不是space