翼度科技»论坛 编程开发 python 查看内容

【爬虫软件】用Python开发的抖音评论区批量采集工具

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
一、背景说明

1.1 效果演示

用python开发的爬虫采集软件,可自动抓取抖音评论数据,并且含二级评论!
为什么有了源码还开发界面软件呢?方便不懂编程代码的小白用户使用,无需安装python、无需懂代码,双击打开即用!
软件界面截图:

爬取结果截图:



以上。
1.2 演示视频

软件运行演示视频:见原文
1.3 软件说明

几点重要说明:

二、代码讲解

2.1 爬虫采集模块

首先,定义接口地址作为请求地址:
  1. # 请求地址
  2. url = 'https://www.douyin.com/aweme/v1/web/comment/list/'
复制代码
定义一个请求头,用于伪造浏览器:
  1. # 请求头
  2. h1 = {
  3.         'accept': 'application/json, text/plain, */*',
  4.         'accept-encoding': 'gzip, deflate, br',
  5.         'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
  6.         'cookie': '换成自己的cookie值',
  7.         'referer': 'https://www.douyin.com/',
  8.         'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
  9.         'sec-ch-ua-mobile': '?0',
  10.         'sec-ch-ua-platform': '"macOS"',
  11.         'sec-fetch-dest': 'empty',
  12.         'sec-fetch-mode': 'cors',
  13.         'sec-fetch-site': 'same-origin',
  14.         'user-agent': ua,
  15. }
复制代码
其中,cookie是个关键参数,需要填写到软件界面里。cookie获取方法如下:

加上请求参数,告诉程序你的爬取条件是什么:
  1. # 请求参数
  2. params = {
  3.         'device_platform': 'webapp',
  4.         'aid': 6383,
  5.         'channel': 'channel_pc_web',
  6.         'aweme_id': video_id,  # 视频id
  7.         'cursor': page * 20,
  8.         'count': 20,
  9.         'item_type': 0,
  10.         'insert_ids': '',
  11.         'rcFT': '',
  12.         'pc_client_type': 1,
  13.         'version_code': '170400',
  14.         'version_name': '17.4.0',
  15.         'cookie_enabled': 'true',
  16.         'screen_width': 1440,
  17.         'screen_height': 900,
  18.         'browser_language': 'zh-CN',
  19.         'browser_platform': 'MacIntel',
  20.         'browser_name': 'Chrome',
  21.         'browser_version': '109.0.0.0',
  22.         'browser_online': 'true',
  23.         'engine_name': 'Blink',
  24.         'engine_version': '109.0.0.0',
  25.         'os_name': 'Mac OS',
  26.         'os_version': '10.15.7',
  27.         'cpu_core_num': 4,
  28.         'device_memory': 8,
  29.         'platform': 'PC',
  30.         'downlink': 1.5,
  31.         'effective_type': '4g',
  32.         'round_trip_time': 150,
  33.         'webid': 7184233910711879229,
  34.         'msToken': 'LZ3nJ12qCwmFPM1NgmgYAz73RHVG_5ytxc_EMHr_3Mnc9CxfayXlm2kbvRaaisoAdLjRVPdLx5UDrc0snb5UDyQVRdGpd3qHgk64gLh6Tb6lR16WG7VHZQ==',
  35. }
复制代码
下面就是发送请求和接收数据:
  1. # 请求地址
  2. url = 'https://www.douyin.com/aweme/v1/web/comment/list/'# 发送请求r = requests.get(url, headers=h1, params=params)# 转json格式json_data = r.json()
复制代码
定义一些空列表,用于存放解析后字段数据:
  1. ip_list = []  # ip属地
  2. text_list = []  # 评论内容
  3. create_time_list = []  # 评论时间
  4. user_name_list = []  # 评论者昵称
  5. user_url_list = []  # 评论者主页链接
  6. user_unique_id_list = []  # 评论者抖音号
  7. like_count_list = []  # 点赞数
  8. cmt_level_list = []  # 评论级别
复制代码
循环解析字段数据,以"评论内容"为例:
  1. # 循环解析
  2. for comment in comment_list:
  3.         # 评论内容
  4.         text = comment['text']
  5.         text_list.append(text)
