shahin
Active Member
I've written a script in vba to get different golf club names from a certain site. The site requires "__VIEWSTATE" and "__EVENTVALIDATION" parameter to be filled in and passed through "POST" request before providing any valid response. If the necessary fields are taken directly from chrome dev tools to decorate the POST parameter then it is very easy to get the response. However, the thing is the aforesaid two fields contain two monster string which I find heavily difficult to rearrange to fit to the VBE.
I've found a technique (probably useful) to arrange the POST request in such a way so that the two fields with giant string can be stored in two variables and passed accordingly to the request parameter. At this point, I've done everything (so far my knowledge goes) to get the response but somewhere along the way I've made mistakes which I can't dig out. If anybody gives a little twitch in my script to make it work, I'll be very grateful. Thanks in advance.
Btw, to get the data manually from web, all you need to do is press the "GO" button.
Post Script: This is the link "https://chandoo.org/forum/threads/t...-dealing-with-post-request.34357/#post-212130" to the thread in which the problem was taken care of using the monster string.
I've found a technique (probably useful) to arrange the POST request in such a way so that the two fields with giant string can be stored in two variables and passed accordingly to the request parameter. At this point, I've done everything (so far my knowledge goes) to get the response but somewhere along the way I've made mistakes which I can't dig out. If anybody gives a little twitch in my script to make it work, I'll be very grateful. Thanks in advance.
Code:
Sub ClubData()
Const url = "http://www.golf.co.nz/PlayGolf/ClubDirectory.aspx"
Dim http As New XMLHTTP60, html As New HTMLDocument, postdata As String
Dim posts As Object, post As Object
Dim items As Object, item As Object, elem As Object
With http
.Open "POST", url, False
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
.send
html.body.innerHTML = .responseText
End With
Set posts = html.getElementById("__VIEWSTATE")
Set post = html.getElementById("__EVENTVALIDATION")
postdata = "ctl00$scm=ctl00$MainContent$pnlFilters|ctl00$MainContent$btnSearch&__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=" & posts.Value & "&__VIEWSTATEGENERATOR=FD74C8C4&__EVENTVALIDATION=" & post.Value & "&ctl00$txtSearchField=SEARCH&ctl00$MyGolfLoginSummary$tbMyGolfLogin_Username=&ctl00$MyGolfLoginSummary$tbMyGolfLogin_Password=&ctl00$MainContent$cbRegion=0&ctl00$MainContent$cbHoleOpt=0&ctl00$MainContent$tbSearch=SEARCH&__ASYNCPOST=true&ctl00$MainContent$btnSearch=GO"
With http
.Open "POST", url, False
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
.send postdata
html.body.innerHTML = .responseText
End With
For Each elem In html.getElementsByTagName("h2")
x = x + 1: Cells(x, 1) = elem.innerText
Next elem
End Sub
Btw, to get the data manually from web, all you need to do is press the "GO" button.
Post Script: This is the link "https://chandoo.org/forum/threads/t...-dealing-with-post-request.34357/#post-212130" to the thread in which the problem was taken care of using the monster string.