从网页中提取表

时间:2019-03-04 06:24:28

标签: javascript html excel vba web-scraping

我将提取网页“ https://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&scripcode=503100&flag=sp&Submit=G”上具有“日期|开盘|高|低|收盘|股数|交易数|总成交量|可交付数量”的表

下面是我的代码:

Sub Macro_BSE()


  Application.ScreenUpdating = False

  Dim FileName, Pathname As String

    MP = ActiveWorkbook.Name

    Workbooks.Add
    WB2 = ActiveWorkbook.Name

    Dim IE As New SHDocVw.internetexplorer

    Const MAX_WAIT_SEC As Long = 5
    Dim frm As Variant
    Dim element, submitInput As Variant
    Dim rowCollection, htmlRow As Variant
    Dim rowSubContent, rowSubData As Variant
    Dim anchorRange As Range, cellRng As Range
    Dim start
    Dim A As String
    Dim hTable As HTMLTable
    Dim clipboard As Object

    IE.Visible = True
    IE.navigate "https://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&scripcode=503100&flag=sp&Submit=G"
    While IE.readyState <> 4: DoEvents: Wend

       Application.Wait (Now + TimeValue("00:00:02"))

    IE.document.querySelector("#ContentPlaceHolder1_rdbDaily").Click

    IE.document.querySelector("[name='ctl00$ContentPlaceHolder1$txtFromDate']").Value = "28/11/2018"
    IE.document.querySelector("[name='ctl00$ContentPlaceHolder1$txtToDate']").Value = "28/12/2018"
    IE.document.querySelector("[name='ctl00$ContentPlaceHolder1$btnSubmit']").Click

    Application.Wait (Now + TimeValue("00:00:10"))

     T = Timer
        Do
            On Error Resume Next
            Set hTable = IE.document.querySelector("#ContentPlaceHolder1_spnStkData table")
            On Error GoTo 0
            If Timer - T > MAX_WAIT_SEC Then Exit Do
        Loop While hTable Is Nothing
        If Not hTable Is Nothing Then
            clipboard.SetText hTable.outerHTML
            clipboard.PutInClipboard
            Workbooks(WB2).Worksheets("Sheet1").Range("A1").PasteSpecial

        End If

End Sub

到目前为止,我无法提取该表。以前我能够从相同的代码中提取

请提出更改建议

1 个答案:

答案 0 :(得分:0)

您永远不会创建剪贴板对象的实例。在尝试向其中添加文本之前,请先这样做。

Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
clipboard.SetText hTable.outerHTML