java - 为什么我不能将我抓取的 HTML 解析为 XML?

标签 java coldfusion web-scraping html-parsing

我正在尝试使用 this function 将一些抓取的 HTML 解析为有效的 xml .

我的测试代码(带有从 Ben Nadel 的博客中复制粘贴的 htmlParse 函数):

<cfscript>
    // I take an HTML string and parse it into an XML(XHTML)
    // document. This is returned as a standard ColdFusion XML
    // document.
    function htmlParse( htmlContent, disableNamespaces = true ){

        // Create an instance of the Xalan SAX2DOM class as the
        // recipient of the TagSoup SAX (Simple API for XML) compliant
        // events. TagSoup will parse the HTML and announce events as
        // it encounters various HTML nodes. The SAX2DOM instance will
        // listen for such events and construct a DOM tree in response.
        var saxDomBuilder = createObject( "java", "com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM" ).init();

        // Create our TagSoup parser.
        var tagSoupParser = createObject( "java", "org.ccil.cowan.tagsoup.Parser" ).init();

        // Check to see if namespaces are going to be disabled in the
        // parser. If so, then they will not be added to elements.
        if (disableNamespaces){

        // Turn off namespaces - they are lame an nobody likes
        // to perform xmlSearch() methods with them in place.
        tagSoupParser.setFeature(
        tagSoupParser.namespacesFeature,
        javaCast( "boolean", false )
        );

        }

        // Set our DOM builder to be the listener for SAX-based
        // parsing events on our HTML.
        tagSoupParser.setContentHandler( saxDomBuilder );

        // Create our content input. The InputSource encapsulates the
        // means by which the content is read.
        var inputSource = createObject( "java", "org.xml.sax.InputSource" ).init(
        createObject( "java", "java.io.StringReader" ).init( htmlContent )
        );

        // Parse the HTML. This will trigger events which the SAX2DOM
        // builder will translate into a DOM tree.
        tagSoupParser.parse( inputSource );

        // Now that the HTML has been parsed, we have to get a
        // representation that is similar to the XML document that
        // ColdFusion users are used to having. Let's search for the
        // ROOT document and return is.
        return(
        xmlSearch( saxDomBuilder.getDom(), "/node()" )[ 1 ]
        );

    }
</cfscript>
<cfset html='<tr > <td align="center"> <span id="id1" >Compliance Review</span> </td><td class="center"> <span id="id2" >395.8(i)</span> </td><td align="left"> <span id="id3" >Failing to submit a record of duty status within 13 days </span> </td><td class="center" > <span id="id4">4/17/2014</span> </td> </tr>' />
<cfset parsedData = htmlParse(html) />

(html 是以这种格式从不同的函数接收的,但我现在尝试对字符串进行硬编码以跟踪问题。)

我收到以下错误:

NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist. 
The error occurred in myfilePath/myfileName.cfm: line 42

40 :        // Parse the HTML. This will trigger events which the SAX2DOM
41 :        // builder will translate into a DOM tree.
42 :        tagSoupParser.parse( inputSource );

出了什么问题?我该如何纠正它?

最佳答案

我没用过 TagSoup 但我一直在用 jTidy多年来,从各种来源(包括 MS Word)获取用户提供的 HTML 并对其进行清理以返回 XHTML,取得了很好的成果。

您可以通过将 jTidy jar 放到您的类路径或使用 JavaLoader 加载它来在同一文档上尝试 jTidy。由于您使用的是 CF10,因此可以使用 this method to include the JAR .

那么,在cfscript中调用jTidy的方法如下:

jTidy = createObject("java", "org.w3c.tidy.Tidy");

jTidy.setQuiet(false);
jTidy.setIndentContent(true);
jTidy.setSmartIndent(true);
jTidy.setIndentAttributes(true);
jTidy.setWraplen(1024);
jTidy.setXHTML(true);
jTidy.setNumEntities(true);
jTidy.setConvertWindowsChars(true);             
jTidy.setFixBackslash(true);        // changes \ in urls to /
jTidy.setLogicalEmphasis(true);     // uses strong/em instead of b/i
jTidy.setDropEmptyParas(true);

// create the in and out streams for jTidy
readBuffer = CreateObject("java","java.lang.String").init(parseData).getBytes();
inP = createobject("java","java.io.ByteArrayInputStream").init(readBuffer);
outx = createObject("java", "java.io.ByteArrayOutputStream").init();

// do the parsing
jTidy.parse(inP,outx);
outstr = outx.toString();

这将返回有效的 XHTML,您可以使用 XPath 对其进行查询。我将上面的代码包装到一个 makeValid() 函数中,然后针对您的 HTML 运行它:

    <cfset html='<tr > <td align="center"> <span id="id1" >Compliance Review</span> </td><td class="center"> <span id="id2" >395.8(i)</span> </td><td align="left"> <span id="id3" >Failing to submit a record of duty status within 13 days </span> </td><td class="center" > <span id="id4">4/17/2014</span> </td> </tr>' />
<cfset out = makeValid(html) />
<cfdump var="#xmlParse(out)#" />

这是输出:

Picture of the cfdump output from xmlParse()

关于java - 为什么我不能将我抓取的 HTML 解析为 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800162/

相关文章:

java - 如何获取另一个应用程序的图标?

java - 没有名为 'springSecurityFilterChain' 的可用 bean

regex - Coldfusion ReReplace 电话号码重新格式化

python - BeautifulSoup 抓取备用 div

java - 为什么多个 DTO 会更改 session 映射?

java - Java线程中变量的作用域

javascript - ColdFusion XML 到 Javascript 变量

amazon-s3 - Coldfusion CFUPLOAD 到 S3 权限问题 - 无法查看图像

ruby - 如何使用 Mechanize 解析本地文件

python - 尝试使用 BeautifulSoup 从我的代码中使用 Xpath 进行网页抓取