【发布时间】:2017-01-03 03:11:03
【问题描述】:
这是我的 HTML 的一部分
<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>
如您所见,HTML 中有 3 个 <p> 标记。但是我怎样才能在 jsoup 中定义只使用普通的<p> 标签,比如 hello world 和 goodbye world,而忽略带有 img 类的 <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();
}
}
【问题讨论】:
-
关于您对here 的编辑,请参阅this answer。确保导入正确的 BuildConfig。 (抱歉,这里对您的问题发表了不相关的评论。如果您查看我的回答的编辑历史记录,您会发现我曾经做过与您建议的编辑相同的事情。但是,使用 BuildConfig 更简洁。)
标签: android html parsing jsoup