【问题标题】:Fetching text from website using VBA for Mac使用 VBA for Mac 从网站获取文本
【发布时间】:2020-02-17 16:04:07
【问题描述】:

如果这是一个非常基本的问题,我很抱歉,但我真的很绝望。我想获取网站https://cactus.nci.nih.gov/chemical/structure/78-70-6/smiles 的输出,并使用VBA 将其输入到Excel 单元格中。有谁知道这样做的简单直接的方法?

在某种程度上,我想要 bash 中的“curl”之类的东西,但它可以在 VBA for Excel 中使用。

我正在使用 Excel for Mac,版本 16.16.14,带有 VBA 7.1。

我尝试了许多在线建议的方法,但在我的 Excel 版本上似乎没有任何效果。

非常感谢。

【问题讨论】:

  • 我猜你先尝试使用 Get & Transform 而不是 VBA? mac 版中是否有一些缺失的功能使其变得更加困难?
  • 不。这个想法是从一开始就使用 VBA 函数,因为这就是我所知道的,从使用数值函数开始。这两个功能是否允许创建类似函数的运算符?
  • Power Query/Get & Transform 是 excel 中用于通过 gui 导入外部数据的一组功能。它相对较新,功能因版本而异,所以我不确定您在 Mac excel 中有哪些选项。链接到特定网页并将数据刷新到范围很简单。也可以将其设置为使用动态 url,但过程因版本而异。

标签: excel vba macos


【解决方案1】:

这应该可以解决问题;

    Dim InternetExplorer As Object 'dim your internet explorer
    Dim WebsiteURL As String: WebsiteURL = "https://cactus.nci.nih.gov/chemical/structure/78-70-6/smiles" 'dim and set your website url
    Dim WebsiteText As String 'dim a variable to store the website text

    Set InternetExplorer = CreateObject("InternetExplorer.Application") 'set your internet explorer

    With InternetExplorer
      .Visible = 1 'set to show (if you put 0 the browser would be hidden - just remember to set to nothing at end)
      .navigate WebsiteURL 'navigates to websiteurl
       While .Busy Or .readyState <> 4 'waits for page to load
         DoEvents
       Wend
    End With

    Dim WebPage As HTMLDocument 'dim your webpage as htmldocument
    Set WebPage = InternetExplorer.document 'set your webpage to the internet explorer

    WebsiteText = WebPage.body.innerText: Range("A1").Value = WebsiteText 'puts website text into variable : write to whatever cell you want

编辑: 刚刚注意到你说的是 MAC - 我很糟糕,因为它使用了 Internet Explorer

【讨论】:

  • 是的,“InternetExplorer.Application”这个东西在 Mac 上不起作用……
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多