【问题标题】:Extract data from a website to a .csv file将数据从网站提取到 .csv 文件
【发布时间】:2020-10-06 03:38:34
【问题描述】:

一周以来,我一直在尝试在这里找到的每个示例(甚至尝试将一些示例从 JavaScript 转换为 VBA),但无法找到适合我想要的网站的东西

网址:https://www.inspq.qc.ca/covid-19/donnees

我可以手动点击每个图表右上角的 3 个点,点击“Télécharger les données en format CSV”选项,然后我会在 .csv 文件中获取图表的原始数据

当我检查我看到的元素时:

<g class="highcharts-button highcharts-contextbutton                 highcharts-button-normal" stroke-linecap="round" transform="translate(1412,10)"><rect fill="#ffffff" class="highcharts-button-box" x="0.5" y="0.5" width="24" height="22" rx="2" ry="2" stroke="none" stroke-width="1"></rect><title>Chart context menu</title><path fill="#666666" d="M 12.666666666666666 6.666666666666668 A 1.3333333333333335 1.3333333333333335 0 1 1 12.667999999777779 6.666666000000056 Z M 12.666666666666666 13.333333333333336 A 1.3333333333333335 1.3333333333333335 0 1 1 12.667999999777779 13.333332666666724 Z M 12.666666666666666 20 A 1.3333333333333335 1.3333333333333335 0 1 1 12.667999999777779 19.999999333333392 Z" class="highcharts-button-symbol" data-z-index="1" stroke="#666666" stroke-width="3"></path><text x="0" data-z-index="1" style="color:#333333;cursor:pointer;font-weight:normal;fill:#333333;" y="12"></text></g>

但我不明白如何更改我在此处找到的每个示例以单击此按钮并选择将原始数据导出到 CSV 文件的选项

【问题讨论】:

  • 我很确定这是使用 javascript 和/或 jquery 完成的。如果您查看源代码,您会发现该页面有很多 javascript 和至少两个 jquery 代码。生成 csv 的代码可以隐藏在其中的任何位置
  • 我尝试在按下链接时监控网络,但日志中没有出现任何内容。我假设它是javascript。

标签: excel vba csv web-scraping


【解决方案1】:

我们与 JS 无关,因为 IE 会自动为我们做这件事。我们“只”必须自动化 IE。

要管理下载,我们必须使用Sendkeys()。这不是很优雅,但我不知道像 theese 这样的 donload 有任何其他解决方案。因此,必须显示 IE,并且它必须具有焦点!

文件将保存在 IE 的标准下载目录中。它们都具有相同的名称​​chart.csv。但这没问题,因为 Windows 会为每个文件名添加一个自己的编号。所以没有编号的文件是第一个。所有其他按以下顺序编号。

我已经评论了代码。所以你可以看到它是如何工作的:

Sub DownloadCovidCSVs()

Const url As String = "https://www.inspq.qc.ca/covid-19/donnees"

Dim ie As Object
Dim nodesAllThreeDots As Object
Dim nodeOneThreeDots As Object
Dim nodeMenueEntries As Object
Dim timeout As Double
Dim failed As Boolean

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set ie = CreateObject("internetexplorer.application")
  ie.Visible = True
  ie.navigate url
  Do Until ie.ReadyState = 4: DoEvents: Loop
  
  'Start time for timeout if the charts would'nt load
  timeout = Timer
  'Waiting until the charts were loaded or timeout takes effect
  Do
    Set nodesAllThreeDots = ie.document.getElementsByClassName("highcharts-contextbutton")
  Loop Until nodesAllThreeDots.Length > 0 Or Timer - timeout > 30 'Timeout in seconds
  
  'Check if charts not been loaded
  If nodesAllThreeDots.Length = 0 Then
    failed = True
  End If
  
  'If charts were loaded, download csv files
  If Not failed Then
    For Each nodeOneThreeDots In nodesAllThreeDots
      'Open current menu
      nodeOneThreeDots.Click
      
      'Get the last entry and click it to initialize download
      Set nodeMenueEntries = ie.document.getElementsByClassName("highcharts-menu-item")
      nodeMenueEntries(nodeMenueEntries.Length - 1).Click
      
      'Give the server time to generate the document and the IE to show the download button at it's bottom
      Application.Wait (Now + TimeValue("0:00:03"))
      
      'Attention!
      'We have to use SendKeys to perform the download!!!
      'As long as the macro is running, keep your fingers away from the mouse and keyboard!!!
      Application.SendKeys ("%{S}")
    Next nodeOneThreeDots
  End If
  
  'Clean up
  ie.Quit
  Set nodesAllThreeDots = Nothing
  Set nodeOneThreeDots = Nothing
  Set nodeMenueEntries = Nothing
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多