Sub DemoRequest1()
Static B As Boolean
If B Then Exit Sub
Const URL = "https://www.google.co.in/search?q="
Dim oJS As Object, oReq As Object, C%, F&, L&, R&, SL$(), SP$(), T$, V
Set oJS = CreateObject("MSScriptControl.ScriptControl")
oJS.Language = "JScript"
oJS.AddCode "function decode(txt) {return decodeURIComponent(txt);}"
Set oReq = CreateObject("Msxml2.XMLHTTP")
V = Application.Caller
B = Not IsError(V)
If B Then
F = Sheet1.Shapes(V).Fill.ForeColor.RGB
With Sheet1.Shapes(V).TextFrame.Characters: T = .Text: .Text = "Downloading links …": End With
End If
With Sheet1.[A4].CurrentRegion
.Columns("C:G").Offset(1).ClearContents
R = .Rows(.Rows.Count).Row
End With
For R = 5 To R
If B Then Sheet1.Shapes(V).Fill.ForeColor.SchemeColor = 24 + (R And 1): DoEvents
With oReq
.Open "GET", URL & Sheet1.Cells(R, 2).Text, False
.setRequestHeader "DNT", "1"
On Error Resume Next
.send
On Error GoTo 0
If .Status <> 200 Then Beep: Debug.Print "Row #" & R; " :"; .Status; " " & .StatusText: GoTo Fin
SP = Split(.responseText, "<h3 class=""r"">")
End With
If UBound(SP) > 0 Then
C = 2
For L = 1 To UBound(SP)
SL = Split(SP(L), "<a href=""")
If UBound(SL) > 0 Then
SL = Split(Split(SL(1), """>")(0), "/url?url=")
If UBound(SL) > 0 Then SL(0) = Split(SL(1), "&rct=")(0)
C = C + 1
Sheet1.Hyperlinks.Add Sheet1.Cells(R, C), oJS.Run("decode", SL(0)), , , Split(SL(0), "/")(2)
If C = 7 Then Exit For
End If
Next
End If
Next
Fin:
Set oJS = Nothing: Set oReq = Nothing
If B Then
With Sheet1.Shapes(V): .TextFrame.Characters.Text = T: .Fill.ForeColor.RGB = F: End With
B = False
End If
End Sub
Sub DemoRequest2()
Static B As Boolean
If B Then Exit Sub
Const URL = "https://www.google.co.in/search?q="
Dim oJS As Object, oDoc As Object, oReq As Object, oElt As Object, C%, F&, R&, S$(), T$, V
Set oJS = CreateObject("MSScriptControl.ScriptControl")
oJS.Language = "JScript"
oJS.AddCode "function decode(txt) {return decodeURIComponent(txt);}"
Set oDoc = CreateObject("htmlfile")
Set oReq = CreateObject("Msxml2.XMLHTTP")
V = Application.Caller
B = Not IsError(V)
If B Then
F = Sheet1.Shapes(V).Fill.ForeColor.RGB
With Sheet1.Shapes(V).TextFrame.Characters: T = .Text: .Text = "Downloading links …": End With
End If
With Sheet1.[A4].CurrentRegion
.Columns("C:G").Offset(1).ClearContents
R = .Rows(.Rows.Count).Row
End With
For R = 5 To R
If B Then Sheet1.Shapes(V).Fill.ForeColor.SchemeColor = 24 + (R And 1): DoEvents
With oReq
.Open "GET", URL & Sheet1.Cells(R, 2).Text, False
.setRequestHeader "DNT", "1"
On Error Resume Next
.send
On Error GoTo 0
If .Status <> 200 Then Beep: Debug.Print "Row #" & R; " :"; .Status; " " & .StatusText: GoTo Fin
oDoc.body.innerHTML = .responseText
End With
C = 2
For Each oElt In oDoc.all.ires.getElementsByTagName("H3")
With oElt.getElementsByTagName("A")
If .Length Then
C = C + 1
S = Split(.Item(0).href, "/url?url=")
If UBound(S) > 0 Then S(0) = Split(S(1), "&rct=")(0)
Sheet1.Hyperlinks.Add Sheet1.Cells(R, C), oJS.Run("decode", S(0)), , , Split(S(0), "/")(2)
If C = 7 Then Exit For
End If
End With
Next
Next
Fin:
Set oJS = Nothing: Set oDoc = Nothing: Set oReq = Nothing: Set oElt = Nothing
If B Then
With Sheet1.Shapes(V): .TextFrame.Characters.Text = T: .Fill.ForeColor.RGB = F: End With
B = False
End If
End Sub
Sub GoogleSearch()
Dim http As New MSXML2.XMLHTTP60, html As New HTMLDocument
Dim posts As Object, Myval As Variant
Dim url As String
Myval = InputBox("Give me any input")
url = "https://www.google.co.in/search?q=" & Myval
With http
.Open "GET", url, False
.setRequestHeader "Content-Type", "text/xml"
.send
html.body.innerHTML = .responseText
End With
Worksheets.Add
ActiveSheet.Name = Myval
Range("A1").Select
Range("A1") = Myval
Set posts = html.getElementById("rso").getElementsByTagName("H3")
For i = 0 To 5
ActiveCell.Offset(0, 1) = posts(i).getElementsByTagName("a")(0).innerText
ActiveCell.Offset(0, 2) = posts(i).getElementsByTagName("a")(0).href
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Sub GoogleSearch()
Const link = "https://www.google.co.in"
Dim http As New MSXML2.XMLHTTP60, html As New HTMLDocument
Dim htm As New HTMLDocument
Dim topics As Object, topic As Object, item As Object
Dim posts As Object, pagination As Object, Myval As Variant
Dim url As String
Myval = InputBox("Give me any input")
url = "https://www.google.co.in/search?q=" & Myval
With http
.Open "GET", url, False
.setRequestHeader "Content-Type", "text/xml"
.send
html.body.innerHTML = .responseText
End With
ActiveSheet.Name = Myval
Range("A1").Select
On Error Resume Next
Set topics = html.getElementById("rso").getElementsByTagName("H3")
For Each topic In topics
ActiveCell = topic.getElementsByTagName("a")(0).innerText
ActiveCell.Offset(0, 1) = topic.getElementsByTagName("a")(0).href
ActiveCell.Offset(1, 0).Select
Next topic
Set pagination = html.getElementsByClassName("fl")
For v = 0 To pagination.Length - IIf(pagination.Length > 0, 1, 0)
If InStr(pagination(v).href, "about:") > 0 Then
zz = link & Split(pagination(v).href, "about:")(1)
End If
With http
.Open "GET", zz, False
.setRequestHeader "Content-Type", "text/xml"
.send
htm.body.innerHTML = .responseText
End With
Set posts = htm.getElementById("rso").getElementsByTagName("H3")
For Each post In posts
ActiveCell = post.getElementsByTagName("a")(0).innerText
ActiveCell.Offset(0, 1) = post.getElementsByTagName("a")(0).href
ActiveCell.Offset(1, 0).Select
Next post
Next v
End Sub