【发布时间】:2020-05-06 15:50:31
【问题描述】:
我想从网站https://www.amfiindia.com/nav-history-download 导入一些数据。在此页面上,有一个链接“以文本格式下载完整的 NAV 报告”,它将为我提供所需的数据。但是这个链接不是静态的,所以我不能直接在 VBA 中使用它来下载我的数据。那么如何使用excel从网页的超链接中下载数据呢?
我的方法是先获取变量中的超链接,然后使用该变量获取数据?
- 首先,使用 getElementsByTagName 函数获取超链接,如下所示。
- 然后使用它作为 URL 来获取数据。
但是当我向该超链接发送请求时,我收到了“错误请求”响应。我不知道为什么会出现这个错误。我使用的代码是
Sub GrabLastNames()
'dimension (set aside memory for) our variables
Dim objIE As InternetExplorer
Dim ele As Object
Dim y As Integer
Dim mtbl As String
Dim request As Object
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
Dim cellAddress As String
Dim rowNumber As Long
'start a new browser instance
Set objIE = New InternetExplorer
'make browser visible
objIE.Visible = True
'navigate to page with needed data
objIE.navigate "https://www.amfiindia.com/nav-history-download"
'wait for page to load
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
' ht.querySelector(".nav-hist-dwnld a").href
'we will output data to excel, starting on row 1
y = 1
mtbl = objIE.document.querySelector(".nav-hist-dwnld a").href
' mtbl = Sheets("Sheet1").Range("A" & y).Value
' Website to go to.
' website = mtbl
' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")
' Where to go and how to go there - probably don't need to change this.
request.Open "GET", mtbl, 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
' MsqBox "bye"
' Get the webpage response data into a variable.
response = request.responseText
' Put the webpage into an html object to make data references easier.
'html.body.innerHTML = response
MsgBox "Hi"
' MsgBox "Bye Bye"
' Get the price from the specified element on the page.
Sheets("Sheet1").Range("A" & y + 1).Value = "Hi"
MsgBox response
'look at all the 'tr' elements in the 'table' with id 'myTable',
'and evaluate each, one at a time, using 'ele' variable
ActiveWorkbook.Save
End Sub
响应变量应该包含来自网站的所有数据,但它在 msgBox 中打印了这个“错误请求”。
【问题讨论】:
-
mtbl的值是多少? -
您是否检查过 mtbl 是一个完整的 url 以及请求是否需要任何标头?
-
mtbl 有我要从中导入数据的 url。此网址不是静态的。现在mtbl有amfiindia.com/spages/NAVAll.txt?t=06052020095056这个链接
-
谢谢@QHarr,我检查了标题,并用这样的超链接将它连接起来:
mtbl = objIE.document.querySelector(".nav-hist-dwnld a").href Header = "https://www.amfiindia.com" website = Header & mtbl现在我没有收到错误的请求错误,但它说的是一个新错误The system cannot locate the resource specified。 -
这不是我所说的标题。您的网址很好且完整。您可以像这样设置标题:codingislove.com/http-requests-excel-vba 即使用 .setrequestheader 方法。我看到了这个标题信息:pastebin.com/TmSUn9vk。您不太可能需要所有这些,但很可能对于 403,您需要用户代理。例如.setRequestHeader "用户代理","Mozilla/5.0"
标签: excel vba hyperlink bad-request