HTTPBuilder - 如何获取网页的 HTML 内容?
我需要提取网页的 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', headers. Accept = 'text/html',设置用户代理等),但结果是相同的。 如何使用 http 生成器获取 www.google.com/search(或任何网页)的 html?
I need to extract the HTML of a web page
I'm using HTTPuilder in groovy, making the following get:
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"
}
}
The response I get, does not contain the same html I can see when I explore the html source of www.google.com/search. In fact, it's neither an html, and does not contains the same info I can see in the html source of the page.
I've tried setting differents headers (for example, headers.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', headers.Accept = 'text/html', seting the user-agent, etc), but the result is the same.
How can I get the html of www.google.com/search (or any web page) using http builder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么使用httpBuilder?您可以改为使用
提取网页内容
Why use httpBuilder? You might instead use
to extract the content of the webpage
因为httpbuilder会根据内容类型自动解析结果。
要获取原始 html,请尝试从 Entity 获取文本
Because the httpbuilder will auto parse the result by the content type.
to get the raw html, try to get text from Entity