爬取京东前十页商品信息

'''

作者: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())

你可能感兴趣的:(python,爬虫,chrome)