gpt4 book ai didi

java - 如何从 String 创建 HtmlUnit HTMLPage 对象?

转载 作者:太空狗 更新时间:2023-10-29 22:33:44 28 4
gpt4 key购买 nike

question was asked once already ,但我猜 API 发生了变化,答案不再有效。

URL url = new URL("http://www.example.com");
StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url);
HtmlPage page = HTMLParser.parseHtml(response, new TopLevelWindow("top", new WebClient()));
System.out.println(page.getTitleText());

无法完成,因为 TopLevelWindow 是 protected ,而扩展/实现窗口之类的东西因为那是荒谬的:)

有人知道如何做到这一点吗?在我看来,这不可能轻易完成,这很奇怪。

最佳答案

此代码在 GroovyConsole 中有效

@Grapes(
@Grab(group='net.sourceforge.htmlunit', module='htmlunit', version='2.8')
)

import com.gargoylesoftware.htmlunit.*
import com.gargoylesoftware.htmlunit.html.*

URL url = new URL("http://www.example.com");
StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url);
WebClient client = new WebClient()
HtmlPage page = HTMLParser.parseHtml(response, client.getCurrentWindow());
System.out.println(page.getTitleText());

关于java - 如何从 String 创建 HtmlUnit HTMLPage 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136435/

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