I want to draw data from the web with Excel macro












-1















I want to take old programs from http://arsiv.sahadan.com/genis_ekran_iddaa_programi/. For this I modified the macro with the module called @QHarr, but I couldn't handle the tables. Macro doesn't work.



    Public Sub Deneme()
Application.ScreenUpdating = False
Sheets("X").Select
Cells.Delete Shift:=xlUp
Range("A1").Select
Dim url As String, ws As Worksheet, html As HTMLDocument, http As clsHTTP, hTable As HTMLTable
Dim headerRow As Boolean, trow As Object, tRows As Object, tCell As Object, tCells As Object
Dim iRow As Long, R As Long, C As Long, Hsay As Long, numberOfRequests As Long
Dim hafta(), results(), headers()
headers = Array("Hsay", "Saat", "Lig", "Kod", "MBS", "Ev Sahibi", "Misafir", "IY", "MS", "MS1", "MSX", "MS2", "IY1", "IYX", "IY2", "he", "H1", "HX", "H2", "hm", "KGV", "GVY", "CS1/X", "CS1/2", "X/2", "IY1,5A", "IY1,5U", "1,5A", "1,5U", "2,5A", "2,5U", "3,5A", "3,5U", "TG01", "TG23", "TG46", "7+")
Set http = New clsHTTP
Set ws = ThisWorkbook.Worksheets("X")
Set html = New HTMLDocument
hafta = Application.Transpose(Sheets("Y").Range("A1:A" & Sheets("Y").Range("A1048576").End(xlUp).Row).Value)
Const numTableRows As Long = 500
Const numTableColumns As Long = 37
Const BASE_URL As String = "http://arsiv.sahadan.com/LargeProgram.aspx?"
numberOfRequests = UBound(hafta)
ReDim results(1 To numTableRows * numberOfRequests, 1 To numTableColumns)
For Hsay = 1 To numberOfRequests
headerRow = True
url = BASE_URL & "id=weekId&value=" & hafta(Hsay)
html.body.innerHTML = http.GetString(url)
Set hTable = html.querySelector("dvLargeHead")
Set tRows = hTable.getElementsByTagName("tr")
For Each trow In tRows
If Not headerRow Then
C = 2: R = R + 1
results(R, 1) = hafta(Hsay)
Set tCells = trow.getElementsByTagName("td")
For Each tCell In tCells
results(R, C) = tCell.innerText
C = C + 1
Next
End If
headerRow = False
Next
Next
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub









share|improve this question




















  • 3





    Can you be more specific about what "doesn't work"? Error message, for example?

    – BigBen
    Nov 21 '18 at 17:57













  • gives error 91 in line.

    – Cumhur Ay
    Nov 21 '18 at 17:59











  • Set tRows = hTable.getElementsByTagName("tr")

    – Cumhur Ay
    Nov 21 '18 at 17:59






  • 1





    Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

    – QHarr
    Nov 22 '18 at 6:50
















-1















I want to take old programs from http://arsiv.sahadan.com/genis_ekran_iddaa_programi/. For this I modified the macro with the module called @QHarr, but I couldn't handle the tables. Macro doesn't work.



    Public Sub Deneme()
