+
80
-

回答

 Android 的话是这样的

1. 使能javascript:

webView.getSettings().setJavaScriptEnabled(true);
2. 编写本地接口
final class InJavaScriptLocalObj {
public void showSource(String html) {
Log.d("HTML", html);
}
}
3. 向网页暴露本地接口
webView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
4. 编写自己的WebViewClient,并在onPageFinished中提取网页源码。

final class MyWebViewClient extends WebViewClient{ 
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d("WebView","onPageStarted");
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
Log.d("WebView","onPageFinished ");
view.loadUrl("BfwJavascriptwindow.local_obj.showSource('<head>'+" +
"document.getElementsByTagName('html')[0].innerHTML+'</head>');");
super.onPageFinished(view, url);
}
}

Ios是这样的

#pragma WebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *JsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";
NSString *HTMLSource = [webView stringByEvaluatingJavaScriptFromString:JsToGetHTMLSource];
NSLog(@"%@",HTMLSource);
}

然后正则匹配,获取title及description标签


网友回复

我知道答案,我要回答