You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.jd.com/')
#通过id找到搜索框输入内容
driver.find_element(By.ID,'key').send_keys("口红")
#通过类名找到button提交并点击
driver.find_element(By.CLASS_NAME, 'button').click()
# 扫码完之后随便输入一个内容
input()
print("登录成功")
# 登录完成后,跳转到指定链接
driver.get('http://www.jd.com/')
#通过id找到搜索框输入内容
driver.find_element(By.ID,'key').send_keys("口红")
#通过类名找到button提交并点击
driver.find_element(By.CLASS_NAME, 'button').click()
driver.implicitly_wait(10)
with open('JD.csv',mode='w',encoding='UTF-8',newline='') as file:
csv.writer(file).writerow(['商品', '价格','店铺','评论数'])
for page in range(0,50):
sleep(5)
# 下滑页面
js_down = "window.scrollTo(0,8000)"
driver.execute_script(js_down)
sleep(5)
goods = driver.find_elements(By.CLASS_NAME,'gl-i-wrap')
for good in goods:
title = good.find_element(By.CSS_SELECTOR,'.p-name em').text.strip()
price = good.find_element(By.CSS_SELECTOR,'.p-price strong').text.strip()
shop = good.find_element(By.CSS_SELECTOR,'.p-shop span a').text.strip()
comment = good.find_element(By.CSS_SELECTOR,'.p-commit strong a').text.strip()
print('title: ' + title)
print('price: ' + price)
print('shop: ' + shop)
print('comment: ' + comment)
# 用a+模式创建csv文件并写入
f = open('JD.csv', 'a+', encoding='utf-8')
# 基于文件对象构建csv写入
csv_a = csv.writer(f)
# 将数据写入
csv_a.writerow([title, price, shop,comment])
# 关闭文件
f.close()
driver.find_element(By.CLASS_NAME,'pn-next').click()
print('下一页')
input()