【问题标题】:How to Web Scrape the Site in Excel/Google Sheets?如何在 Excel/Google 表格中抓取网站?
【发布时间】:2019-06-02 13:42:10
【问题描述】:

我应该如何抓取这个网页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,其余不需要。

【问题讨论】:

  • 缺少两个主要标签 excelvba
  • 这个问题太宽泛了,因为它显然是在询问在两个电子表格应用程序上的工作方式非常不同的功能。

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


【解决方案1】:

使用页面使用它的 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

【讨论】:

  • 谢谢,这很好用,如果我想使用多个公司数据怎么办?我该怎么办?
  • 你会使用循环。有哪些不同的公司?您需要将适当的 id 连接到 api 调用中。
  • 说实话,我对 VBA 不太感兴趣。就像我之前提到的,我可以做一些简单的事情。但我不能详细介绍这件事
  • 例如 500312、500325、532540
  • 现在明白了。谢谢!
【解决方案2】:

以下是我发现更容易获得特定价值的方式。一旦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

【讨论】:

  • 我担心我应该如何添加这些引用?
  • 查看this link 了解如何操作。
  • 非常感谢!我将对此进行更深入的研究。再次感谢
  • 完成!再次非常感谢。为我简化了很多。
  • SIM ~ 它给了我运行时错误'91':对象变量或未设置块变量
【解决方案3】:

很遗憾,这是不可能的,因为该网站由 JavaScript 控制,而 Google 表格无法理解/导入 JS。您可以通过禁用给定链接的 JS 来测试这一点,您将看到一个空白页面:

所见即所得:

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

【讨论】:

  • 手动输入数据,我猜,在自动化没有帮助的一天结束时。虽然我确实注意到他们改变了网站功能。我确实很容易在网站上抓取其他东西,但这似乎很难破解。你提到的一切都说明了。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-01-15
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多