2015-10-08 43 views
1

對於典型的eBay搜索結果頁面,如this,我使用的webdriver從而提取每個結果的價格:使用WebDriver在eBay搜索結果中區分商品和相關商品?

PRICEELEMENT = 'ul:nth-child(3) > li:nth-child(1) > span:nth-child(1)' 
prices = [float(price.text.replace('USD','')) for price in driver.find_elements_by_css_selector(PRICEELEMENT)] 

這是運作良好。它抓住了實際列表和「更多相關項目」的價格。

現在我想要做的事情,在像上面的鏈接那裏只有3個結果,其餘都是「相關」的情況下,只提取實際列表的價格。特別是當有1到5(含)實際列表中的時,只提取這些。

我沒有看到除了文本「更多項目與美津濃褲子腰帶填充」之外,頁面正在區分搜索結果。無論它們是實際列表還是相關項目,都具有相同的CSS選擇器(ul:nth-child(3) > li:nth-child(1) > span:nth-child(1))和類名稱(bold bidsold)。

如果必須,我可以先取X列表的數量,然後只考慮prices中的第一個X價格。但有沒有辦法使用頁面結構本身來實現這一目標?

回答

2

簡單一點 - 作爲一個人,你會如何定義哪些列表是實際的搜索結果,哪些是「相關的」? - 我想通過中間的「更多與......相關的項目」標籤。讓我們使用,隨着preceding-siblingfollowing-sibling符號的幫助:

search_results = driver.find_elements_by_xpath("//li[.//*[contains(., 'More items related to')]]/preceding-sibling::li[@listingid]") 

related_results = driver.find_elements_by_xpath("//li[.//*[contains(., 'More items related to')]]/following-sibling::li[@listingid]") 
+0

有一種感覺,可能需要一些先進的XPath。謝謝。 WebDriver不喜歡錶達式:http://pastebin.com/M1jLFM0Q – Pyderman