Application.ScreenUpdating = False
Sheets("X").Select
Cells.Delete Shift:=xlUp
Range("A1").Select
Dim url As String, ws As Worksheet, html As HTMLDocument, http As clsHTTP, hTable As HTMLTable
Dim headerRow As Boolean, trow As Object, tRows As Object, tCell As Object, tCells As Object
Dim iRow As Long, R As Long, C As Long, Hsay As Long, numberOfRequests As Long
Dim hafta(), results(), headers()
headers = Array("Hsay", "Saat", "Lig", "Kod", "MBS", "Ev Sahibi", "Misafir", "IY", "MS", "MS1", "MSX", "MS2", "IY1", "IYX", "IY2", "he", "H1", "HX", "H2", "hm", "KGV", "GVY", "CS1/X", "CS1/2", "X/2", "IY1,5A", "IY1,5U", "1,5A", "1,5U", "2,5A", "2,5U", "3,5A", "3,5U", "TG01", "TG23", "TG46", "7+")
Set http = New clsHTTP
Set ws = ThisWorkbook.Worksheets("X")
Set html = New HTMLDocument
hafta = Application.Transpose(Sheets("Y").Range("A1:A" & Sheets("Y").Range("A1048576").End(xlUp).Row).Value)
Const numTableRows As Long = 500
Const numTableColumns As Long = 37
Const BASE_URL As String = "http://arsiv.sahadan.com/LargeProgram.aspx?"
numberOfRequests = UBound(hafta)
ReDim results(1 To numTableRows * numberOfRequests, 1 To numTableColumns)
For Hsay = 1 To numberOfRequests
headerRow = True
url = BASE_URL & "id=weekId&value=" & hafta(Hsay)
html.body.innerHTML = http.GetString(url)
Set hTable = html.querySelector("dvLargeHead")
Set tRows = hTable.getElementsByTagName("tr")
For Each trow In tRows
If Not headerRow Then
C = 2: R = R + 1
results(R, 1) = hafta(Hsay)
Set tCells = trow.getElementsByTagName("td")
For Each tCell In tCells
results(R, C) = tCell.innerText
C = C + 1
Next
End If
headerRow = False
Next
Next
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub









share|improve this question




















  • 3





    Can you be more specific about what "doesn't work"? Error message, for example?

    – BigBen
    Nov 21 '18 at 17:57













  • gives error 91 in line.

    – Cumhur Ay
    Nov 21 '18 at 17:59











  • Set tRows = hTable.getElementsByTagName("tr")

    – Cumhur Ay
    Nov 21 '18 at 17:59






  • 1





    Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

    – QHarr
    Nov 22 '18 at 6:50














-1












-1








-1


0






I want to take old programs from http://arsiv.sahadan.com/genis_ekran_iddaa_programi/. For this I modified the macro with the module called @QHarr, but I couldn't handle the tables. Macro doesn't work.



    Public Sub Deneme()
Application.ScreenUpdating = False
Sheets("X").Select
Cells.Delete Shift:=xlUp
Range("A1").Select
Dim url As String, ws As Worksheet, html As HTMLDocument, http As clsHTTP, hTable As HTMLTable
Dim headerRow As Boolean, trow As Object, tRows As Object, tCell As Object, tCells As Object
Dim iRow As Long, R As Long, C As Long, Hsay As Long, numberOfRequests As Long
Dim hafta(), results(), headers()
headers = Array("Hsay", "Saat", "Lig", "Kod", "MBS", "Ev Sahibi", "Misafir", "IY", "MS", "MS1", "MSX", "MS2", "IY1", "IYX", "IY2", "he", "H1", "HX", "H2", "hm", "KGV", "GVY", "CS1/X", "CS1/2", "X/2", "IY1,5A", "IY1,5U", "1,5A", "1,5U", "2,5A", "2,5U", "3,5A", "3,5U", "TG01", "TG23", "TG46", "7+")
Set http = New clsHTTP
Set ws = ThisWorkbook.Worksheets("X")
Set html = New HTMLDocument
hafta = Application.Transpose(Sheets("Y").Range("A1:A" & Sheets("Y").Range("A1048576").End(xlUp).Row).Value)
Const numTableRows As Long = 500
Const numTableColumns As Long = 37
Const BASE_URL As String = "http://arsiv.sahadan.com/LargeProgram.aspx?"
numberOfRequests = UBound(hafta)
ReDim results(1 To numTableRows * numberOfRequests, 1 To numTableColumns)
For Hsay = 1 To numberOfRequests
headerRow = True
url = BASE_URL & "id=weekId&value=" & hafta(Hsay)
html.body.innerHTML = http.GetString(url)
Set hTable = html.querySelector("dvLargeHead")
Set tRows = hTable.getElementsByTagName("tr")
For Each trow In tRows
If Not headerRow Then
C = 2: R = R + 1
results(R, 1) = hafta(Hsay)
Set tCells = trow.getElementsByTagName("td")
For Each tCell In tCells
results(R, C) = tCell.innerText
C = C + 1
Next
End If
headerRow = False
Next
Next
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub









