如何从亚马逊获取产品的图片和标题?

2024-01-06

我正在尝试根据亚马逊的独特产品代码制作一个产品列表。

例如:https://www.amazon.in/gp/product/B00F2GPN36 https://www.amazon.in/gp/product/B00F2GPN36

其中 B00F2GPN36 是唯一代码。

我想将产品的图像和标题提取到产品图像和产品名称列下的 Excel 列表中。

我努力了html.getElementsById("productTitle") and html.getElementsByTagName.

我也怀疑要描述什么样的变量来存储上述信息,因为我尝试过声明Object类型和HtmlHtmlElement.

我尝试提取 html 文档并将其用于数据搜索。

Code:

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 升,银色”应该反映出来(不带引号),同样我也想提取图像。

但总是会出现一些错误,例如:
1.运行时错误'13':
当我使用“Dim topic As HTMLHtmlElement”时,类型不匹配
2.运行时错误'438':
对象不支持此属性或方法

注意:我添加了来自工具 > 参考即所需的库。


不存在这样的事情html.getElementsById("productTitle")在 vba 中。 ID 始终是唯一的,所以应该是html.getElementById("productTitle")。运行以下脚本来获取它们:

Sub ParseHtml()
    Dim IE As New InternetExplorer, elem As Object
    Dim Html As HTMLDocument, imgs As Object

    With IE
        .Visible = False
        .navigate "https://www.amazon.in/gp/product/B00F2GPN36"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Set elem = Html.getElementById("productTitle")
    Set imgs = Html.getElementById("landingImage")

    Sheets(1).Cells(1, 1) = elem.innerText
    Sheets(1).Cells(1, 1).Offset(0, 1) = imgs.getAttribute("data-old-hires")
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从亚马逊获取产品的图片和标题? 的相关文章

随机推荐