2014-08-27 111 views
0

我尝试用JSOUP解析HTML页面,我的问题是我需要获取所有标题(只有10个)和图像(也有)。所以我必须把它们放在同一个地方(新闻馈送)。所以,我尝试用这个循环无法正确循环

@Override 
protected String doInBackground(String... arg) { 


    Document doc; 
    try { 

     doc = Jsoup.connect("http://example.com/news/").get(); 

     title = doc.select("h2[class=et_pt_title]"); 
     picture = doc.select("img"); 


      for (Element titles : title) { 
       FeedItem item = new FeedItem(); 
       item.setTitle(titles.text()); 
       Log.w("title", "" + item.getTitle()); 
       for (Element img : picture) { 

       // feedList.add(item); 

        String iurl; 
        iurl = img.absUrl("src"); 

       // FeedItem item = new FeedItem(); 
        if (iurl.contains("http://example.com/wp-content/uploads/2014/")) { 
         item.setAttachmentUrl(iurl); 
        } 
        Log.w("imgUrl", "" + item.getAttachmentUrl()); 

       } 

       feedList.add(item); 

      } 



    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

做,但标题是正确的位于和ListView只包含10行,但每行包含相同的画面。我该如何改变我的循环for?我需要每行的图片标题,其中不同的行包含不同的图片和标题。

我的HTML看起来像

<h2 class="et_pt_title"> 
    <a href="http://example.com/fotoshkola-yuurgu-otkryvaet-svoi-dveri/">&#171;Фотошкола &#187; открывает свои двери</a> 
</h2> 

<p class="et_pt_blogmeta"> 
    27.08.2014 &nbsp 
    <a href="http://example.com/category/abiturientam/" title="Просмотреть все записи в рубрике &laquo;Абитуриентам&raquo;" rel="category tag">Абитуриентам</a>, 
    <a href="http://example.com/category/aktual-noe/" title="Просмотреть все записи в рубрике &laquo;Актуальное&raquo;" rel="category tag">Актуальное</a>, 
    <a href="http://example.com/category/tvorchestvo/" title="Просмотреть все записи в рубрике &laquo;Творчество&raquo;" rel="category tag">Творчество</a>, 
    <a href="http://example.com/category/fotoshkola/" title="Просмотреть все записи в рубрике &laquo;Фотошкола&raquo;" rel="category tag">Фотошкола</a>, 
    <a href="http://example.com/category/hochu-v-gu/" title="Просмотреть все записи в рубрике&raquo;" rel="category tag">ba</a> 
</p> 

<div class="et_pt_thumb2 alignleft"> 
    <img src="http://example.com/wp-content/uploads/2014/08/3kSAbKRKwHM-282878_120x120.jpg" alt='&#171;Фотошкола &#187; открывает свои двери' width='120px' height='120px' /> 
    <a href="http://example.com/fotoshkola-otkryvaet-svoi-dveri/"></a> 
</div> <!-- end .thumb --> 

回答

1

关于图像,有什么你基本上做的是:

  1. 循环中的所有冠军(title),并在每次迭代:
  2. 循环中的所有图像( pictures
  3. 选择符合条件的图像(img)(if(iUrl.contains...

如果一个或多个图像满足条件(3),然后最后迭代图像,满足条件得到它的URL设置为item#attachmentUrl(从二传手名假定属性名)。

你可能想要的是挑最近的图像您h2title -loop后:

@Override 
protected String doInBackground(String... arg) { 


    Document doc; 
    try { 

     doc = Jsoup.connect("http://example.com/news/").get(); 

     title = doc.select("h2[class=et_pt_title]"); 

     for (Element titles : title) { 
      FeedItem item = new FeedItem(); 
      item.setTitle(titles.text()); 

      Log.w("title", "" + item.getTitle()); 

      // From Jsoup doc: 
      // siblingA ~ siblingX: finds sibling X element preceded by sibling A, e.g. h1 ~ p 
      // :eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1) 
      Elements imgContainingDiv = doc.select("h2[class=et_pt_title] ~ div[class=et_pt_thumb2]:eq(0)") // THIS MIGHT NEED TO BE :eq(1)! 
      Elements picture = imgContainingDiv.select("img") 

      for (Element img : picture) { 

      // feedList.add(item); 

       String iurl; 
       iurl = img.absUrl("src"); 

      // FeedItem item = new FeedItem(); 
       if (iurl.contains("http://example.com/wp-content/uploads/2014/")) { 
        item.setAttachmentUrl(iurl); 
       } 
       Log.w("imgUrl", "" + item.getAttachmentUrl()); 

      } 

      feedList.add(item); 

     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 
+0

遗憾的是它不为我工作:(我试图改变:EQ(0) (0)“或”h2.et_pt_title〜(eq(1)“)并且甚至擦除它 – vendettacore 2014-08-27 11:59:50

+0

你使用的是什么版本的Jsoup?如果是最近的版本,你可以试试: ”h2.et_pt_title_div.et_pt_thumb2:eq(0) div.et_pt_thumb2.alignleft:eq(0)“。 – heikkim 2014-08-27 12:08:13

+0

我使用jsoup-1.7.3。考虑到这个问题不在语法中,但我会尝试 – vendettacore 2014-08-27 12:13:44