share|improve this question
















I want to take old programs from http://arsiv.sahadan.com/genis_ekran_iddaa_programi/. For this I modified the macro with the module called @QHarr, but I couldn't handle the tables. Macro doesn't work.



    Public Sub Deneme()
Application.ScreenUpdating = False
Sheets("X").Select
Cells.Delete Shift:=xlUp
Range("A1").Select
Dim url As String, ws As Worksheet, html As HTMLDocument, http As clsHTTP, hTable As HTMLTable
Dim headerRow As Boolean, trow As Object, tRows As Object, tCell As Object, tCells As Object
Dim iRow As Long, R As Long, C As Long, Hsay As Long, numberOfRequests As Long
Dim hafta(), results(), headers()
headers = Array("Hsay", "Saat", "Lig", "Kod", "MBS", "Ev Sahibi", "Misafir", "IY", "MS", "MS1", "MSX", "MS2", "IY1", "IYX", "IY2", "he", "H1", "HX", "H2", "hm", "KGV", "GVY", "CS1/X", "CS1/2", "X/2", "IY1,5A", "IY1,5U", "1,5A", "1,5U", "2,5A", "2,5U", "3,5A", "3,5U", "TG01", "TG23", "TG46", "7+")
Set http = New clsHTTP
Set ws = ThisWorkbook.Worksheets("X")
Set html = New HTMLDocument
hafta = Application.Transpose(Sheets("Y").Range("A1:A" & Sheets("Y").Range("A1048576").End(xlUp).Row).Value)
Const numTableRows As Long = 500
Const numTableColumns As Long = 37
Const BASE_URL As String = "http://arsiv.sahadan.com/LargeProgram.aspx?"
numberOfRequests = UBound(hafta)
ReDim results(1 To numTableRows * numberOfRequests, 1 To numTableColumns)
For Hsay = 1 To numberOfRequests
headerRow = True
url = BASE_URL & "id=weekId&value=" & hafta(Hsay)
html.body.innerHTML = http.GetString(url)
Set hTable = html.querySelector("dvLargeHead")
Set tRows = hTable.getElementsByTagName("tr")
For Each trow In tRows
If Not headerRow Then
C = 2: R = R + 1
results(R, 1) = hafta(Hsay)
Set tCells = trow.getElementsByTagName("td")
For Each tCell In tCells
results(R, C) = tCell.innerText
C = C + 1
Next
End If
headerRow = False
Next
Next
With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub






html excel vba web-scraping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 14:09









QHarr

32.1k82042




32.1k82042










asked Nov 21 '18 at 17:56









Cumhur AyCumhur Ay

266




266








  • 3





    Can you be more specific about what "doesn't work"? Error message, for example?

    – BigBen
    Nov 21 '18 at 17:57













  • gives error 91 in line.

    – Cumhur Ay
    Nov 21 '18 at 17:59











  • Set tRows = hTable.getElementsByTagName("tr")

    – Cumhur Ay
    Nov 21 '18 at 17:59






  • 1





    Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

    – QHarr
    Nov 22 '18 at 6:50














  • 3





    Can you be more specific about what "doesn't work"? Error message, for example?

    – BigBen
    Nov 21 '18 at 17:57













  • gives error 91 in line.

    – Cumhur Ay
    Nov 21 '18 at 17:59











  • Set tRows = hTable.getElementsByTagName("tr")

    – Cumhur Ay
    Nov 21 '18 at 17:59






  • 1





    Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

    – QHarr
    Nov 22 '18 at 6:50








3




3





Can you be more specific about what "doesn't work"? Error message, for example?

– BigBen
Nov 21 '18 at 17:57







Can you be more specific about what "doesn't work"? Error message, for example?

