当前位置:  开发笔记 > 编程语言 > 正文

是否可以从WebView获取HTML代码

如何解决《是否可以从WebView获取HTML代码》经验,为你挑选了2个好方法。

我想抢先获取要加载的网页的HTML代码,webView使用正则表达式解析它,并只显示我想要的HTML代码,同时让网页仍然认为它已经加载了所有内容.

有没有办法在WebViewClient.onLoadResource()相似的方法中做到这一点?

编辑:我试过这个:

class MyJavaScriptInterface  
 {  
      @SuppressWarnings("unused")  
         public void showHTML(String html, Context context)  
         {  
            new AlertDialog.Builder(context)  
                 .setTitle("HTML")  
                 .setMessage(html)  
                 .setPositiveButton(android.R.string.ok, null)  
             .setCancelable(false)  
             .create();  
               pageHTML = html;
         }  
 }

@Override
    public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
        mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
        webview.getSettings().setJavaScriptEnabled(true);
        MyJavaScriptInterface interfaceA = new MyJavaScriptInterface();
        webview.addJavascriptInterface(interfaceA, "HTMLOUT");  
        WebViewClient anchorWebViewClient = new WebViewClient()
        {
            @Override  
            public void onPageFinished(WebView view, String url)  
            {  
                /* This call inject JavaScript into the page which just finished loading. */  
                webview.loadUrl("javascript:window.HTMLOUT.showHTML(''+document.getElementsByTagName('html')[0].innerHTML+'');");
                Pattern pattern = Pattern.compile("

Winning Sc.+

(.+)
", Pattern.DOTALL); Matcher matcher = pattern.matcher(pageHTML); matcher.find();

永远不会调用该接口



1> Aymon Fourni..:

不得不使用HttpClient.不需要cookie,只需要解析html:

private String getDownloadButtonOnly(String url){
    HttpGet pageGet = new HttpGet(url);

    ResponseHandler handler = new ResponseHandler() {
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            HttpEntity entity = response.getEntity();
            String html; 

            if (entity != null) {
                html = EntityUtils.toString(entity);
                return html;
            } else {
                return null;
            }
        }
    };

    pageHTML = null;
    try {
        while (pageHTML==null){
            pageHTML = client.execute(pageGet, handler);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        Pattern pattern = Pattern.compile("

Direct Down.+?

(
)*(.+?)<.+?>", Pattern.DOTALL); Matcher matcher = pattern.matcher(pageHTML); String displayHTML = null; while(matcher.find()){ displayHTML = matcher.group(); } return displayHTML; } @Override public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) { mRom.setFileSize(getFileSize(mRom.getURLSuffix())); webview.getSettings().setJavaScriptEnabled(true); WebViewClient anchorWebViewClient = new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); String downloadButtonHTML = getDownloadButtonOnly(url); if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){ lastLoadedURL = url; webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url); } }



2> Pentium10..:

这是一个从WebView中提取HTML的教程,不要忘记阅读本教程末尾的警告.

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有