gpt4 book ai didi

html - 是否可以从 WebView 获取 HTML 代码

转载 作者:IT老高 更新时间:2023-10-28 22:14:56 24 4
gpt4 key购买 nike

我想抢先获取要在webView中加载的网页的HTML代码,使用正则表达式对其进行解析,并仅显示我想要的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('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
Pattern pattern = Pattern.compile("<h2>Winning Sc.+</h2></div>(.+)<br>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
matcher.find();

接口(interface)永远不会被调用

最佳答案

必须使用 HttpClient。不需要cookies,只需要解析html:

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

ResponseHandler<String> handler = new ResponseHandler<String>() {
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("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", 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);
}
}

关于html - 是否可以从 WebView 获取 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3479833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com