– BigBen
Nov 21 '18 at 17:57















gives error 91 in line.

– Cumhur Ay
Nov 21 '18 at 17:59





gives error 91 in line.

– Cumhur Ay
Nov 21 '18 at 17:59













Set tRows = hTable.getElementsByTagName("tr")

– Cumhur Ay
Nov 21 '18 at 17:59





Set tRows = hTable.getElementsByTagName("tr")

– Cumhur Ay
Nov 21 '18 at 17:59




1




1





Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

– QHarr
Nov 22 '18 at 6:50





Can you give an example of one full URL created by url = BASE_URL & "id=weekId&value=" & hafta(Hsay) .... does it produce valid URLs?

– QHarr
Nov 22 '18 at 6:50












1 Answer
1






active

oldest

votes


















2














I cannot successfully reproduce the ajax requests that are used to update the page. I get Access Denied which makes me think there must be some protocol/authentication I am missing beyond the simple query string part.



Below is an example using selenium basic. It is slow as I am copying all the formatting across as the layout is a little finicky.



I have written something without using the clipboard which I may add later if I am happy with. It is a lot faster.



Option Explicit

Public Sub GetInfo()
Dim d As WebDriver, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, weeks As Object, i As Long
Const MAX_WAIT_SEC As Long = 15
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set d = New ChromeDriver
Const URL = "http://arsiv.sahadan.com/genis_ekran_iddaa_programi/"

With d
.Start "Chrome"
.get URL, timeout:=90000

Set weeks = .FindElementsByCss("#weekId option")
.FindElementByCss("[value='-1']").Click
For i = 1 To weeks.Count
If i > 1 Then
.FindElementsByCss("#weekId option")(i).Click
End If
Dim html As HTMLDocument
Set html = New HTMLDocument
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .FindElementByCss("#dvLarge #resultsList")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
clipboard.SetText ele.Attribute("outerHTML")
clipboard.PutInClipboard
ws.Cells.UnMerge
Application.Wait Now + TimeSerial(0, 0, 1)
ws.Cells(GetLastRow(ws, 1) + 1, 1).PasteSpecial
Application.Wait Now + TimeSerial(0, 0, 3)
End If

Set ele = Nothing
Next
.Quit
End With

End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function





share|improve this answer


























  • Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

    – Cumhur Ay
    Nov 22 '18 at 19:32






  • 1





    This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

    – QHarr
    Nov 22 '18 at 19:34








  • 1





    Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

    – QHarr
    Nov 22 '18 at 19:35






  • 1





    Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

    – QHarr
    Nov 26 '18 at 9:05






  • 2





    I did what you said and the code worked fine. Thanks.

    – Cumhur Ay
    Nov 26 '18 at 11:24













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417990%2fi-want-to-draw-data-from-the-web-with-excel-macro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I cannot successfully reproduce the ajax requests that are used to update the page. I get Access Denied which makes me think there must be some protocol/authentication I am missing beyond the simple query string part.



Below is an example using selenium basic. It is slow as I am copying all the formatting across as the layout is a little finicky.



I have written something without using the clipboard which I may add later if I am happy with. It is a lot faster.



Option Explicit

Public Sub GetInfo()
Dim d As WebDriver, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, weeks As Object, i As Long
Const MAX_WAIT_SEC As Long = 15
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set d = New ChromeDriver
Const URL = "http://arsiv.sahadan.com/genis_ekran_iddaa_programi/"

With d
.Start "Chrome"
.get URL, timeout:=90000

Set weeks = .FindElementsByCss("#weekId option")
.FindElementByCss("[value='-1']").Click
For i = 1 To weeks.Count
If i > 1 Then
.FindElementsByCss("#weekId option")(i).Click
End If
Dim html As HTMLDocument
Set html = New HTMLDocument
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .FindElementByCss("#dvLarge #resultsList")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
clipboard.SetText ele.Attribute("outerHTML")
clipboard.PutInClipboard
ws.Cells.UnMerge
Application.Wait Now + TimeSerial(0, 0, 1)
ws.Cells(GetLastRow(ws, 1) + 1, 1).PasteSpecial
Application.Wait Now + TimeSerial(0, 0, 3)
End If

