【问题标题】:How to pull the image and title of the product from Amazon?如何从亚马逊拉取产品的图片和标题?
【发布时间】: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


    【解决方案1】:

    vba 中没有html.getElementsById("productTitle") 这样的东西。 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
    

    【讨论】:

    • 看起来 OP 犯了一个简单的错字:他写的是“elements”而不是“element”。有眼光!
    • 不能相信只是一个's'。谢谢你,你是一个救生员。
    • 顺便说一句,我们可以将图像的代码更改为实际图像吗?
    • 当然。查看编辑。我认为你的意思是得到最大的。
    • 是的,很抱歉要求如此苛刻,但是有没有办法通过代码下载图像并调整其大小以根据单元格进行格式化?您知道我们如何将图像附加到单元格上。
    【解决方案2】:

    更快的是使用xhr 并避免浏览器并将结果从数组写入工作表

    Option Explicit
    Public Sub GetInfo()
        Dim html As HTMLDocument, results()
        Set html = New HTMLDocument
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "https://www.amazon.in/gp/product/B00F2GPN36", False
            .send
            html.body.innerHTML = .responseText
            With html
                results = Array(.querySelector("#productTitle").innerText, .querySelector("#landingImage").getAttribute("data-old-hires"))
            End With
        End With
        With ThisWorkbook.Worksheets("Sheet1")
            .Cells(1, 1) = results(0)
            Dim file As String
            file = DownloadFile("C:\Users\User\Desktop\", results(1))  'your path to download file
            With .Pictures.Insert(file)
                .Left = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Left
                .Top = ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Top
                .Width = 75
                .Height = 100
                .Placement = 1
            End With
        End With
        Kill file
    End Sub 
    

    【讨论】:

    • 好人,是的,至少在我的笔记本电脑上,它确实更快。非常感谢。
    • 虽然现在我正在检查它似乎比上一个复杂。 :-|)
    • 是的,很抱歉要求如此苛刻,但是有没有办法通过代码下载图像并调整其大小以根据单元格进行格式化?您知道我们如何将图像附加到单元格上。
    • 这并不难,因为您只是使用 querySelector 通过它们的 id 返回两个元素并存储在一个数组中。然后你可以一口气写出数组。使用数组和 xmlhttp 更快,并且减少了写入工作表的开销。
    • 是的,您可以将图像写入单元格。我看看能不能找到你的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2020-04-11
    • 2021-01-10
    • 2018-06-04
    • 2011-09-08
    相关资源
    最近更新 更多