我需要提取网页的 HTML 我在 groovy 中使用 HTTPuilder,得到以下结果:
def http = new HTTPBuilder('http://www.google.com/search')
http.request(Method.GET) {
requestContentType = ContentType.HTML
response.success = { resp, reader ->
println "resp: " + resp
println "READER: " + reader
}
response.failure = { resp, reader ->
println "Failure"
}
}
我得到的响应不包含我在浏览 www.google.com/search 的 html 源时看到的相同 html。事实上,它既不是 html,也不包含我在页面的 html 源代码中看到的相同信息。 我试过设置不同的标题(例如,headers.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',标题。 accept = 'text/html',设置用户代理等),但结果是一样的。 如何使用 http 构建器获取 www.google.com/search(或任何网页)的 html?
最佳答案
为什么要使用httpBuilder?您可以改用
def url = "http://www.google.com/".toURL()
println url.text`
提取网页内容
关于html-content-extraction - HTTPBuilder - 如何获取网页的 HTML 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6816943/