为什么代码不显示产品总数?


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By

path = r'C:Users.wdmdriverschromedriverwin32108.0.5359chromedriver.exe'
# open the browser
# browser = webdriver.Chrome(executable_path=path)
s=Service(r'C:Users.wdmdriverschromedriverwin32108.0.5359chromedriver.exe')
browser = webdriver.Chrome(service=s)
# load the webpage
browser.get('https://www.amazon.in')
browser.maximize_window()
# get the input elements
input_search = browser.find_element(By.ID,'twotabsearchtextbox')
search_button = browser.find_element(By.XPATH,"(//input[@type='submit'])[1]")
# send the input to the webpage
input_search.send_keys("Smartphones under 10000")
sleep(1)
search_button.click()
products = []
for i in range(10):
print('Scraping page', i+1)
product = browser.find_elements(By.CLASS_NAME,'a-size-medium a-color-base a-text-normal')
for p in product:
products.append(p.text)
next_button = browser.find_element(By.CLASS_NAME,'a-last')
next_button.click()
sleep(3)
print(len(products))
products[:5]
brower.quit()

上面是从页面中抓取数据,并显示总数和产品名称的代码。但是这段代码并没有显示抓取的数据,这可能是什么问题呢?

输出:

Scraping page 1
Scraping page 2
Scraping page 3
Scraping page 4
Scraping page 5
Scraping page 6
Scraping page 7
Scraping page 8
Scraping page 9
Scraping page 10
0
Process finished with exit code 0

就是这样,不算数。

你必须修改for循环中的定位符:

# send the input to the webpage
input_search.send_keys("Smartphones under 10000")
search_button.click()
sleep(1)
products = []
for i in range(10):
# print('Scraping page', i+1)
product = browser.find_elements(By.CSS_SELECTOR,'.a-size-medium.a-color-base.a-text-normal')
for p in product:
products.append(p.text)
next_button = browser.find_element(By.CSS_SELECTOR,'.s-pagination-item.s-pagination-next')
next_button.click()
sleep(3)
print("Total products:", len(products))
for i in range(len(products)):
print(products[i])

输出:

Total products: 164
Lava Blaze NXT(Glass Red, 4GB RAM, 64GB Storage)| 2.3 Ghz Octa Core Helio G37| 13 MP AI Triple Camera |Fingerprint Sensor| 5000 mAh Battery| Upto 7GB Expandable RAM
Redmi 9 Activ (Carbon Black, 4GB RAM, 64GB Storage) | Octa-core Helio G35 | 5000 mAh Battery
Redmi A1 (Light Blue, 2GB RAM, 32GB Storage) | Segment Best AI Dual Cam | 5000mAh Battery | Leather Texture Design | Android 12
realme narzo 50i (Carbon Black, 2GB RAM+32GB Storage) Octa Core Processor | 6.5" inch Large Display
Redmi 9A Sport (Coral Green, 2GB RAM, 32GB Storage) | 2GHz Octa-core Helio G25 Processor | 5000 mAh Battery
Redmi 10A (Slate Grey, 4GB RAM, 64GB Storage) | 2 Ghz Octa Core Helio G25 | 5000 mAh Battery | Finger Print Sensor | Upto 5GB RAM with RAM Booster
Redmi 9A (Nature Green, 2GB RAM, 32GB Storage) | 2GHz Octa-core Helio G25 Processor | 5000 mAh Battery
Redmi A1 (Black, 2GB RAM, 32GB Storage) | Segment Best AI Dual Cam | 5000mAh Battery | Leather Texture Design | Android 12
Redmi A1 (Light Green, 2GB RAM 32GB ROM) | Segment Best AI Dual Cam | 5000mAh Battery | Leather Texture Design | Android 12
realme narzo 50i (Mint Green, 4GB RAM+64GB Storage) Octa Core Processor | 6.5" inch Large Display
...

相关内容

  • 没有找到相关文章

最新更新