使用Jsoup从Webview android的网页中删除DIV [英] Remove DIV from a webpage for webview android using Jsoup

查看:303
本文介绍了使用Jsoup从Webview android的网页中删除DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在webview android上部分查看网页,并从该网页中删除一些div元素.我有一个这样的网页

I want to partially view a webpage on webview android and remove some div element from the webpage. I have a webpage like this

<!DOCTYPE html>
<body>

<div id="a"><p>Remove aa</p></div>
<div id="b"><p>bb</p></div>

</body></html>

现在,我要从网页中删除ID为"a"的div.

Now I want to remove the div with id 'a' from the webpage.

我试图用Jsoup对其进行编码,但是我还不足以使其实现.请查看我的完整代码:

I tried to code it with Jsoup but I am not well enough to make it out. Please see my full code:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class CustomWebsite extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_website); 

         Document doc;

         String htmlcode = "";

         try {
             doc = Jsoup.connect("http://skyasim.info/ab.html").get();
             doc.head().getElementsByTag("DIV#a").remove();

              htmlcode = doc.html();


         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }

        webView = (WebView) findViewById(R.id.webView_test);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true); 
        webView.loadUrl("htmlcode");


        }

          public class myWebClient extends WebViewClient
            {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    // TODO Auto-generated method stub
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // TODO Auto-generated method stub

                    view.loadUrl(url);
                    return true;

                }
            }


}

推荐答案

您可以执行此操作而无需使用您知道的Jsoup.只需使用普通的旧javascript.以下代码将显示如何从HTML页面中删除元素并显示其余元素.

You can do this without using Jsoup you know. Just use plain old javascript. The following code will show how to remove an element from the HTML page and display the rest.

final WebView mWebView = (WebView) findViewById(R.id.mWebViewId);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
 @Override
public void onPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
    "document.getElementById('a')[0].style.display='none'; " +
    "})()");
}
});
mWebView.loadUrl(youUrl);

这篇关于使用Jsoup从Webview android的网页中删除DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