【问题标题】:Open webpage, select all, copy into sheet打开网页,全选,复制到工作表
【发布时间】:2018-07-23 08:42:44
【问题描述】:

希望从 Barcharts.com 复制股票期权数据并粘贴到 Excel 工作表中。

Sub CopyTables()

    Dim ie As Object
    Dim I As Long
    I = 0
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate "https://www.barchart.com/stocks/quotes/GOOG/options?moneyness=allRows&view=sbs&expiration=2018-02-23"
    ie.Visible = True

    Do While ie.Busy And Not ie.readyState = 4
        DoEvents
    Loop

    DoEvents

  Set tables = ie.document.getElementsByTagName("table")
  SetDataFromWebTable tables, Range("B5")
  ie.Quit
End Sub

另外,我如何从网页下拉菜单“过期”中提取日期并将它们全部粘贴到 Excel 中?

我一直在寻找对我有用的东西,但运气不好!

【问题讨论】:

  • ...而您对代码的具体问题是...?
  • 除了@Jeeped 注释,这个过程的代码在哪里:SetDataFromWebTable?

标签: json excel vba web-scraping xmlhttprequest


【解决方案1】:

所提供链接的网页源HTML

https://www.barchart.com/stocks/quotes/GOOG/options?moneyness=allRows&view=sbs&expiration=2018-02-23

不包含必要的数据,它使用 AJAX。 https://www.barchart.com 网站有一个可用的 API。响应以 JSON 格式返回。导航页面 e. G。在 Chrome 中,然后打开 Developer Tools 窗口 (F12)、Network 选项卡,重新加载 (F5) 页面并检查记录的 XHR。最相关的数据是 URL 返回的 JSON 字符串:

https://core-api.barchart.com/v1/options/chain?symbol=GOOG&fields=optionType%2CstrikePrice%2ClastPrice%2CpercentChange%2CbidPrice%2CaskPrice%2Cvolume%2CopenInterest&groupBy=strikePrice&meta=field.shortName%2Cfield.description%2Cfield.type&raw=1&expirationDate=2018-02-23

您可以使用下面的 VBA 代码来检索上述信息。 JSON.bas 模块导入VBA 项目进行JSON 处理。

Option Explicit

Sub Test48759011()

    Dim sUrl As String
    Dim sJSONString As String
    Dim vJSON As Variant
    Dim sState As String
    Dim aData()
    Dim aHeader()

    sUrl = "https://core-api.barchart.com/v1/options/chain?" & _
        Join(Array( _
            "symbol=GOOG", _
            "fields=" & _
            Join(Array( _
                "optionType", _
                "strikePrice", _
                "lastPrice", _
                "percentChange", _
                "bidPrice", _
                "askPrice", _
                "volume", _
                "openInterest"), _
            "%2C"), _
            "groupBy=", _
            "meta=" & _
            Join(Array( _
                "field.shortName", _
                "field.description", _
                "field.type"), _
            "%2C"), _
            "raw=1", _
            "expirationDate=2018-02-23"), _
        "&")
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sUrl, False
        .send
        sJSONString = .responseText
    End With
    JSON.Parse sJSONString, vJSON, sState
    vJSON = vJSON("data")
    JSON.ToArray vJSON, aData, aHeader
    With Sheets(1)
        .Cells.Delete
        .Cells.WrapText = False
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With

End Sub

Sub OutputArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

Sub Output2DArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
                UBound(aCells, 1) - LBound(aCells, 1) + 1, _
                UBound(aCells, 2) - LBound(aCells, 2) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

我的输出如下:

为了使输出更接近网页上的并排视图,您可以稍微调整一下查询参数:

    sUrl = "https://core-api.barchart.com/v1/options/chain?" & _
        Join(Array( _
            "symbol=GOOG", _
            "fields=" & _
            Join(Array( _
                "optionType", _
                "strikePrice", _
                "lastPrice", _
                "percentChange", _
                "bidPrice", _
                "askPrice", _
                "volume", _
                "openInterest"), _
            "%2C"), _
            "groupBy=strikePrice", _
            "meta=", _
            "raw=0", _
            "expirationDate=2018-02-23"), _
        "&")

还有换行

    Set vJSON = vJSON("data")

在这种情况下,输出如下:

顺便说一句,类似的方法适用于in other answers

【讨论】:

  • 这是一个不错的方法。为此加一个。
  • 非常感谢@omegastripes!太不可思议了!
  • @omegastripes 我整个上午都在努力寻找在您提供的脚本中并排设置 Call & Puts 列的方法,就像在网页 barchart.com/stocks/quotes/GOOG/… 中一样。你能帮忙吗?我会继续努力,但我将如何提取到期日期呢?多亏了你,我找到了可用的过期时间,但我不擅长这种类型的代码来提取它们:(
  • @Stotch 尝试修改sUrl 中的查询参数(也显示在屏幕截图中)。将expirationDate 添加到fields 参数,与其他类似。
  • @omegastripes 哇,太好了!我永远无法做到这一点。我的帽子给你!谢谢!!!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多