将div加载到Android的webview中

时间:2022-09-04 08:09:56

I have an android app, wich contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage. I should mention that I do not have access to that page.

我有一个android应用,它包含一个WebView,我想在里面显示的不是一个网页,而是那个网页的一个div。我应该指出,我没有访问该页面的权限。

Thank you!

谢谢你!

2 个解决方案

#1


14  

I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:

我建议Jsoup。它比tagsoup更大,但提供了内置的功能,可以从URL中提取HTML,而且超级容易使用。例如,如果您想使用id示例提取div,您可以执行以下操作:

Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");

Then to load the HTML you've extracted into your web view you would do:

然后,要将提取的HTML加载到web视图中,您需要做的是:

String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);

#2


2  

You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.

您需要自己加载页面的HTML,提取div内容并将其作为字符串传递给WebView。您可能会发现一个HTML解析器库(例如tagsoup)对此很有用。

#1


14  

I would recommend Jsoup. It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use. For example if you want to pull a div with id example you would do the following:

我建议Jsoup。它比tagsoup更大,但提供了内置的功能,可以从URL中提取HTML,而且超级容易使用。例如,如果您想使用id示例提取div,您可以执行以下操作:

Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");

Then to load the HTML you've extracted into your web view you would do:

然后,要将提取的HTML加载到web视图中,您需要做的是:

String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);

#2


2  

You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string. You may find an HTML parser library (e.g. tagsoup) useful for this.

您需要自己加载页面的HTML,提取div内容并将其作为字符串传递给WebView。您可能会发现一个HTML解析器库(例如tagsoup)对此很有用。