VBSCript从google.com页面检索链接

时间:2013-03-04 04:43:57

标签: internet-explorer object vbscript

我正在做的是:

  1. 加载www.google.com
  2. 在输入框中输入内容以搜索Google.com
  3. 然后在文本文件中检索谷歌提供的链接。
  4. 我尝试了下面的脚本,但它没有返回Google提供的“链接”:

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = TRUE
    ie.Navigate("https://www.google.co.in/#hl=en&output=search&sclient=psy-  ab&q=my+sa+fria&oq=my+sa+fria&gs_l=hp.3...136.425.0.1178.5.4.0.0.0.2.318.916.0j2j1j1.4.0.les%3B..0.0...1c.1.5.psy-ab.ILEhJPZdulQ&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&bvm=bv.43148975,d.bmk&fp=aceecfc5e1faf854&biw=1366&bih=487")
    Do until ie.ReadyState = 4
    WScript.Sleep 50
    Loop
    
    set urls = ie.document.all.tags("a")
     For x = 0 to (urls.length)-1
     wscript.echo urls(x).innerHTML & " : " & urls(x).href
    Next
    wscript.echo "Number of URLs:", urls.length, vbNewline
    

1 个答案:

答案 0 :(得分:1)

搜索结果位于<h3>标记中,因此您可以使用此信息将其与页面上的其他链接区分开来。试试这个:

searchUrl   = "http://www.google.co.in/search?q="
searchTerms = Array("my", "sa", "fria")

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate searchUrl & Join(searchTerms, "+")

Do Until ie.ReadyState = 4
  WScript.Sleep 50
Loop

Set results = ie.document.all.tags("h3")
For Each anchor In results.children.tags("a")
  WScript.Echo anchor.href
Next
WScript.Echo "Number of results:", results.Length, vbNewline