美文网首页
Python抓取电商平台数据 / 采集商品评论 / 可视化展示

Python抓取电商平台数据 / 采集商品评论 / 可视化展示

作者: 颜狗一只 | 来源:发表于2022-12-16 16:12 被阅读0次

    前言

    大家早好、午好、晚好吖 ❤ ~

    本次亮点

    1. selenium工具的使用

    2. 结构化的数据解析

    3. csv数据保存

    环境介绍:

    • python 3.8

    • pycharm

    • 谷歌驱动 谷歌浏览器

    selenium 操控 谷歌驱动 然后 操控浏览器 模拟人的行为去操作浏览器

    模块使用:

    • selenium

      pip install selenium==3.141.0 (指定版本安装模块)

      安装模块时候速度比较慢 可以切换一下镜像源

      (模拟人的行为去操作浏览器)

    • csv

      内置模块 不需要安装 把数据保存到csv表格里面

    • time

      内置模块 不需要安装 时间模块 延时操作 延时等待

    安装python第三方模块:

    1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

    2. 在pycharm中点击Terminal(终端) 输入安装命令

    selenium 模拟人的行为去操作浏览器

    1. 打开浏览器

    2. 输入网址

    3. 输入想要商品名字

    4. 点击搜索 查看商品数据

    5. 获取我们想要数据内容

    6. 保存数据

    代码展示

    """

    爬取商品数据

    """

    导入模块

    import pprint
    from selenium import webdriver  # 从selenium里面导入webdriver的方法
    # 导入时间模块
    import time
    import csv
    
    word = input('请输入你想要获取商品: ')
    

    创建一个文件保存 如果utf-8保存csv文件乱码 改成 utf-8-sig

    f = open(f'{word}.csv', mode='a', encoding='utf-8', newline='')
    
    csv_writer = csv.DictWriter(f, fieldnames=[
        'title',
        'price',
        'comment',
        'shop_name',
        'href',
    ])
    

    写入表头

    csv_writer.writeheader()  
    

    如果把浏览器驱动放到和python安装目录下面, 可以不用指定驱动路径

    executable_path=r'C:\01-Software-installation\Miniconda3\chromedriver.exe'

    1. 打开浏览器

    driver = webdriver.Chrome()  
    

    实例化浏览器对象, 打开一个浏览器 原本是需要一个谷歌驱动 selenium 对象

    2. 输入网址

    image

    3. 输入想要商品名字

    driver.find_element_by_css_selector('#key').send_keys(word)
    

    4. 点击搜索 查看商品数据

    driver.find_element_by_css_selector('#search > div > div.form > button > i').click()  # 点击动作
    

    5. 下滑网页, 让商品数据全部加载出来

    """执行页面滚动的操作"""  # javascript
    
    def drop_down():
        for x in range(1, 12, 2):  # 1 3 5 7 9 11 在你不断的下拉过程中, 页面高度也会变的
            time.sleep(1)  # 延时操作 死等
            j = x / 9  # 1/9  3/9  5/9  9/9
            # document.documentElement.scrollTop  指定滚动条的位置
            # document.documentElement.scrollHeight 获取浏览器页面的最大高度
            js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
            driver.execute_script(js)
    
    
    def get_shop_info():
        driver.implicitly_wait(10)  # 隐式等待, 等待网页数据加载 只要数据加载完了 就运行下面的程序
        drop_down()
    

    6. 获取所有商品li标签

    css语法 class 可以用小圆点代替, 加上类名字 可以直接定位到标签

        lis = driver.find_elements_by_css_selector('.gl-item')  
    

    elements 提取多个标签 element 提取一个标签

    一个一个提取列表里面元素, 用for循环遍历

        for li in lis:
            try:
                title = li.find_element_by_css_selector('a em').text.replace('\n', '')  # 标题
                price = li.find_element_by_css_selector('.p-price strong i').text  # 价格
                comment = li.find_element_by_css_selector('.p-commit strong a').text  # 评论数
                shop_name = li.find_element_by_css_selector('.p-shop span a').text  # 店铺名字
                href = li.find_element_by_css_selector('.p-name a').get_attribute('href')  # 详情页
    

    7. 保存数据

                dit = {
                    'title': title,
                    'price': price,
                    'comment': comment,
                    'shop_name': shop_name,
                    'href': href,
                }
                csv_writer.writerow(dit)
                print(title, price, comment, shop_name, href)
                # pprint.pprint(title)  格式化输出模块
            except:
                pass
    
        driver.find_element_by_css_selector('.pn-next').click()  # 点击下一页
    
    
    for page in range(1, 11):
        print(f'===========================正在采集第{page}页的数据内容===========================')
        get_shop_info()
    
    driver.quit()  # 采集完数据之后 自动关闭浏览器
    

    """

    爬取商品评论数据

    """

    import requests
    import time
    
    for page in range(10):
        time.sleep(2)
    
    image
        response = requests.get(url=url, headers=headers)
        comments = '\n'.join([index['content'] for index in response.json()['comments']])
    

    comments = [] 创建空列表

    for index in response.json()['comments']: for循环遍历提取列表元素

    a = index['content'] 根据键值对取值 提取评论数据

    comments.append(a) 把评论数据 添加到列表里面

    comments = '\n'.join(comments) 通过join的方法把comments 列表里面的元素 用\n合并成为一个字符串

        print(comments)
        with open('评论.txt', mode='a', encoding='utf-8') as f:
            f.write(comments)
            f.write('\n')
    

    """

    评论制作词云图

    """

    导入模块

    结巴分词

    import jieba    
    

    词云图模块

    import wordcloud    
    

    读取文件 返回对象

    f = open('评论.txt', encoding='utf-8')   
    

    读取文本内容 返回字符串

    text = f.read()  
    

    通过jieba分词 对文本进行词语分割 返回的列表

    text_list = jieba.lcut(text)   
    print(text_list)
    

    通过join方法把 文本词语列表合并成一个字符串

    string = ' '.join(text_list)   
    

    词云图配置

    wc = wordcloud.WordCloud(
        width=800,
        height=800,
        background_color='white',
        scale=15,
        font_path='msyh.ttc'
    )
    

    写入词语内容

    wc.generate(string)   
    

    输出词云图

    wc.to_file('1.png')  
    

    尾语 💝

    有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

    喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

    相关文章

      网友评论

          本文标题:Python抓取电商平台数据 / 采集商品评论 / 可视化展示

          本文链接:https://www.haomeiwen.com/subject/wrodqdtx.html