【问题标题】:Receiving partial results when parsing HTML with HttpBuilder使用 HttpBuilder 解析 HTML 时接收部分结果
【发布时间】:2015-08-24 18:40:58
【问题描述】:

当我使用HttpBuilder 解析 HTML 时,如下所示,我没有收到完整的 HTML,正如我在访问该页面并检查时看到的那样。例如,在生成的文件中看不到<img> 标签。

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON
import groovy.json.*

def http = new HTTPBuilder('http://www.google.com') 
def html = http.get(uri: 'http://www.imdb.com/title/tt2004420/', contentType: groovyx.net.http.ContentType.TEXT) { resp, reader ->

    def p = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(s) 
    new File("/Users/../Documents/temp.txt") << p              
}

我希望通过解析来获取该 html 页面上的图像数量。

【问题讨论】:

    标签: groovy httpbuilder


    【解决方案1】:

    这是因为当您解析文件并显示它时,只显示内容 - 没有标签。运行以下脚本后:

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
    
    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.Method.GET
    import static groovyx.net.http.ContentType.JSON
    import groovy.json.*
    
    def http = new HTTPBuilder('http://www.google.com') 
    def html = http.get(uri: 'http://www.imdb.com/title/tt2004420/', contentType: groovyx.net.http.ContentType.TEXT) { resp, reader ->
    
        def p = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(reader.text) 
        new File("lol") << p              
    }
    

    lol 文件包含例如以下行:

    IMDbMoreAllTitlesTV 剧集名称公司关键字人物语录传记情节电影,

    哪个(部分)在解析之前看起来:

        <div class="quicksearch_dropdown_wrapper">
          <select name="s" id="quicksearch" class="quicksearch_dropdown navbarSprite"
                  onchange="jumpMenu(this); suggestionsearch_dropdown_choice(this);">
            <option value="all" >All</option>
            <option value="tt" >Titles</option>
            <option value="ep" >TV Episodes</option>
            <option value="nm" >Names</option>
            <option value="co" >Companies</option>
            <option value="kw" >Keywords</option>
            <option value="ch" >Characters</option>
            <option value="qu" >Quotes</option>
            <option value="bi" >Bios</option>
            <option value="pl" >Plots</option>
          </select>
        </div>
    

    如果您想查看标签,请使用以下脚本:

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
    
    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.Method.GET
    import static groovyx.net.http.ContentType.JSON
    import groovy.json.*
    
    def http = new HTTPBuilder('http://www.google.com') 
    def html = http.get(uri: 'http://www.imdb.com/title/tt2004420/', contentType: groovyx.net.http.ContentType.TEXT) { resp, reader ->
    
        new File("lol") << reader.text
    }
    

    【讨论】:

    • 它没有解决问题。我得到一个html 文件。我正在使用这个@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) import groovyx.net.http.HTTPBuilder import static groovyx.net.http.Method.GET import static groovyx.net.http.ContentType.JSON import groovy.json.* def http = new HTTPBuilder('http://www.google.com') def html = http.get(uri: 'http://www.imdb.com/title/tt2004420/', contentType: groovyx.net.http.ContentType.TEXT) { resp, reader -&gt; new File("/Users/../Documents/file1") &lt;&lt; reader.text }
    • 实际上,当我使用不同的文本编辑器打开文件时,我可以看到完整的 html 代码。上面,我指的是一个带有一堆超链接的文件。所以我认为它确实有效。
    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 2011-11-10
    • 2014-02-23
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多