#excel #vba
Вопрос:
Как новое кодирование, возможно, этот вопрос может показаться глупым, но когда я пытаюсь запустить этот код, он продолжает показывать ошибку 91, выделяющую эту строку
'Get the number from the specified element on the page number = html.getElementsByClassName("arial_14 redFont").Item(0).innerText
Поскольку я уже определил номер в начале, я понятия не имею, почему он продолжает показывать, что переменная не была определена.
Sub Get_Housing_Starts_From_Investing() Dim request As Object Dim response As String Dim html As New HTMLDocument Dim website As String Dim number As Variant ' Source: https://www.youtube.com/watch?v=IOzHacoP-u4 ' Remember to activate References: Microsoft Office 16.0 Object Library, Microsoft HTML Object Library, Microsoft XML, v6.0, Visual Basic For Applications, Microsoft Excel 16.0 Object Library, OLE Automation ' Website to go to website = "https://www.investing.com/economic-calendar/housing-starts-151" ' Create the object that will make the website request Set request = CreateObject("Msxml2.ServerXMLHTTP.6.0") ' Where to go and how to go there - don´t need to change it request.Open "GET", website, False ' Get fresh data request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ' Send the request for the webpage request.send ' Get the webpage response data into a variable response = StrConv(request.responseBody, vbUnicode) ' Put the webpage into an html object to make data references easier html.body.innerHTML = response 'Get the number from the specified element on the page number = html.getElementsByClassName("arial_14 redFont").Item(0).innerText ' Output the number into a message box MsgBox number End Sub
Комментарии:
1. Это, вероятно, означает, что
getElementsByClassName("arial_14 redFont")
он не вернул никакого объекта и поэтомуItem(0)
не существует. И еслиDebug.Print response
вы можете прочитать: «У вас нет разрешения на доступ к /экономическому календарю/началу строительства-151 на этом сервере».2. Но я могу получить доступ к этой веб-странице с помощью Chrome. Является ли это каким-то ограничением со стороны Investing.com?
3. многие веб-сайты не допускают автоматического извлечения данных/скриптов и пытаются предотвратить это. Или данные загружаются с помощью JavaScript (который запускается только в браузере). Проверьте, есть ли на веб-сайте официальный API, и используйте его вместо этого.