asp.net - 使用 Excel VBA 从 aspx 页面的表格中检索数据

标签 asp.net vba excel http

我正在尝试使用 excel vba 从 aspx 页面检索表格数据。我知道如何从 URL 获取表格数据,但下面是主要问题。

问题

有一个 aspx 页面(比如 www.abc.aspx)。我目前在这个页面上。让这个页面成为page1

现在我单击当前页面上的 page2 链接。值得注意的是,点击此链接后,旧网址(www.abc.aspx)没有变化,但内容发生了变化。(内容为page2)

如果你查看page1源代码它有

<form method="post" action="page1 url" id="Form1">

无论 page1 上的操作(page2 点击)是什么,它都会返回相同的 page1 url。

那么,由于我不知道其 URL,如何在 excel VBA 中获取 page2 table 数据?

代码

这是我用来获取表格数据的。

我使用了 Internet Explorer 对象。然后导航到链接并将文档保存在 htmldoc 中。

ie.navigate "url"

Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Fetching data..."
DoEvents
Loop

Set htmldoc = ie.document

'Column headers
Set eleColth = htmldoc.getElementsByTagName("th")
j = 0 'start with the first value in the th collection
        For Each eleCol In eleColth 'for each element in the td collection
            ThisWorkbook.Sheets(1).Range("A1").Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
            j = j + 1 'move to next element in td collection
        Next eleCol 'rinse and repeat


'Content
Set eleColtr = htmldoc.getElementsByTagName("tr")

'This section populates Excel
    i = 0 'start with first value in tr collection
    For Each eleRow In eleColtr 'for each element in the tr collection
        Set eleColtd = htmldoc.getElementsByTagName("tr")(i).getElementsByTagName("td") 'get all the td elements in that specific tr
        j = 0 'start with the first value in the td collection
        For Each eleCol In eleColtd 'for each element in the td collection
            ThisWorkbook.Sheets(1).Range("D3").Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
            j = j + 1 'move to next element in td collection
        Next eleCol 'rinse and repeat
        i = i + 1 'move to next element in td collection
    Next eleRow 'rinse and repeat

ie.Quit
Set ie = Nothing

编辑:

示例

如果我们点击 Stack Overflow 中的问题 (https://stackoverflow.com/questions) 现在点击问题的第 2 页(新链接是 https://stackoverflow.com/questions ?page=2&sort=newest)

在我的例子中,如果我们点击 page2,新链接不会更新。它是同一个旧链接。

编辑:我在这里发现了一个类似的问题

How do I get url that is hidden by javascript on external website?

谢谢。

最佳答案

好吧,我很同情,有一个学派(包括 Tim Berners-Lee )说每个单独的页面都应该有自己的 URI 和 that these don't change .

但是网站管理员可以而且确实会把你搞得一团糟。他们可以重定向您的 HTTP 请求,并且可以像您的情况一样混淆导航。他们可以重写 HTTP 请求。

你有两个选择

选项 1 - 让 Internet Explorer 为您解析新内容

因此,如果内容在屏幕上可见,那么它一定在文档对象模型 (DOM) 中。在 IE 中,或者实际上在 Chrome 中,可以右键单击并获取上下文菜单,然后选择检查以查看该元素在 DOM 中的位置。

我认为您的代码展示了足够的专业知识来深入研究。但是,有时某些网站喜欢禁用“检查”菜单选项以避免程序员四处寻找。 (编辑:就像你的情况一样,我已经阅读了评论)

选项 2 - 使用 Fiddler 等 HTTP 嗅探工具来检测 HTTP 重定向/重写

正如我上面所说,HTTP 请求可以被网络服务器重写和重定向,但是 HTTP protocol does give notifications of redirects .有工具可以检测到这一点。一个流行的工具是 Fiddler ,今天我发现有一个特定的IE Fiddler add-on .

老实说,尽管浏览器本身附带的开发者工具(尤其是 Chrome)(Ctrl+Shift+I,然后是“网络”选项卡)显示网络流量的详细程度越来越接近于任何嗅探工具。

抱歉,您被否决了,这似乎是一个非常合理的问题。

关于asp.net - 使用 Excel VBA 从 aspx 页面的表格中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939045/

相关文章:

c# - MVC4 Bundle 中的 {version} 通配符

asp.net - 将 JSON 对象传递给 Web 方法

arrays - 将 VBA 数组元素输出到 Excel 中的一个单元格

c# - 获取发布的文件扩展名 asp.net

excel - 在 Excel 2016 Power Query 中创建时如何修改 Excel 2013 中的查询

excel - 如果我后来从其中任何一个中删除了任何超链接,为什么excel会删除所有粘贴的超链接?

database - VBA 中的记录集是什么? ... 它的作用是什么?

excel - Excel中一个单词的十六进制值

java - 使用 Apache Wink 通过 Java REST 服务返回 excel 文档

asp.net - 匹配 web.config 转换中的元素名称