复制代码
其他字段同理,不再赘述。
最后,是把数据保存到csv文件:
  1. # 保存数据到DF
  2. df = pd.DataFrame(
  3.         {
  4.                 '目标链接': 'https://www.douyin.com/video/' + str(video_id),
  5.                 '页码': page,
  6.                 '评论者昵称': user_name_list,
  7.                 '评论者id': user_unique_id_list,
  8.                 '评论者主页链接': user_url_list,
  9.                 '评论时间': create_time_list,
  10.                 '评论IP属地': ip_list,
  11.                 '评论点赞数': like_count_list,
  12.                 '评论级别': cmt_level_list,
  13.                 '评论内容': text_list,
  14.         }
  15. )
  16. # 保存到csv文件
  17. if os.path.exists(result_file):  # 如果文件存在,不再设置表头
  18.         header = False
  19. else:  # 否则,设置csv文件表头
  20.         header = True
  21. df.to_csv(result_file, mode='a+', index=False, header=header, encoding='utf_8_sig')
复制代码
完整代码中,还含有:判断循环结束条件、时间戳转换、二级评论及二级展开评论的采集等关键实现逻辑,详见文末。
2.2 软件界面模块

软件界面采用tkinter开发。
主窗口部分:
  1. # 创建日志目录
  2. work_path = os.getcwd()
  3. if not os.path.exists(work_path + "/logs"):
  4.         os.makedirs(work_path + "/logs")
  5. # 创建主窗口
  6. root = tk.Tk()
  7. root.title('抖音评论采集软件 | 马哥python说')
  8. # 设置窗口大小
  9. root.minsize(width=850, height=650)
复制代码
填写cookie控件:
  1. # 【填入Cookie】
  2. tk.Label(root, justify='left', font=('微软', 14), text='个人Cookie:').place(x=30, y=75)
  3. entry_ck = tk.Text(root, bg='#ffffff', width=110, height=2, )
  4. entry_ck.place(x=30, y=100, anchor='nw')  # 摆放位置
复制代码
填写视频链接控件:
  1. # 【视频链接】
  2. tk.Label(root, justify='left', font=('微软', 14), text='视频链接:').place(x=30, y=145)
  3. note_ids = tk.StringVar()
  4. note_ids.set('')
  5. entry_nt = tk.Text(root, bg='#ffffff', width=110, height=14, )
  6. entry_nt.place(x=30, y=170, anchor='nw')  # 摆放位置
复制代码
底部软件版权说明:
  1. # 版权信息
  2. copyright = tk.Label(root, text='@马哥python说 All rights reserved.', font=('仿宋', 10), fg='grey')
  3. copyright.place(x=290, y=625)
复制代码
以上。
2.3 日志模块

好的日志功能,方便软件运行出问题后快速定位原因,修复bug。
核心代码:
  1. def get_logger(self):
  2.         self.logger = logging.getLogger(__name__)
  3.         # 日志格式
  4.         formatter = '[%(asctime)s-%(filename)s][%(funcName)s-%(lineno)d]--%(message)s'
  5.         # 日志级别
  6.         self.logger.setLevel(logging.DEBUG)
  7.         # 控制台日志
  8.         sh = logging.StreamHandler()
  9.         log_formatter = logging.Formatter(formatter, datefmt='%Y-%m-%d %H:%M:%S')
  10.         # info日志文件名
  11.         info_file_name = time.strftime("%Y-%m-%d") + '.log'
  12.         case_dir = r'./logs/'
  13.         info_handler = TimedRotatingFileHandler(filename=case_dir + info_file_name,
  14.                                                                                         when='MIDNIGHT',
  15.                                                                                         interval=1,
  16.                                                                                         backupCount=7,
  17.                                                                                         encoding='utf-8')
复制代码
日志文件截图:

三、转载声明

转载已获原作者 @马哥python说授权:
博客园原文链接:【GUI界面软件】抖音评论采集:自动采集10000多条,含二级评论、展开评论!
持续分享Python干货中,欢迎交流开发技术!

来源:https://www.cnblogs.com/ws235/p/18210398
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具