是否可以从WebView获取HTML代码

我想抢先获取要在webView加载的网页的HTML代码,使用正则expression式解析它,并只显示我想要的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();

永远不会调用该接口

Solutions Collecting From Web of "是否可以从WebView获取HTML代码"

不得不使用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); } }

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

尝试在public void showHTML(String html,Context context)之前添加@JavascriptInterface

如果您有机会影响从中接收页面的服务器部分,您可以要求在出现错误时重定向到特定页面。 在WebViewClient中,您可以检测到此重定向并将其用作错误信号。