'''
作者:shangjianweidemacbook
创建时间:2023/3/31 17:39
'''
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from csv import writer
import time
def jd_ricecooker():
b = Chrome()
b.get('https://www.jd.com')
b.find_element(By.CLASS_NAME, 'text').send_keys('电饭煲\n')
time.sleep(2)
for x in range(6):
b.execute_script('window.scrollBy(0,1000)')
time.sleep(1)
goods = b.find_elements(By.CSS_SELECTOR, '.p-name>a')
list_good = []
for count in range(60):
goods[count].click()
b.switch_to.window(b.window_handles[-1])
time.sleep(3)
title = b.find_element(By.CLASS_NAME, 'sku-name').text
price = b.find_element(By.CSS_SELECTOR, '.p-price>.price').text
comment = b.find_element(By.CSS_SELECTOR, '#comment-count>a').text
good_info = b.find_elements(By.CSS_SELECTOR, '.parameter2>li')
name = good_info[0].text
num = good_info[1].text
weight = good_info[2].text
addredd = good_info[3].text
good_num = good_info[4].text
caozuo = good_info[5].text
capacity = good_info[6].text
people_num = good_info[7].text
heat = good_info[-2].text
func = good_info[-1].text
list_good.append(
[title, price, comment, name, num, weight, addredd, good_num, caozuo, capacity, people_num, heat, func
])
print(title, price, comment, name, num, weight, addredd, good_num, caozuo, capacity, people_num, heat, func)
b.close()
b.switch_to.window(b.window_handles[0])
return list_good
def save_csv():
with open('files/京东电饭煲.csv', 'w') as f:
w = writer(f)
w.writerow(['商品标题', '商品价格', '评论数', '商品名', '商品编号', '重量', '产地', '货号', '操作方式', '容量', '适用人数', '加热方式', '功能'])
for x in range(10):
w.writerows(jd_ricecooker())
if __name__ == '__main__':
print(save_csv())