Set ele = Nothing
Next
.Quit
End With

End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function





share|improve this answer


























  • Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

    – Cumhur Ay
    Nov 22 '18 at 19:32






  • 1





    This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

    – QHarr
    Nov 22 '18 at 19:34








  • 1





    Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

    – QHarr
    Nov 22 '18 at 19:35






  • 1





    Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

    – QHarr
    Nov 26 '18 at 9:05






  • 2





    I did what you said and the code worked fine. Thanks.

    – Cumhur Ay
    Nov 26 '18 at 11:24


















2














I cannot successfully reproduce the ajax requests that are used to update the page. I get Access Denied which makes me think there must be some protocol/authentication I am missing beyond the simple query string part.



Below is an example using selenium basic. It is slow as I am copying all the formatting across as the layout is a little finicky.



I have written something without using the clipboard which I may add later if I am happy with. It is a lot faster.



Option Explicit

Public Sub GetInfo()
Dim d As WebDriver, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, weeks As Object, i As Long
Const MAX_WAIT_SEC As Long = 15
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set d = New ChromeDriver
Const URL = "http://arsiv.sahadan.com/genis_ekran_iddaa_programi/"

With d
.Start "Chrome"
.get URL, timeout:=90000

Set weeks = .FindElementsByCss("#weekId option")
.FindElementByCss("[value='-1']").Click
For i = 1 To weeks.Count
If i > 1 Then
.FindElementsByCss("#weekId option")(i).Click
End If
Dim html As HTMLDocument
Set html = New HTMLDocument
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .FindElementByCss("#dvLarge #resultsList")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
clipboard.SetText ele.Attribute("outerHTML")
clipboard.PutInClipboard
ws.Cells.UnMerge
Application.Wait Now + TimeSerial(0, 0, 1)
ws.Cells(GetLastRow(ws, 1) + 1, 1).PasteSpecial
Application.Wait Now + TimeSerial(0, 0, 3)
End If

Set ele = Nothing
Next
.Quit
End With

End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function





share|improve this answer


























  • Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

    – Cumhur Ay
    Nov 22 '18 at 19:32






  • 1





    This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

    – QHarr
    Nov 22 '18 at 19:34








  • 1





    Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

    – QHarr
    Nov 22 '18 at 19:35






  • 1





    Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

    – QHarr
    Nov 26 '18 at 9:05






  • 2





    I did what you said and the code worked fine. Thanks.

    – Cumhur Ay
    Nov 26 '18 at 11:24
















2












2








2







I cannot successfully reproduce the ajax requests that are used to update the page. I get Access Denied which makes me think there must be some protocol/authentication I am missing beyond the simple query string part.



Below is an example using selenium basic. It is slow as I am copying all the formatting across as the layout is a little finicky.



I have written something without using the clipboard which I may add later if I am happy with. It is a lot faster.



Option Explicit

Public Sub GetInfo()
Dim d As WebDriver, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, weeks As Object, i As Long
Const MAX_WAIT_SEC As Long = 15
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set d = New ChromeDriver
Const URL = "http://arsiv.sahadan.com/genis_ekran_iddaa_programi/"

With d
.Start "Chrome"
.get URL, timeout:=90000

Set weeks = .FindElementsByCss("#weekId option")
.FindElementByCss("[value='-1']").Click
For i = 1 To weeks.Count
If i > 1 Then
.FindElementsByCss("#weekId option")(i).Click
End If
Dim html As HTMLDocument
Set html = New HTMLDocument
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .FindElementByCss("#dvLarge #resultsList")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
clipboard.SetText ele.Attribute("outerHTML")
clipboard.PutInClipboard
ws.Cells.UnMerge
Application.Wait Now + TimeSerial(0, 0, 1)
ws.Cells(GetLastRow(ws, 1) + 1, 1).PasteSpecial
Application.Wait Now + TimeSerial(0, 0, 3)
End If

