Html 从标记名为“的网站表中提取数据”;表「;没有别的了

Html 从标记名为“的网站表中提取数据”;表「;没有别的了,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,要导入的数据位于一个表中,该表的标记名为“table”,除此之外没有其他内容 当我分配页面中的所有表时,我认为它不算作表 Sub PullData() Dim IE As New SHDocVw.InternetExplorer Dim hdoc As MSHTML.HTMLDocument Dim HEL As MSHTML.IHTMLElement Dim ha, hb, hc, hd, he, hf, hg, hh, hi, hj As String

要导入的数据位于一个表中,该表的标记名为“table”,除此之外没有其他内容

当我分配页面中的所有表时,我认为它不算作表

Sub PullData()

    Dim IE As New SHDocVw.InternetExplorer
    Dim hdoc As MSHTML.HTMLDocument
    Dim HEL As MSHTML.IHTMLElement
    Dim ha, hb, hc, hd, he, hf, hg, hh, hi, hj As String
    Dim i, x As Integer
    i = 2

    IE.Visible = True
    IE.navigate "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=FEL"
    Do While IE.readyState <> READYSTATE_COMPLETE
    Loop

    Set hdoc = IE.document
    Set HEL = hdoc.getElementById("tab8")
    HEL.Click
    Set HEL = hdoc.getElementById("period")
    HEL.Value = "3months"
    Set HEL = hdoc.getElementById("get")
    HEL.Click

End Sub
Sub-PullData()
Dim IE作为新的SHDocVw.InternetExplorer
将hdoc设置为MSHTML.HTMLDocument
Dim HEL As MSHTML.IHTMLElement
Dim ha、hb、hc、hd、he、hf、hg、hh、hi、hj作为字符串
Dim i,x为整数
i=2
可见=真实
即“导航”https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=FEL"
在IE.readyState readyState\u完成时执行此操作
环
设置hdoc=IE.document
Set HEL=hdoc.getElementById(“tab8”)
嗨,点击
Set HEL=hdoc.getElementById(“期间”)
帮助值=“3个月”
Set HEL=hdoc.getElementById(“get”)
嗨,点击
端接头

您可以使用URL中的查询字符串返回该信息。这意味着您可以直接使用比打开浏览器并进行选择快得多的方法

Option Explicit

Public Sub GetTable()
    Dim sResponse As String, html As HTMLDocument, clipboard As Object, ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=FEL&series=EQ&fromDate=undefined&toDate=undefined&datePeriod=3months", False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With
    Set html = New HTMLDocument
    html.body.innerHTML = sResponse

    clipboard.SetText html.querySelector("table").outerHTML
    clipboard.PutInClipboard
    ws.Cells(1, 1).PasteSpecial
End Sub

Less tidy正在拦截用于文件下载的URL,并将其用于二进制下载:

Option Explicit

Public Sub DownloadFile()
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=FEL&series=EQ&fromDate=undefined&toDate=undefined&datePeriod=3months&hiddDwnld=true", False
    http.send
    With CreateObject("ADODB.Stream")
        .Open
        .Type = 1
        .write http.responseBody
        .SaveToFile "C:\Users\User\Desktop\TestDownload.csv" '<== specify your path here
        .Close
    End With
    Debug.Print "FileDownloaded"
    Exit Sub
errhand:
    If Err.Number <> 0 Then
        Debug.Print Err.Number, Err.Description
        MsgBox "Download failed"
    End If
End Sub
选项显式
公共子下载文件()
Dim http作为对象
设置http=CreateObject(“MSXML2.XMLHTTP”)
http.Open“GET”https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/getHistoricalData.jsp?symbol=FEL&series=EQ&fromDate=undefined&toDate=undefined&datePeriod=3months&hiddDwnld=true”“错
http.send
使用CreateObject(“ADODB.Stream”)
打开
.Type=1
.write http.responseBody

.SaveToFile“C:\Users\User\Desktop\TestDownload.csv”'能否显示页面的HTML,以便我们更好地了解DOM的外观?该表名的可见文本是什么?这真的不清楚。我还有一个问题,我想画一个特定日期的数据行,比如9月3日。你说你想要这个表。这还没有回答原来的问题吗?您的新问题是如何获取特定日期的数据?是的,代码正在运行。我有很多链接,从那里我必须带来9月3日的数据。所以,这是一个新的问题?查询字符串似乎有参数,您可以指定日期。