【问题标题】:How can I scrape worded information from a website?如何从网站上抓取文字信息?
【发布时间】:2016-08-25 13:37:22
【问题描述】:

我一般是 VBA 和 html 编码的新手。如果我不理解基本术语或错误使用它们,我深表歉意。我正在寻找在 excel 中创建和运行一个宏来让我的工作变得更容易。本质上,我需要从房地产网站上获取大量信息。这包括地址、标价、上市机构、拍卖日期(如果有的话)等。我花了过去 4 个小时阅读所有关于网络抓取的内容,我了解这些过程,但我只是不知道如何编码。根据我的阅读,我需要编写一个代码来自动打开网站,强制等待它加载,然后通过标签、名称或 ID 检索信息。它是否正确?我该怎么办。我应该使用哪些资源。

TL;DR 如何从搜索结果网页中抓取文本(菜鸟指令)。

【问题讨论】:

    标签: excel vba scrape


    【解决方案1】:

    我不会告诉你所有细节,你必须自己找到它们。有些网页很复杂,有些网页很简单。其他是不可能的,特别是如果文本不是以 HTML 而是以其他形式显示时 - 图片、Flash 等。

    然而,在 Excel 中从 HTML 网页中提取数据非常简单。首先,你想自动化它。因此,单击“开发人员”功能区上的“记录宏”。这样,您将记录所有可重现的步骤,然后您可以查看宏并调整一些步骤以满足您的需要。但是我不能在这里教你如何编写 VBA。

    录制宏时,点击“数据”功能区上的“来自网络”。这将显示一个新的网络查询。然后输入您要阅读的网页地址,并尝试选择(带有小箭头或勾号)您感兴趣的尽可能窄的区域。您还可以在此向导对话框中探索一些微调选项。

    完成后,单击“导入”,您将以某种形式获得网页内容。如果幸运的话,您感兴趣的数据将始终位于相同的单元格中。然后您可以读取单元格并将值存储在某处(可能使用另一个宏)。如果每次刷新查询时数据不在同一个单元格中,那么你运气不好,必须使用一些复杂的公式或宏来查找它们。

    接下来停止您正在录制的宏并查看已录制的代码。尝试尝试并使用它,直到你发现你真正需要的东西。然后由你决定,你想如何自动化它。选项很多...

    否则 Excel 可能不是最好的工具。如果我想加载 HTML 页面并从中提取数据,我会使用一些脚本,例如Python 拥有比 Excel 和 VBA 更好的工具。还有一些工具可以将 HTML 转换为 XHTML,然后像从格式良好的 XML 中一样从中提取数据。

    【讨论】:

      【解决方案2】:

      下面是一个非常基本的示例,说明了网络抓取的一些概念。您应该阅读的其他内容是如何使用其他元素选择器,例如 getElementByID getElementByClassName getElementByName

      这里有一些代码可以帮助您入门。

      Public Sub ExampleWebScraper()
          Dim Browser         As Object: Set Browser = CreateObject("InternetExplorer.Application")
          Dim Elements        As Object 'Will hold all the elements in a collection
          Dim Element         As Object 'Our iterator that will show us the properties
      
          'Open a page and wait for it to load
          With Browser
              .Visible = True
              .Navigate "www.google.com"
      
              'Wait for the page to load
              While .busy Or .readystate <> 4
                  Application.Wait (Now() + TimeValue("00:00:01"))
              Wend
      
              'Enumerate all Elements on the page
              'It will store these elements into a collection which we can
              'iterate over. The * is the key for ALL, here you can specify
              'any tagName and it will limit your search to just those.
              'E.g. the most common is Likely Input
              Set Elements = .document.getElementsByTagname("*") ' All elements
      
              'Iterate through all elements, and print out some properties
              For Each Element In Elements
                  On Error Resume Next ' This is needed as not all elements have the properties below
                                       ' if you try and return a property that doesn't exist for that element
                                       ' you will receive an error
                  'The following information will be output to the 'Immediate Window'
                  'If you don't see this window, Press Ctrl+G, and it will pop up. That's where this info will display
                  Debug.Print "The Inner Text is: " & Element.InnerText
                  Debug.Print "The Value is: " & Element.Value
                  Debug.Print "The Name is: " & Element.Name
                  Debug.Print "The ID is: " & Element.ID
                  Debug.Print "The ClassName is: " & Element.Class
              Next Element
          End With
      
          'Clean up, free memory
          Set Browser = Nothing
          Set Elements = Nothing
          Set Element = Nothing
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-09
        • 2020-01-09
        • 1970-01-01
        相关资源
        最近更新 更多