2017-01-02 163 views
1

這裏是我的HTML我怎樣才能獲得特定的標籤與jsoup的Android

<p>hello world </p> 
<p><img class=\"aligncenter size-full wp-image-3197\" src=\"data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=\" data-lazy-src=\"http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02.jpg\" alt=\"harmony-02\" width=\"800\" height=\"450\" data-lazy-srcset=\"http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02.jpg 800w, http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02-300x169.jpg 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" /><noscript><img class=\"aligncenter size-full wp-image-3197\" src=\"http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02.jpg\" alt=\"harmony-02\" width=\"800\" height=\"450\" srcset=\"http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02.jpg 800w, http://memaraneha.ir/wp-content/uploads/2016/12/harmony-02-300x169.jpg 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" /></noscript></p 
<p>goodbye world</p> 

正如你看到裏面有3 HTML標籤<p>的某些部分。但是我怎樣才能在jsoup中定義像正常的<p>這樣的標籤,如hello world和goodbye world,並忽略那個帶有img class的<p>標籤?

這裏是到目前爲止我的代碼:

public class MainActivity extends AppCompatActivity { 

    public WebView webView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_page); 
     webView=(WebView)findViewById(R.id.webi); 


     new AsyncTask<Void, Void, String>() { 
      @Override 
      protected String doInBackground(Void... voids) { 
       String html = ""; 
       try { 
        Document document = Jsoup.connect("http://memaraneha.ir/%db%8c%da%a9%d9%be%d8%a7%d8%b1%da%86%da%af%db%8c-%d9%87%d9%85%d8%a7%d9%87%d9%86%da%af%db%8c-%d8%b7%d8%b1%d8%a7%d8%ad%db%8c-%d8%af%d8%a7%d8%ae%d9%84%db%8c/") 
          .timeout(20000).get(); 

        Elements elements=document.select("div.base-box:nth-child(2)").select("p"); 
        html = elements.toString(); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return html; 
      } 
      @Override 
      protected void onPostExecute(String html) { 

       String mime = "text/html"; 
       String encoding = "utf-8"; 

       webView.loadDataWithBaseURL(null,html, mime, encoding,null); 
      } 
     }.execute(); 

    } 

} 

回答

0

你可以用這樣的嘗試。

選擇沒有任何<img>標籤內

Document document = Jsoup.connect().get(); 
    Elements elements = new Elements(); 
    for (Element e : document.select("p")) { 
     if (e.select("img").isEmpty()) { 
      elements.add(e); 
     } 
    } 
+0

嘿,你可以看看這個:HTTP:/ /stackoverflow.com/questions/41449446/remove-blank-sapaces-in-webveiw-not-work – erfan

1

可以避開環路及使用所有<p>標籤以下內容:

Elements e = doc.select("p:not(:has(img))"); 
+0

嘿似乎你是親在這種情況下。你可以看看這個請:http://stackoverflow.com/questions/41449446/remove-blank-sapaces-in-webveiw-not-work – erfan

+0

Thx,但我沒有得到在這種情況下的線索。 – TDG