从具有标签名称“ table”的网站表中提取数据,仅此而已

时间:2018-11-26 15:25:49

标签: html excel vba web-scraping

要导入的数据在一个表中,该表的标签名称为“ table”,仅此而已。

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    if let childViewController = segue.destination as? MyContentViewController {
        childViewController.view.translatesAutoresizingMaskIntoConstraints = false
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

少了一些小巧的东西,它拦截了用于文件下载的URL和用于二进制下载的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