加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:24:56 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用excel vba从aspx页面检索表数据.我知道如何从URL获取表数据但下面是主要问题. 问题 有一个aspx页面(比如www.abc.aspx).我目前在此页面.请将此页面设为page1. 现在,我单击当前页面上的page2链接.值得注意的是,点击此链接后,旧URL(www.abc.aspx)
我正在尝试使用excel vba从aspx页面检索表数据.我知道如何从URL获取表数据但下面是主要问题.

问题

有一个aspx页面(比如www.abc.aspx).我目前在此页面.请将此页面设为page1.

现在,我单击当前页面上的page2链接.值得注意的是,点击此链接后,旧URL(www.abc.aspx)不会更改,但内容会发生变化.(内容属于第2页)

如果您查看它的page1源代码

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

无论第1页(第2页点击)上的操作是什么,它都会回发相同的page1网址.

那么如何在excel VBA中获取page2表数据,因为我不知道它的URL?

这就是我用来获取表数据的方法.

我使用了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,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)

在我的情况下,如果我们点击第2页,新链接不会更新.它是相同的旧链接.

编辑:我在这里找到了类似的问题

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中,可以右键单击并获取上下文菜单,然后选择Inspect以查看该元素所在的DOM中的位置.

我认为你的代码展示了足够的专业知识来钻取.但是,有时一些网站喜欢禁用Inspect菜单选项以避免程序员四处寻找. (编辑:就像你现在我已阅读评论一样)

选项2 – 使用像Fiddler这样的HTTP嗅探工具来检测HTTP重定向/重写

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

说实话,虽然浏览器本身附带的开发人员工具,特别是Chrome(Ctrl Shift I,然后是网络选项卡),网络流量显示的细节水平越来越与任何嗅探工具相提并论.

对不起,你得到了投票,这似乎是一个非常合理的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读