html - 我们可以通过在 vba 中使用 url 来获取特定数据吗
问题描述
我有 15 个不同的 URL,我需要从 Excel 中的特定网站的特定列中获取价格,请您帮帮我。这是我的第一个 VBA 程序,我尝试过,但它显示了我的语法错误。
Sub myfile()
Dim IE As New InternetExplorer Dim url As String Dim item As
HTMLHtmlElement Dim Doc As HTMLDocument Dim tagElements As Object
Dim element As Object Dim lastRow Application.ScreenUpdating =
False Application.DisplayAlerts = False Application.EnableEvents =
False Application.Calculation = xlCalculationManual url =
"https://wtb.app.channeliq.com/buyonline/D_nhoFMJcUal_LOXlInI_g/TOA-60?html=true"
IE.navigate url IE.Visible = True Do DoEvents Loop Until
IE.readyState = READYSTATE_COMPLETE
Set Doc = IE.document
lastRow = Sheet1.UsedRange.Rows.Count + 1 Set tagElements =
Doc.all.tags("tr") For Each element In tagElements
If InStr(element.innerText, "ciq-price")> 0 And
InStr(element.className, "ciq-product-name") > 0 Then
Sheet1.Cells(lastRow, 1).Value = element.innerText
' Exit the for loop once you get the temperature to avoid unnecessary processing
Exit For End If Next
IE.Quit Set IE = Nothing Application.ScreenUpdating = True
Application.DisplayAlerts = True Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
解决方案
您不能出于您的目的复制任何网络抓取宏。每个页面都有自己的 HTML 代码结构。因此,您必须为每个页面编写一个自己的网络抓取宏。
我无法在这里解释有关使用 VBA 进行网络抓取的所有内容。请使用“excel vba web scraping”和“document object model”开始您的recherche信息。此外,您还需要有关 HTML 和 CSS 的知识。最好的情况也是关于 JavaScript:
错误消息user-defined type not defined ocours 因为您使用早期绑定而不引用库Microsoft HTML Object Library和Microsoft Internet Controls。您可以在此处阅读如何通过工具 -> 参考...设置参考以及早期绑定和后期绑定之间的区别Early Binding v/s Late Binding以及 Microsoft在自动化中使用早期绑定和后期绑定的更深入信息
要从显示的 url 获取价格,您可以使用以下宏。我使用后期绑定:
Option Explicit
Sub myfile()
Dim IE As Object
Dim url As String
Dim tagElements As Object
Dim element As Object
Dim item As Object
Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.Rows.Count + 1
url = "https://wtb.app.channeliq.com/buyonline/D_nhoFMJcUal_LOXlInI_g/TOA-60?html=true"
Set IE = CreateObject("internetexplorer.application")
IE.navigate url
IE.Visible = True
Do: DoEvents: Loop Until IE.readyState = 4
Set tagElements = IE.document.getElementsByClassName("ciq-online-offer-item ")
For Each element In tagElements
Set item = element.getElementsByTagName("td")(1)
ActiveSheet.Cells(lastRow, 1).Value = Trim(item.innerText)
lastRow = lastRow + 1
Next
IE.Quit
Set IE = Nothing
End Sub
编辑第二个示例: 新链接指向报价。我假设要获取产品的价格。为此不需要循环。您只需要找出价格在哪个 HTML 段中,然后您就可以决定如何获取它。最后只有两行 VBA 将价格写入 Excel 电子表格。
我在德国,Excel 已自动将货币符号从美元设置为欧元。这当然是错误的。根据您所在的位置,这可能必须被拦截。
Sub myfile2()
Dim IE As Object
Dim url As String
Dim tagElements As Object
Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.Rows.Count + 1
url = "https://www.wayfair.com/kitchen-tabletop/pdx/cuisinart-air-fryer-toaster-oven-cui3490.html"
Set IE = CreateObject("internetexplorer.application")
IE.navigate url
IE.Visible = True
Do: DoEvents: Loop Until IE.readyState = 4
'Break for 3 seconds
Application.Wait (Now + TimeSerial(0, 0, 3))
Set tagElements = IE.document.getElementsByClassName("BasePriceBlock BasePriceBlock--highlight")(0)
ActiveSheet.Cells(lastRow, 1).Value = Trim(tagElements.innerText)
IE.Quit
Set IE = Nothing
End Sub
推荐阅读
- javascript - 使用带有数据表的组合框进行过滤
- facebook - 当我邀请某人时,如何从 facebook api 获取 requestId
- html - 如何删除页脚下方的空白?
- sql-server - 从 Azure BLOB 读取 SQL Server 数据文件数据的性能
- linux - 在 amazon linux 上编译 suricata 时出错
- ansible - 在ansible中包含另一个剧本时出现问题?
- asynchronous - 在 Tokio 中的异步请求之间共享数据
- go - 如何使用 Youtube Go API 获取视频信息?
- ionic-framework - 如何在调用 api 将图像上传到服务器 for mobilefirst 8 时修复错误 414 uri too long
- r - 将数字列转换为字符