无法使用单个html容器对shopee进行定价

2024-09-28 17:05:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法使用类名或XPATH从特定容器中提取商品的价格。我试着拉不同的html块,但就是不起作用。价格格式应该是这样的₱38-₱120。这是网站https://shopee.ph/search?keyword=a4%20notebook

#all containers
container = driver.find_elements_by_xpath('//*[@class="_1gkBDw _2O43P5"]') #list
time.sleep(10)

#product 
item_list = []
price_list = []
for item in container: #individual container
     product = item.find_element_by_class_name('O6wiAW') #'//*[@class="O6wiAW"]' #specific container 
     item_list +=[product.text]
     price = item.find_element_by_class_name('_1w9jLI _37ge-4 _2ZYSiu') #MY PROBLEM
     price_list += [price.text]
     
print(price_list)
print(len(price_list))

print(item_list)
print(len(item_list))



exit(1)


Tags: textnamebylencontainer价格elementfind
1条回答
网友
1楼 · 发布于 2024-09-28 17:05:19

要打印第一项的价格,即₱38-₱120,您需要为visibility_of_element_located()引入WebDriverWait,并且您可以使用以下任一Locator Strategies

  • 使用XPATH

    driver.get('https://shopee.ph/search?keyword=a4%20notebook')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row shopee-search-item-result__items']/div[@data-sqe='item']//span[text()='₱']//.."))).text)
    
  • 控制台输出:

    ₱38 - ₱120
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

相关问题 更多 >