如何在Excel / Google表格中通过Web抓取网站?

时间:2019-06-02 13:42:10

标签: xpath web-scraping google-sheets google-sheets-formula google-sheets-importxml

我应该如何抓取此网页https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/,并特别需要表中提到的ROE数字?

我在Excel中使用了以下代码。我对Google Sheets Scraping不太了解

 Sub FetchData()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/", Destination:=Range( _
        "$A$1"))
        .Name = "www"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

我无法正确获取数据。

对此有任何建议/帮助吗?需要ROE数字,其余部分则不需要。

3 个答案:

答案 0 :(得分:0)

以下是我发现更容易获得该特定价值的方法。 for loop一旦检测到ROE,它将跟随所需的值并退出循环,因为它们都在同一父节点中。

Sub FetchData()
    Dim IE As New InternetExplorer, post As Object
    Dim Html As HTMLDocument, elem As Object

    With IE
        .Visible = False
        .navigate "https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    For Each post In Html.getElementsByTagName("td")
        If post.innerText = "ROE" Then
            Set elem = post.ParentNode.querySelector(".textvalue")
            Exit For
        End If
    Next post

    [A1] = elem.innerText
End Sub

要添加的参考:

Microsoft Html Object Library
Microsoft Internet Controls

答案 1 :(得分:0)

不幸的是,这是不可能的,因为该网站由JavaScript控制,并且Google表格无法理解/导入JS。您可以通过禁用给定链接的JS进行测试,您会看到一个空白页:

0

您所能获得的就是所看到的:

=ARRAY_CONSTRAIN(IMPORTDATA("https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"), 5000, 15)

答案 2 :(得分:0)

使用该页面可以更快使用该页面的API。您可以使用powerquery来处理json响应,json解析器或仅使用split。如果要在按下按钮时刷新,请将代码放入标准模块中并链接到按钮。

Option Explicit
Public Sub GetInfo()
    Dim s As String, ids(), i As Long
    ids = Array(500820, 500312, 500325, 532540)
    With CreateObject("MSXML2.XMLHTTP")
        For i = LBound(ids) To UBound(ids)
            .Open "GET", "https://api.bseindia.com/BseIndiaAPI/api/ComHeader/w?quotetype=EQ&scripcode=" & ids(i) & "&seriesid=", False
            .send
            s = .responseText
            ActiveSheet.Cells(i + 1, 1) = Split(Split(s, """ROE"":""")(1), Chr$(34))(0)
        Next
    End With
End Sub