【问题标题】:Scraping price of retailers website using excel使用excel刮掉零售商网站的价格
【发布时间】:2018-02-15 11:08:33
【问题描述】:

我创建了一个宏来使用 TagName 抓取零售商网站的价格,这确实有效,但是一旦 TagName 移动位置,我就必须重新定位并更改位置编号。

我目前正在使用 IF 函数在目标价格方面对冲我的赌注。理想情况下,我需要使用 Class 或 ID 来恢复价格,但我多次尝试都失败了。

网站: https://www.usc.co.uk/puma-roma-basic-perforated-trainers-023210?colcode=02321069

样式 023210/69(前 6 位 = sName)(后 2 位 = SCOL)

任何帮助将不胜感激!以下是我当前的代码

Sub browse()

 Do Until IsEmpty(ActiveCell)
Dim sPath As String, sPathEnd As String, sName As String, SCOL As String

 sName = ActiveCell.Value
 SCOL = ActiveCell.Offset(0, 1)

Dim IE As New SHDocVw.InternetExplorer


IE.Visible = False
IE.navigate "https://www.usc.co.uk/" & sName & "?colcode=" & SCOL & ""

Do While IE.ReadyState <> READYSTATE_COMPLETE

Loop

''Debug.Print IE.LocationName, IE.LocationURL
Dim doc As HTMLDocument
Set doc = IE.Document
Dim SellingPrice As String
Dim OriginalPrice As String
Dim SellingPrice2 As String
Dim OriginalPrice2 As String

SellingPrice = Trim(doc.getElementsByTagName("SPAN")(154).innerText)
OriginalPrice = Trim(doc.getElementsByTagName("SPAN")(155).innerText)
SellingPrice2 = Trim(doc.getElementsByTagName("SPAN")(111).innerText)
OriginalPrice2 = Trim(doc.getElementsByTagName("SPAN")(112).innerText)


If SellingPrice = "Add to bag" Then


  ActiveCell.Offset(0, 2).Select
  ActiveCell.Value = SellingPrice2
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = OriginalPrice2
  ActiveCell.Offset(1, -3).Select

Else


  ActiveCell.Offset(0, 2).Select
  ActiveCell.Value = SellingPrice
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = OriginalPrice
  ActiveCell.Offset(1, -3).Select
End If

Loop


 Cells.Select
    Cells.EntireColumn.AutoFit

End Sub

【问题讨论】:

  • 如果我考虑您粘贴在顶部的 URL,您的预期输出是什么?
  • 您应该循环所有元素以确保获得正确的元素。检查HtmlElement.GetAttribute。例如,如果class=productHasRef,您可以循环所有跨度并确保获得正确的跨度,或者可以按 ID 选择 div,然后获取 Innertext。

标签: vba excel web-scraping screen-scraping


【解决方案1】:

我相信以下内容应该适合您,而不是使用跨度,而是使用 ID 作为 Selling Price 和 Original Price:

Sub browse()

Do Until IsEmpty(ActiveCell)
    Dim sPath As String, sPathEnd As String, sName As String, SCOL As String

     sName = ActiveCell.Value
     SCOL = ActiveCell.Offset(0, 1)

    Dim IE As New SHDocVw.InternetExplorer

    IE.Visible = False
    IE.navigate "https://www.usc.co.uk/" & sName & "?colcode=" & SCOL & ""

    Do While IE.ReadyState <> READYSTATE_COMPLETE

    Loop

    Set doc = IE.Document
    Dim SellingPrice As String
    Dim OriginalPrice As String
    Dim SellingPrice2 As String
    Dim OriginalPrice2 As String


    SellingPrice = doc.getElementbyID("dnn_ctr103436_ViewTemplate_ctl00_ctl07_lblSellingPrice").innerText
    OriginalPrice = doc.getElementbyID("dnn_ctr103436_ViewTemplate_ctl00_ctl26_lblTicketPrice").innerText

    If SellingPrice = "Add to bag" Then
      ActiveCell.Offset(0, 2).Select
      ActiveCell.Value = SellingPrice2
      ActiveCell.Offset(0, 1).Select
      ActiveCell.Value = OriginalPrice2
      ActiveCell.Offset(1, -3).Select
    Else
      ActiveCell.Offset(0, 2).Select
      ActiveCell.Value = SellingPrice
      ActiveCell.Offset(0, 1).Select
      ActiveCell.Value = OriginalPrice
      ActiveCell.Offset(1, -3).Select
    End If

Loop
 Cells.Select
    Cells.EntireColumn.AutoFit
End Sub

更新:

另外,在这个例子中,我没有使用 ActiveCell,而是让代码从 A1 向下循环,直到找到一个没有任何数据的单元:

Sub browse()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required

Count = 1
Do Until IsEmpty(ws.Range("A" & Count))
    Dim sPath As String, sPathEnd As String, sName As String, SCOL As String

    sName = ws.Range("A" & Count)
    SCOL = ws.Range("B" & Count)

    Dim IE As New SHDocVw.InternetExplorer

    IE.Visible = False
    IE.navigate "https://www.usc.co.uk/" & sName & "?colcode=" & SCOL & ""

    Do While IE.ReadyState <> READYSTATE_COMPLETE

    Loop

    Set doc = IE.Document
    Dim SellingPrice As String
    Dim OriginalPrice As String

    SellingPrice = doc.getElementbyID("dnn_ctr103436_ViewTemplate_ctl00_ctl07_lblSellingPrice").innerText
    OriginalPrice = doc.getElementbyID("dnn_ctr103436_ViewTemplate_ctl00_ctl26_lblTicketPrice").innerText

    ws.Range("C" & Count).Value = SellingPrice
    ws.Range("D" & Count).Value = OriginalPrice
    Count = Count + 1

Loop
ws.Cells.Select
ws.Cells.EntireColumn.AutoFit
End Sub

【讨论】:

  • 两者都能完美运行!非常感谢,这让我绞尽脑汁……将同时使用这两种方法!
  • @DannyBrown 如果有帮助,您能否将我的回复标记为答案?谢谢。
猜你喜欢
  • 2012-05-01
  • 2015-09-02
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2017-03-17
相关资源
最近更新 更多