【问题标题】:VBA code to scrape data using html/javascript won't work使用 html/javascript 抓取数据的 VBA 代码不起作用
【发布时间】:2020-10-26 08:01:00
【问题描述】:

我想根据第一列中的输入制作VBA 代码以在网站上搜索。范围从 A1 到 A102。这段代码工作正常,除了一件事:它从 Excel 单元格复制我的数据,然后将其粘贴到网站的搜索框中。但它不会自动单击搜索按钮。欢迎各位专家提出好的建议。

我知道如何从网站上抓取数据,但是这个搜索框按钮有一个特定的类。我应该用什么类来点击?这个问题与 VBA 和 javascript/html 专家都有关。

当我单击 Inspect 元素时,我将其作为按钮 ID " nav-search-submit-text " 并将此代码作为“类“nav-search-submit-text nav-sprite”。

两个都不行?

谢谢

Private Sub worksheet_change(ByVal target As Range)

If Not Intersect(target, Range("A1:A102")) Is Nothing Then

Call getdata

End If

End Sub

Sub getdata()

Dim i As Long

Dim URL As String

Dim IE As Object

Dim objElement As Object

Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")

'Set IE.Visible = True to make IE visible, or False for IE to run in the background

IE.Visible = True

URL = "https://www.amazon.co.uk"

'Navigate to URL

IE.Navigate URL

'making sure the page is done loading

 Do
   
 DoEvents

 Loop Until IE.ReadyState = 4

 'attempting to search date based on date value in cell

 IE.Document.getElementById("twotabsearchtextbox").Value = ActiveCell.Value

 'Sheets("Sheet1").Range("A1:A102").Text

 'Select the date picker box and press Enter to 'activate' the new date

 IE.Document.getElementById("twotabsearchtextbox").Select

 'clicking the search button

 IE.Document.getElementsByClassName("nav-sprite").Click

 'Call nextfunction

 End Sub

【问题讨论】:

  • 我给你建议,处理 URL 的结构。要在亚马逊上搜索产品,您只需要为相应搜索词构建的 URL。基本 URL 是https://www.amazon.co.uk/s?k=,后跟搜索词。例如,如果您要查找ear buds,请在等号后写上ear+buds。为确保 URL 有效,请将搜索词放入 URL 编码公式support.microsoft.com/en-us/office/…
  • 另外两个说明:如果您使用IE.Document.getElementById("twotabsearchtextbox").Select,则不会发生任何事情,并且IE.Document.getElementsByClassName("nav-sprite").Click 会引发运行时错误,因为您必须指定要单击由getElementsByClassName("nav-sprite") 形成的NodeCollection 的哪个元素的索引。
  • 您好。感谢您的关注。我想使用 ASIN 代码(产品的唯一 ID)进行搜索。如果我想搜索任何产品,我只会在 excel 单元格中添加 ASIN 代码,它会自动在相邻列中显示该产品的价格.如您所知,亚马逊的价格会逐渐变化和更新。因此,每当我想获得每日价格更新时,我只需单击刷新宏按钮。它会自动更新所有价格。我希望它不会令人困惑。如果你有兴趣解决我关于搜索按钮的真实查询,更欢迎你。谢谢
  • 这个问题在这里得到了两个有效的解决方案:stackoverflow.com/questions/64491907/…
  • 感谢分享知识!你能建议我应该用什么来代替“IE.Document.getElementsByClassName("nav-sprite").Click”来使搜索按钮自动点击。

标签: html excel vba


【解决方案1】:

要在 Excel 中使用 Web 抓取,您必须能够同时使用 VBA 和 HTML。此外 CSS 和至少一些 JS。最重要的是,您应该熟悉 DOM(文档对象模型)。仅使用 VBA 或仅使用 HTML,您不会走得太远。

当您可以简单地通过 URL 进行操作时,为什么要以复杂的方式进行操作,这对我来说是个谜。对于您的解决方案,您必须使用 nav-input 类。此类在 HTML 文档中存在两次。搜索按钮是第二次出现nav-input 的元素。由于 NodeCollection 的索引从 0 开始,因此您必须单击索引为 1 的元素。

Sub getdata()

Dim URL As String
Dim IE As Object

  URL = "https://www.amazon.co.uk"
  
  Set IE = CreateObject("InternetExplorer.Application")
  IE.Visible = True ' True to make IE visible, or False for IE to run in the background
  IE.Navigate URL 'Navigate to URL
  'making sure the page is done loading
  Do: DoEvents: Loop Until IE.ReadyState = 4
  
  'attempting to search date based on date value in cell
  IE.Document.getElementById("twotabsearchtextbox").Value = ActiveCell.Value
  
  'clicking the search button
  IE.Document.getElementsByClassName("nav-input")(1).Click
End Sub

编辑:使用已知 ASIN 打开报价的解决方案

如果您知道 ASIN,您可以直接在亚马逊网页上打开报价。要在 URL 中的活动单元格中使用 ASIN(这并不可靠。如果您必须按 Enter 完成输入,活动单元格是所需单元格下的那个),可以将其作为参数传递给Sub()getdata():

Private Sub worksheet_change(ByVal target As Range)
  If Not Intersect(target, Range("A1:A102")) Is Nothing Then
    Call getdata(ActiveCell.Value)
  End If
End Sub

然后在Sub()getdata() 中调用带有转移ASIN 的URL:

Sub getdata(searchTerm As String)

Dim URL As String
Dim IE As Object
  
  'Use the right base url
  URL = "https://www.amazon.co.uk/dp/" & searchTerm
  
  Set IE = CreateObject("InternetExplorer.Application")
  IE.Visible = True ' True to make IE visible, or False for IE to run in the background
  IE.Navigate URL 'Navigate to URL
  'making sure the page is done loading
  Do: DoEvents: Loop Until IE.ReadyState = 4
End Sub

也可以在工作表的worksheet_change 事件中完成所有操作(包括获取价格和报价标题):

Private Sub worksheet_change(ByVal target As Range)
  If Not Intersect(target, Range("A1:A102")) Is Nothing Then
    With CreateObject("InternetExplorer.Application")
      .Visible = True ' True to make IE visible, or False for IE to run in the background
      .Navigate "https://www.amazon.co.uk/dp/" & ActiveCell 'Navigate to URL
      'making sure the page is done loading
      Do: DoEvents: Loop Until .ReadyState = 4
      'Get Price
      ActiveCell.Offset(0, 1).Value = .document.getElementByID("priceblock_ourprice").innertext
      'Get offer title
      ActiveCell.Offset(0, 2).Value = .document.getElementByID("productTitle").innertext
    End With
  End If
End Sub

【讨论】:

  • 非常感谢!实际上“导航输入”是类名。但我没有使用它的第一个索引。我只是通过引用它来访问该类。你的解决方案奏效了。但是 URL 的方法是什么?如果我要在我的宏中添加手动产品的 URL。我必须更改每个产品的宏吗?因为每个产品都有不同的 URL。不是很花时间吗?现在我只需编写 ASIN 代码,它就会从站点获取数据并将其粘贴到相邻的列中。
  • @AlexG 我添加了两个解决方案,直接在亚马逊网页上打开 ASIN。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2019-05-30
  • 2021-12-18
相关资源
最近更新 更多