将 HTML 源代码从变量读取到 HTMLUNIT

发布于 2024-12-28 02:19:02 字数 367 浏览 2 评论 0 原文

我看到的所有 htmlunit 示例都使用 getPage 通过 URL 从网站检索 HTML。

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

我在字符串变量中保存了一个 HTML 片段,但找不到从变量中为其提供 HTML 的方法。

我尝试将 String 转换为 HtmlPage 但这不起作用。

HtmlPage page = (HtmlPage) htmlFragment

All of the examples I see for htmlunit retrieve the HTML from website via a URL using getPage.

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

I'm holding an HTML fragment in a string variable and can't find a way to give it the HTML from a variable.

I've tried casting the String to HtmlPage but that doesn't work.

HtmlPage page = (HtmlPage) htmlFragment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2025-01-04 02:19:02

也许您可以将 HTML 片段(可能包含在标准 html/head/body 标记中)写入临时文件,并将其 URL 用于您的 Web 客户端。例如(未经测试):

File tempFile = File.createTempFile("fragment", "html");
HtmlPage startPage = webClient.getPage(tempFile.toURI().toURL().toString());
tempFile.deleteOnExit();

或者,您可以构建您的拥有一个带有虚假 URL 和临时 HtmlPage “nofollow">WebResponseWebWindow 但这可能需要大量的黑客工作。

Perhaps you could write the HTML fragment (possibly wrapped in the standard html/head/body tags) into a temp file and use its URL for your web client. For example (untested):

File tempFile = File.createTempFile("fragment", "html");
HtmlPage startPage = webClient.getPage(tempFile.toURI().toURL().toString());
tempFile.deleteOnExit();

Alternatively, you might be able to construct your own HtmlPage with a phony URL and ad-hoc WebResponse and WebWindow but that might require a lot of hacking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文