如何获得网站生成的数据到Excel

时间:2019-07-19 09:03:27

标签: excel vba

您是否知道如何将这张小桌子从website变成Excel?

正常的源代码清理将不起作用,因为结果未存储在源代码中。超级查询也不起作用...

编辑:

我尝试了Power Query。我有一些代码可以从按类,标记等搜索的网站上下载数据-但是所有代码都在源代码中搜索,而不是在呈现的网站中搜索,因此发布这样的代码(仅发布任何内容)是没有意义的。

2 个答案:

答案 0 :(得分:0)

我使用Chrome,并将结果存储在源代码中。然后,我只是将html代码复制到在线html到csv中:

Html to csv online editor

对我有用。或者,如果这不是您的解决方案,请尝试更好地描述您的问题。

答案 1 :(得分:0)

我知道从网络抓取开始有时会很麻烦,并且那里的信息量可能非常庞大,所以我决定启动您的工作,希望将来您至少知道从哪里开始。< / p>

  1. 检查网络流量。

使用浏览器的开发人员工具检查浏览网站时发送的请求。在您的情况下,按搜索键时发送的请求很多。但是,您只需要其中之一。产生该表作为响应的是XHR请求。

enter image description here

  1. 检查请求本身

该请求基本上由一个URL组成,该URL包含您在下拉菜单中选择的参数,标头(对于您而言不是必不可少的结果)和一个正文(对于您而言为空,因为所有参数都包含在其中)网址。

enter image description here

  1. 检查回复

您的情况是HTML。可能是JSON之类的东西。您想要的数据在ID为“ qoutaTable”的HTML表中。

<html>

<head>
  <!-- Including version.html for defect CUSTD00035918 Start -->
  <meta name="application" content="DDS2-TARIC" />
  <meta name="version" content="@REL@" />

  <!-- Defect# CUSTD00024730 Start -->
  <!-- IPG Rule requires the following 7 metatags in all application pages. Additional metatags e.g. version and application can be added if required by the application. -->
  <meta http-equiv="Content-Language" content="en">
  <meta name="description" content="DDS2-TARIC Application page">
  <meta name="reference" content="DDS2-TARIC Reference">
  <meta name="creator" content="DG-TAXUD">
  <meta name="classification" content="DDS2-TARIC">
  <meta name="keywords" content="DDS2-TARIC, TARIC, DDS2">
  <meta name="date" content="">
  <!-- Defect# CUSTD00024730 End -->
  <!-- Including version.html for defect CUSTD00035918 End -->
</head>



<body style="background-color:#FFFFF0;">












  <div id="quotaMarkedUpContainer">

    <div class='scroller' id="navigation" align=center>
      <table>
        <tr>
          <td>

          </td>


          <td>

          </td>
        </tr>
      </table>
    </div>

    <table id="quotaTable" class="list" width="100%" style="padding-left: 7%; padding-right: 7%;">

      <thead>
        <tr class="columnHeader">
          <th>
            Order number
          </th>
          <th>
            Origins
          </th>
          <th style="text-align: center;">
            Start date
          </th>
          <th style="text-align: center;">
            End date
          </th>
          <th style="text-align: right;">
            Balance
          </th>
          <th/>
        </tr>
      </thead>










      <tr class="oddRow">

        <td>
          096714
        </td>
        <td>






          <div>
            Ukraine
          </div>


        </td>
        <td style="text-align: center;">
          01-01-2019
        </td>
        <td style="text-align: center;">
          31-12-2019
        </td>
        <td style="text-align: right;">

          0&nbsp; Kilogram


        </td>
        <td>

          <a id="quotaLink" href="https://ec.europa.eu/taxation_customs/dds2/taric/quota_tariff_details.jsp?Lang=en&StartDate=2019-01-01&Code=096714" style="color:#3247e8; text-decoration:underline;" class='browse_action_a'>[More info]</a>


        </td>
      </tr>



    </table>

    <div class='scroller' id="navigation" align=center>
      <table>
        <tr>
          <td>

          </td>


          <td>

          </td>
        </tr>
      </table>
    </div>

  </div>





</body>

</html>

  1. 编写代码

为此,您需要以下参考文献

  • Microsoft WinHTTP Services版本5.1(用于创建和处理HTTP请求)
  • Microsoft HTML对象库(用于处理HTML元素)

下面是一个如何获取表格单元格之一的示例:

Option Explicit
Sub getData()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim table As HTMLTable
Dim url As String, code As String, year As String, origin As String, status As String, critical As String 'the request's parameters

critical = "" 'you can leave it blank if it's not important to your search
status = "" 'you can leave it blank if it's not important to your search
origin = "UA"
year = "2019"
code = "096714"
url = "https://ec.europa.eu/taxation_customs/dds2/taric/quota_list.jsp?Lang=en&Origin=" & origin & "&Code=" & code & "&Year=" & year & "&Status=" & status & "&Critical=" & critical & "&Expand=true&Offset=0" 'build the URL by concatenating the various parameters

With req
    .Open "GET", url, False
    .send
    doc.body.innerHTML = .responseText 'Assign the HTML response to an HTML document object
    'Debug.Print .responseText
End With
Set table = doc.getElementById("quotaTable") 'get the table you're interested in
Debug.Print table.Rows(1).Cells(4).innerText 'print the 5th cell of the 2nd row in the immediate window
End Sub

结果如下:

enter image description here

出于演示目的,我仅向您展示如何打印表格单元格之一的内容。您可以尝试上面的代码并对其进行修改,以获取对表中其他元素的访问权限。