2014-10-26 113 views
1

所以我一直想寫的東西VBA從任何亞馬遜頁面獲取所有賣家名&價格,這裏是一個產品列表例如:亞馬遜賣家獲取與VBA

http://www.amazon.com/gp/offer-listing/B00KTOE41I/ref=dp_olp_all_mbc?ie=UTF8&condition=all

我試着VBA中的每個選項都會返回包含我想要的數據的某些標記的innertext/innerhtml,但無論使用什麼方法都會失敗。

getElementsByClassName方法 - 錯誤, getElementsByName - 錯誤, 的getElementById - 錯誤 的getElementsByTagName - 帶回了類似 「[對象HTMLSpanElement]」

賣家ID是包含在此標記中: P類=「一個間距 - 小olpSellerName」,它包含在此標籤內 &價格:

這裏是我的代碼,我知道它需要一個for循環在那裏遍歷指定標籤的所有實例,但我甚至無法讓它返回內聯文本。我試着將變量定爲「IHTMLElement」或「IHTMLElementCollection」,但沒有任何效果。

此外,我有微軟HTML對象庫&互聯網控制檢查。

Option Explicit 
Sub RunNewModule() 

Dim ie As InternetExplorer 
Dim html As HTMLDocument 

Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = False 
ie.Navigate "http://rads.stackoverflow.com/amzn/click/B00KTOE41I" 

Do While ie.ReadyState <> READYSTATE_COMPLETE 
DoEvents 
Loop 

Set html = ie.Document 
Set ie = Nothing 
Dim table 
Dim sellers 

UserForm1.TextBox2.Text = html.DocumentElement.innerHTML 
Set table = html.getElementsByTagName("a").Item 

sellers = table.all 
UserForm1.TextBox7.Text = sellers 

Cells.Clear 
Range("A3").Value = "Seller" 
Range("B3").Value = "Price" 
Range("A4").Value = sellers 

End Sub 
+0

在這裏找不到任何這樣的標籤:view-source:http://www.amazon.com/dp/B00KTOE41I/?tag = stackoverfl08-20 – 2014-10-26 16:16:46

+0

你是對的我更新了ie.navigate url。標籤應該在那裏。 – Zebra 2014-10-26 16:48:21

回答

1

使用這個網址:http://www.amazon.com/gp/offer-listing/B00KTOE41I/ref=dp_olp_all_mbc?ie=UTF8&condition=all

你可以抓住的價格是這樣的:Set priceData = html.getElementsByClassName("olpOfferPrice")

B列這樣的後輸出:

cntr = 4 
For Each Item In priceData 
    Range("B" & cntr) = Item.innerText 
    cntr = cntr + 1 
Next Item 

賣方同樣的東西:Set sellerData = html.getElementsByClassName("olpSellerName")

除了這將只返回名稱在文本中的賣家而不是圖像。


結果:

enter image description here


請務必關閉IE瀏覽器(ie.Quit),否則你保持在內存中。

+0

該代碼有效!非常感謝! – Zebra 2014-10-26 18:38:57