问题描述
由于Javascript支持,我正在使用HtmlUnit尝试抓取网页. (我宁愿使用Jsoup,但不使用JS支持).
I'm using HtmlUnit to try and scrape a webpage because of it's Javascript support. (I'd rather use Jsoup, but no JS support).
该问题与基础NekoHtml解析器的功能有关: " http://cyberneko.org/html/features/scanner/allow-selfclosing- iframe "
The issue relates to a feature of the underlying NekoHtml parser: "http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe"
请参阅: http://nekohtml.sourceforge.net/settings.html
这显然可以在Neko中启用,但是我正在使用HtmlUnit.有没有办法配置HTML单元用来启用此功能的基础Neko解析器?
This can apparently be enabled in Neko, but I'm using HtmlUnit. Is there a way to configure the underlying Neko parser that HTML unit is using to enable this feature?
尝试运行此代码时:
final WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url.toString());
我收到此错误:
Caused by: com.gargoylesoftware.htmlunit.ObjectInstantiationException: unable to create HTML parser
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:418)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:342)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:203)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:179)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:221)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:106)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:433)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:311)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:358)
at
Caused by: org.xml.sax.SAXNotRecognizedException: Feature 'http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe' is not recognized.
at org.apache.xerces.parsers.AbstractSAXParser.setFeature(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:411)
... 41 more
推荐答案
已解决...
BrowserVersionFeatures[] bvf = new BrowserVersionFeatures[1];
bvf[0] = BrowserVersionFeatures.HTMLIFRAME_IGNORE_SELFCLOSING;
BrowserVersion bv = new BrowserVersion(
BrowserVersion.NETSCAPE, "5.0 (Windows; en-US)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8",
(float) 3.6, bvf);
WebClient webClient = new WebClient(bv);
webClient.setJavaScriptEnabled(true);
这篇关于使用HtmlUnit时,如何配置基础NekoHtml解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!