Home » excel » html – I can't extract data from a site that does query with Java script

html – I can't extract data from a site that does query with Java script

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to get data from an Internet site, but I get an error.

I’ve modified earlier codes.

Sub DENEME()
    Dim S As String
    Dim html As HTMLDocument
    Dim hTable As HTMLTable
    Dim clipboard As Object
    Set html = New HTMLDocument
    With New XMLHTTP60
        .Open "GET", "https://www.scorespro.com/basketball/results/date/2019-02-15", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send
        S = .responseText
    End With
    html.body.innerHTML = S
    Set hTable = html.querySelector(".matches-data")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    clipboard.SetText hTable.outerHTML
    clipboard.PutInClipboard
    Range("A1").PasteSpecial
End Sub

run-time error ’91’

Note: Dear @QHarr Greetings

How to&Answers:

A few points to note.

  1. Yes it is id not class for that element but that element is a div and copy pasting via clipboard the outer html will only paste the html of the div – not all tables within.
  2. What you want is a list of tables so you need to change your selector to get the tables within that div
  3. As using clipboard and there are merged output cells you need to find the last used row independent of column and add 1 to write out next table to a few row.
  4. If unsure about your selector use the search bar in browser as shown [here].1

VBA:

Option Explicit
Public Sub Deneme()
    Dim s As String, ws As Worksheet, tables As Object, i As Long
    Dim html As HTMLDocument, clipboard As Object
    Set html = New HTMLDocument
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Application.ScreenUpdating = False
    With New XMLHTTP60
        .Open "GET", "https://www.scorespro.com/basketball/results/date/2019-02-15", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send
        s = .responseText
    End With

    html.body.innerHTML = s
    Set tables = html.querySelectorAll("#matches-data table")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    For i = 0 To tables.Length - 1
        clipboard.SetText tables.item(i).outerHTML
        clipboard.PutInClipboard
        ws.Range("A" & GetLastRow(ws) + 1).PasteSpecial
    Next
    Application.ScreenUpdating = True
End Sub

Public Function GetLastRow(ByVal sh As Worksheet) As Long
    On Error Resume Next
    GetLastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function

Answer:

Your selector string is set up so that it looks for the first element whose class is matches-data. However, matches-data is the id. As a result, hTable is being assigned Nothing, hence the error. Instead, try the following…

Set hTable = html.querySelector("#matches-data")