假設我有一個 eccormece 網站,我想抓取它,我對十大熱門產品感興趣,當深入研究 html 元素時,它是這樣的:
<div>
<div>
<span>
<a href='www.mysite/products/1'>
Product 1
</a>
</spa>
</div>
<div>
<span>
<a href='www.mysite/products/2'>
Product 2
</a>
</spa>
</div>
<div>
<span>
<a href='www.mysite/products/3'>
Product 3
</a>
</spa>
</div>
<div>
<span>
<a href='www.mysite/products/4'>
Product 4
</a>
</spa>
</div>
</div>
我的第一個解決方案是提取 href 屬性,然后將它們存盤在一個串列中,然后我會為每個屬性打開瀏覽器實體,但是這是有代價的,因為我必須關閉并打開瀏覽器,每次打開它我必須進行身份驗證。然后我嘗試了解決方案 2。在我的解決方案二中,外部 div 是父級,按照 selenium 的做事方式,這意味著我存盤的產品如下:
product_1 = driver.find_element_by_xpath("//div/div[1]")
product_2 = driver.find_element_by_xpath("//div/div[2]")
product_3 = driver.find_element_by_xpath("//div/div[3]")
product_4 = driver.find_element_by_xpath("//div/div[4]")
所以我的目標是搜索一個產品,在獲取串列目標框的標簽后點擊它,去提取產品的更多詳細資訊,然后回傳而不關閉瀏覽器,直到我的串列完成,下面是我的解決方案:
for i in range(10):
try:
num = i 1
path = f"//div/div[{num}]/span/a"
poduct_click = driver.find_element_by_xpath(path)
driver.execute_script("arguments[0].click();", poduct_click)
scrape_product_detail() #function that scrapes the whole product detail
driver.execute_script("window.history.go(-1)") # goes backwards to continue looping
except NoSuchElementException:
print('Element not found')
問題是它適用于第一個產品,它會刮掉所有細節,然后再回傳。盡管回傳到產品頁面,程式未能找到第二個元素和之后的元素,我無法理解可能是什么問題。愿你幫助。謝謝
uj5u.com熱心網友回復:
謝謝@Debenjan 你在那里幫了我很多。您的解決方案就像一個魅力。對于那些想知道我在這里是如何進行的人,以下代碼:
article_elements = self.find_elements_by_class_name("s-card-image")
collection = []
for news_box in article_elements:
# Pulling the hotel name
slug = news_box.find_element_by_tag_name(
'a'
).get_attribute('href')
collection.append(
slug
)
for i in range(len(collection)):
self.execute_script("window.open()")
self.switch_to.window(self.window_handles[i 1])
url = collection[i]
self.get(url)
print(self.title, url, self.current_url)
@AD 非常感謝您的解決方案也有效,我只需要測驗并查看最佳策略并采用它。非常感謝各位
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362327.html
