2016-02-26 61 views
1

我一直在試圖讓這個代碼工作在工作流程效率的目的,但我似乎無法使其正常工作。Excel亞馬遜賣家Web Scraper問題

步驟: 1.登錄到亞馬遜賣方

  • 使用順序在列A中的數字,並將它們置於搜索框,以搜尋
  • 查找「預計交貨的元件的innerText :「並將信息刮到與訂單號相鄰的列B中
  • 移到下一個訂單號並重復處理,直到訂單號列爲空。
  • 網頁代碼(我想要獲得被高亮顯示):

    enter image description here

    Option Explicit 
    
    Dim HTMLDoc As HTMLDocument 
    Dim MyBrowser As InternetExplorer 
    
    Sub MyAmazonSellereEDD() 
    
        Dim MyHTML_Element As IHTMLElement 
        Dim MyURL As String 
        Dim oSignInLink As HTMLLinkElement 
        Dim oInputEmail As HTMLInputElement 
        Dim oInputPassword As HTMLInputElement 
        Dim oInputSignInButton As HTMLInputButtonElement 
    
        'InputSearchOrder will be the destination for order numbers taken from the workbook 
        Dim InputSearchOrder As HTMLInputElement 
    
        Dim InputSearchButton As HTMLInputButtonElement 
        Dim IE As InternetExplorer 
        Dim AAOrder As Workbook 
        Dim AAws As Worksheet 
        Dim AAws2 As Worksheet 
    
        Dim R As Range 
        Dim x As Integer 
        Dim i As Long 
        Dim ar As Variant 
        Dim elems As IHTMLElementCollection 
        Dim TDelement As HTMLTableCell 
    
        Dim ExcludWords() As Variant, a As Range, b As Long, LR As Long 
    
        ExcludWords = Array("Estimated Delivery:") 
    
    
        MyURL = "https://sellercentral.amazon.com/gp/homepage.html" 
    
        Set IE = New InternetExplorer 
    
        ' Open the browser and navigate. 
        With IE 
         .Silent = True 
         .navigate MyURL 
         .Visible = True 
         Do 
          DoEvents 
         Loop Until .readyState = READYSTATE_COMPLETE 
        End With 
    
    
        ' Get the html document. 
        Set HTMLDoc = IE.document 
    
    
    
        With HTMLDoc 
         .all.Item("username").Value = "[email protected]" 
         .all.Item("password").Value = "*********" 
         .all.Item("sign-in-button").Click 
        End With 
    
         Do 
         DoEvents 
        Loop Until IE.readyState = READYSTATE_COMPLETE 
    
        Application.Wait (Now + TimeValue("0:00:08")) 
    
        'Set AAOrder = Application.Workbooks.Open("Z:\Automation Anywhere\5 Automated Tracking Imports\Amazon Prime\PrimeOrdersWithNoFulfillment.csv") 
        'Set AAws = AAOrder.Worksheets("PrimeOrdersWithNoFulfillment") 
    
    
    
    
    
        x = 2 
        'Do Until Range("A" & x) = "" 
        If Range("B" & x).Value = "" Then 
    
         'If AAws.Range("B" & x).Value = "" Then 
         'x = x + 1 
         Do Until Range("A" & x) = "" 
          Set InputSearchOrder = HTMLDoc.getElementById("sc-search-field") 
          InputSearchOrder.Value = Range("A" & x) 
    
          Set InputSearchButton = HTMLDoc.getElementsByClassName("sc-search-button")(0) 
          InputSearchButton.Click 
           Do 
           DoEvents 
          Loop Until IE.readyState = READYSTATE_COMPLETE 
          Application.Wait (Now + TimeValue("0:00:05")) 
    
    
          Set elems = HTMLDoc.getElementsByTagName("td") 
    
    
          'ExcludWords = Array("Package Weight:", "Tracking ID:", "Ship Date:", "Carrier:", "Shipping Service:") 
    
    
          i = 2 
    
          For Each TDelement In elems 
           If TDelement.className = "data-display-field" And InStr(TDelement.innerText, "Estimated Delivery:") Then 
            Range("B" & x).Value = TDelement.innerText 
            i = i + 1 
           End If 
          Next 
    
    
    
    
    
    
    
          LR = Range("B" & Rows.Count).End(xlUp).Row 
          For i = 1 To LR 
          Set a = Cells(i, "B") 
          For b = 0 To UBound(ExcludWords) 
           a.Formula = Replace((a.Formula), ExcludWords(b), "") 
            Next b 
          Next i 
    
    
         'End If 
         x = x + 1 
         Loop 
    
    
        'Loop 
        End If 
    
        Err_Clear: 
         If Err <> 0 Then 
          Err.Clear 
          Resume Next 
         End If 
    
        MsgBox ("Process is done! :)") 
    
    
        End Sub 
    

    我的問題是,當刮擦數據中,「預計交貨:」並且實際估計的交付日期應該是分開的,但仍然應該包含在B列的輸出數據中。它所做的只是查找並插入「Estimated Delivery:」,然後使用代碼修剪這些字符按照指示。此後空間依然空白。我不確定問題是什麼。

    回答

    2

    TDelement您在下面的代碼部份僅包括撿「預計交貨時間:」在其innerText,隨着日期的部分實際上是一個獨立的TDelement

    For Each TDelement In elems 
        If TDelement.className = "data-display-field" And InStr(TDelement.innerText, "Estimated Delivery:") Then 
         Range("B" & x).Value = TDelement.innerText 
         i = i + 1 
        End If 
    Next 
    

    ,因爲沒有任何獨特用於引用TDelement的html代碼中的信息(例如id,name等),其中包含您可以使用已有的引用的日期與NextSibling一起使用,以便在包含文本「Estimated交貨:」。也許嘗試這個(目前無法測試任何東西,但應該工作):

    For Each TDelement In elems 
        If TDelement.className = "data-display-field" And InStr(TDelement.innerText, "Estimated Delivery:") Then 
         Range("B" & x).value = TDelement.NextSibling.innerText 
         i = i + 1 
        End If 
    Next 
    
    +0

    我試過了,它拋出「VBA對象不支持此屬性或方法」。我意識到它特別不能引用元素,並且我一直在嘗試諸如你的解決方法。我需要更新.NextSibling.innerText工作的參考庫嗎? – Tak

    +0

    所以我搞亂了代碼,'Range(「B」&x).value = TDelement.Children(0).innerText'是我得到交付日期信息最近的,但是提供了一個全新的一組問題,因爲它向單元格添加了更多的數據,並且在第一個單元格填充了額外的數據之後,宏引發了一個對象錯誤。 – Tak