【问题标题】:How to solve a "BAD REQUEST" error while trying to import data from a hyperlink in excel using macros?尝试使用宏从 Excel 中的超链接导入数据时,如何解决“错误请求”错误?
【发布时间】:2020-05-06 15:50:31
【问题描述】:

我想从网站https://www.amfiindia.com/nav-history-download 导入一些数据。在此页面上,有一个链接“以文本格式下载完整的 NAV 报告”,它将为我提供所需的数据。但是这个链接不是静态的,所以我不能直接在 VBA 中使用它来下载我的数据。那么如何使用excel从网页的超链接中下载数据呢?

我的方法是先获取变量中的超链接,然后使用该变量获取数据?

  1. 首先,使用 getElementsByTagName 函数获取超链接,如下所示。
  2. 然后使用它作为 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 &amp; 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


【解决方案1】:

您可以设置像 this 这样的标题,即使用 .setrequestheader 方法。

我看到了这个标题信息:

GET /spages/NAVAll.txt?t=06052020095056 HTTP/1.1
Host: www.amfiindia.com
Connection: keep-alive
Cache-Control: max-age=0
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Cookie: __utma=57940026.1471746098.1588710696.1588710696.1588710696.1; __utmc=57940026; __utmz=57940026.1588710696.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
If-None-Match: "0d8e9bad223d61:0"
If-Modified-Since: Wed, 06 May 2020 18:18:24 GMT 

您不太可能需要所有这些,但很可能对于 403,您需要用户代理。例如.setRequestHeader "User-Agent","Mozilla/5.0"

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2021-08-07
    • 1970-01-01
    • 2021-02-03
    • 2020-08-20
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多