代碼:
def page_function():
driver.get('https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs+/?N=4176697776&No=12&Nrpp=12')
driver.maximize_window()
# gets the amount of items in the search bar
print("Number of products:", 69)
# for loop to read the product name and descriptions
# product = driver.find_element(By.CSS_SELECTOR, ".search-item")
# product = product.text
# print(product)
pr = "]/product-card//article/div/div[2]/div[1]/pes-product-price/p/span[1]"
nam = "]/product-card//article/div/div[1]/product-card-main-info//div/pes-router-link[1]/a"
des = ') > product-card").shadowRoot.querySelector("product-card-main-info.hydrated").shadowRoot.querySelector("pes-router-link.description.hydrated a > h3")'
# des_path = "#search-items > .search-item .details > a > .row.pt-5.pb-sm-5 > .multilines-3.text-truncate-multilines.xs-single-col-8.col-12 > .font-weight-bold.text-dark"
follow_loop = range(1, 70)
for x in follow_loop:
y = x
if (x > 61):
y = x - 60
elif (x > 49):
y = x - 48
elif (x > 37):
y = x - 36
elif (x > 25):
y = x - 24
elif(x > 13):
y = x - 12
else:
print("")
if ( ((x % 13) == 0) ):
driver.delete_all_cookies()
next_arrow = driver.find_element(By.CLASS_NAME, "page-links__arrow page-links__arrow--next js-page-link js-page-link-next")
driver.execute_script("arguments[0].click();", next_arrow)
xpath = "return document.querySelector("product-cards-wrapper.hydrated").shadowRoot.querySelector("div > ul > li:nth-child(str(y)) > product-card )
xpath = str(y)
xpath = des
xpath = "'"
driver.implicitly_wait(10)
description.append(driver.execute_script(xpath))
print(description[x].text)
input("continue??")
xpath2 = xpath.replace(des, '')
xpath2 = pr
unit_price.append(driver.find_element(By.XPATH, xpath2).text)
xpath3 = xpath2.replace(pr, '')
xpath3 = nam
name.append(driver.find_element(By.XPATH, xpath3).text)
錯誤:
File "C:\Users\....\Documents\web_scrap\main.py", line 65, in page_function
xpath = 'return document.querySelector("product-cards-wrapper.hydrated").shadowRoot.querySelector("div > ul > li:nth-child('
UnboundLocalError: local variable 'xpath' referenced before assignment
網頁: 網站
問題:
目前,在stackoverflow中借助他人的幫助,能夠從shadow root中識別出他們產品網站上的描述、價格和產品名稱。因此,我需要使用 execute_script() 命令才能作業。此外,我能夠看到每個元素都在不同的差異類中,這正是我在這段代碼中嘗試做的。但是,這似乎不起作用。有任何想法嗎??
謝謝
uj5u.com熱心網友回復:
xpath = "return document ...
是相同的
xpath = xpath "return document
但是您還沒有定義它,這就是您收到錯誤的原因'xpath' referenced before assignment,您可能只想擁有:
xpath = "return document.querySelector('product-cards-wrapper.hydrated').shadowRoot.querySelector('div > ul > li:nth-child(str(y)) > product-card')"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374264.html
標籤:javascript Python for循环 硒网络驱动程序 自动化
下一篇:如何使用R字串Python回圈
