[Python] 纯文本查看 复制代码 from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import csv
f = open('su//'+'suning5.csv', mode='a', encoding='utf-8', newline='')
writer = csv.writer(f)
writer.writerow(['价格', '标题', '链接', '评价', '店铺'])
# 打开chrome浏览器
driver = webdriver.Chrome()
# 打开网址
driver.get('https://search.suning.com/iphone14/')
driver.implicitly_wait(3)
# 滚动窗口
for page in range(2):
driver.execute_script('document.querySelector("body > div.ng-footer > div.ng-s-footer").scrollIntoView()')
time.sleep(2)
# 提取数据
divs = driver.find_elements(By.CSS_SELECTOR, '.product-box')
for div in divs:
price = div.find_element(By.CSS_SELECTOR, '.price-box').text
title = div.find_element(By.CSS_SELECTOR, '.title-selling-point').text
href = div.find_element(By.CSS_SELECTOR, '.title-selling-point a').get_attribute('href')
evaluate = div.find_element(By.CSS_SELECTOR, '.info-evaluate').text
store = div.find_element(By.CSS_SELECTOR, '.store-stock').text
print(price, title, href, evaluate, store)
# 保存数据
writer.writerow([price, title, href, evaluate, store])
# 点击下一页
driver.execute_script('document.querySelector("#nextPage").click()')
input()
|