0

我正在编写一个简单的 Groovy 脚本来请求来自 Google 搜索的简单搜索,然后解析结果集。我知道有自定义搜索 API - 但这对我不起作用,所以请不要向我指出那个方向。

我正在使用 HTTPBuilder 发出请求。我发现所有其他方法“string”.toURL()、HTMLCleaner ......如果您使用它们进行调用,它们都会得到一个 http 403 代码。我假设这是因为请求标题对 Google 无效。

我可以让 HTTP Builder 发出并获得非 403 请求。也就是说,当我在“html”上执行 println(请参见下面的代码片段)时,它看起来不像 html 或 xml。它看起来就像文本。

这是获取响应的 HTTPBuilder 片段:

    //build query
    def query = ""
    queryTerms.eachWithIndex({term , i -> (i > 0) ? (query += "+" + term) : (query        += term)})

    def http = new HTTPBuilder(baseUrl)

    http.request(Method.GET,ContentType.TEXT) { req ->
        headers.'User-Agent' = 'Mozilla/5.0' }

    def html = http.get(path : searchPath, contentType : ContentType.HTML, query : [q:query])
    // println html
    assert html instanceof groovy.util.slurpersupport.GPathResult
    assert html.HEAD.size() == 1
    assert html.BODY.size() == 1

我得到了一些结果,所以我尝试按照下面的方式解析它。我将先提供实际结构,然后再进行解析。也就是说,在任何已解析的元素中都没有显示任何内容。

实际结构:

html->body#gsr->div#main->div->div#cnt->div#rcnt->div#center_col->div#res.med->div#search->div#ires->ol# rso->

代码:

    def mainDiv = html.body.div.findAll {it.@id.text() == 'main'}
    println mainDiv
    def rcntDiv = mainDiv.div.div.div.findAll { it.@id.text() == 'rcnt' }
    println rcntDiv
    def searchDiv = rcntDiv.div.findAll { it.@id.text == "center_col" }.div.div.findAll { it.@id.text == "search" }
    println searchDiv
    searchDiv.div.ol.li.each { println it }

那么这是不可能的吗?谷歌是在欺骗我并向我发送垃圾数​​据,还是我需要更多地调整我的 HTTPBuilder?有任何想法吗?

4

1 回答 1

0

你没有提到你使用的搜索 URL,所以我无法解释你为什么会得到 403。以下代码使用标准 Google 站点进行搜索,并且对我有用,没有任何 Forbidden 或其他状态错误:

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

import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.*

def http = new groovyx.net.http.HTTPBuilder('http://www.google.com')

def queryTerms =['queen','of','hearts']

http.request(GET,HTML) { req ->
    uri.path = '/search'
    uri.query= [q: queryTerms.join('+'), hl: 'en']

    headers.'User-Agent' = 'Mozilla/5.0'

  response.success = { resp, html ->
      println "Site title: ${html.HEAD.TITLE.text()}"
  }
  response.failure = { resp ->
    println resp.statusLine
  }
}

它输出站点标题,以表明它正在成功解析 HTML:

网站标题:queen+of+hearts - Google 搜索

于 2011-09-25T19:50:55.237 回答