Set ele = Nothing
Next
.Quit
End With

End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function





share|improve this answer















I cannot successfully reproduce the ajax requests that are used to update the page. I get Access Denied which makes me think there must be some protocol/authentication I am missing beyond the simple query string part.



Below is an example using selenium basic. It is slow as I am copying all the formatting across as the layout is a little finicky.



I have written something without using the clipboard which I may add later if I am happy with. It is a lot faster.



Option Explicit

Public Sub GetInfo()
Dim d As WebDriver, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, weeks As Object, i As Long
Const MAX_WAIT_SEC As Long = 15
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set d = New ChromeDriver
Const URL = "http://arsiv.sahadan.com/genis_ekran_iddaa_programi/"

With d
.Start "Chrome"
.get URL, timeout:=90000

Set weeks = .FindElementsByCss("#weekId option")
.FindElementByCss("[value='-1']").Click
For i = 1 To weeks.Count
If i > 1 Then
.FindElementsByCss("#weekId option")(i).Click
End If
Dim html As HTMLDocument
Set html = New HTMLDocument
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .FindElementByCss("#dvLarge #resultsList")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then
clipboard.SetText ele.Attribute("outerHTML")
clipboard.PutInClipboard
ws.Cells.UnMerge
Application.Wait Now + TimeSerial(0, 0, 1)
ws.Cells(GetLastRow(ws, 1) + 1, 1).PasteSpecial
Application.Wait Now + TimeSerial(0, 0, 3)
End If

Set ele = Nothing
Next
.Quit
End With

End Sub

Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 12:48

























answered Nov 22 '18 at 11:35









QHarrQHarr

32.1k82042




32.1k82042













  • Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

    – Cumhur Ay
    Nov 22 '18 at 19:32






  • 1





    This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

    – QHarr
    Nov 22 '18 at 19:34








  • 1





    Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

    – QHarr
    Nov 22 '18 at 19:35






  • 1





    Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

    – QHarr
    Nov 26 '18 at 9:05






  • 2





    I did what you said and the code worked fine. Thanks.

    – Cumhur Ay
    Nov 26 '18 at 11:24





















  • Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

    – Cumhur Ay
    Nov 22 '18 at 19:32






  • 1





    This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

    – QHarr
    Nov 22 '18 at 19:34








  • 1





    Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

    – QHarr
    Nov 22 '18 at 19:35






  • 1





    Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

    – QHarr
    Nov 26 '18 at 9:05






  • 2





    I did what you said and the code worked fine. Thanks.

    – Cumhur Ay
    Nov 26 '18 at 11:24



















Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

– Cumhur Ay
Nov 22 '18 at 19:32





Dear Mr. @QHarr, thank you very much for your effort. However, the macro returns a "User-defined type not defined" error in the WebDriver object. Is there a reference to open?

– Cumhur Ay
Nov 22 '18 at 19:32




1




1





This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

– QHarr
Nov 22 '18 at 19:34







This is selenium basic which requires installation of selenium basic and VBE > tools > references > selenium type library . I may write something for IE over next few days.

– QHarr
Nov 22 '18 at 19:34






1




1





Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

– QHarr
Nov 22 '18 at 19:35





Do you have a value for BASE_URL & "id=weekId&value=" & hafta(Hsay) that works when pasted in the browser?

– QHarr
Nov 22 '18 at 19:35




1




1





Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

– QHarr
Nov 26 '18 at 9:05





Likely you you either aren't using the latest chrome browser/chromedriver or chromedriver is not in the same folder as your selenium install. If you open the folder location of your selenium install and place your chromedriver.exe in there.

– QHarr
Nov 26 '18 at 9:05




2




2





I did what you said and the code worked fine. Thanks.

– Cumhur Ay
Nov 26 '18 at 11:24







I did what you said and the code worked fine. Thanks.

– Cumhur Ay
Nov 26 '18 at 11:24






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417990%2fi-want-to-draw-data-from-the-web-with-excel-macro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]