【发布时间】:2019-06-02 12:33:32
【问题描述】:
我正在尝试根据亚马逊的唯一产品代码制作产品列表。
例如:https://www.amazon.in/gp/product/B00F2GPN36
其中 B00F2GPN36 是唯一代码。
我想将产品的图片和标题提取到产品图片和产品名称列下的 Excel 列表中。
我已经尝试过html.getElementsById("productTitle") 和html.getElementsByTagName。
我也对描述哪种变量来存储上述信息存有疑问,因为我尝试过声明Object 类型和HtmlHtmlElement。
我尝试提取 html 文档并将其用于数据搜索。
代码:
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub parsehtml()
Dim ie As InternetExplorer
Dim topics As Object
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "https://www.amazon.in/gp/product/B00F2GPN36"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Amazon.in...."
DoEvents
Loop
Application.StatusBar = ""
Set html = ie.document
Set topics = html.getElementsById("productTitle")
Sheets(1).Cells(1, 1).Value = topics.innerText
Set ie = Nothing
End Sub
我希望输出是单元格 A1 中的输出:
“Milton Thermosteel Carafe Flask, 2 litres, Silver”应该会反射(不带引号),同样我也想拉出图像。
但总是有一些错误,例如:
1.运行时错误'13':
当我使用“将主题设为 HTMLHtmlElement”时,类型不匹配
2.运行时错误'438':
对象不支持该属性或方法
注意:我添加了来自 Tools > References 的引用,即所需的库。
【问题讨论】:
标签: html excel vba web-scraping simple-html-dom