【GUI界面软件】快手批评区采集:自动采集10000多条,含二级批评、睁开批评 ...

金歌  金牌会员 | 2024-5-14 19:23:58 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 928|帖子 928|积分 2784

目录

一、背景阐明

1.1 效果演示

您好!我是@马哥python说,一名10年步伐猿。
我用python开发了一个爬虫采集软件,可自动抓取快手批评数据,并且含二级批评!
为什么有了源码还开发界面软件呢?方便不懂编程代码的小白用户使用,无需安装python、无需懂代码,双击打开即用!
软件界面截图:

爬取结果截图:
结果截图1:

结果截图2:

结果截图3:

以上。
1.2 演示视频

软件运行演示视频:
【软件演示】快手批评采集工具,可爬取上万条,含二级批评、睁开批评!
1.3 软件阐明

几点告急阐明:

  • Windows用户可直接双击打开使用,无需Python运行环境,非常方便!
  • 必要填入cookie和爬取目标视频链接。
  • 支持同时爬多个视频的批评。
  • 可爬取9个关键字段,含:目标链接,页码,批评者昵称,批评者id,批评者主页链接,批评时间,批评点赞数,批评级别,批评内容。
  • 批评中包含二级批评及二级睁开批评。
  • 爬取结果自动导出到csv文件。
二、代码解说

2.1 爬虫采集模块

首先,定义接口地址作为请求地址:
  1. # 请求地址
  2. url = 'https://www.kuaishou.com/graphql'
复制代码
定义一个请求头,用于伪造浏览器:
  1. # 请求头
  2. h1 = {
  3.         'Accept': '*/*',
  4.         'Accept-Encoding': 'gzip, deflate, br',
  5.         'Accept-Language': 'zh-CN,zh;q=0.9',
  6.         'Connection': 'keep-alive',
  7.         'Content-Type': 'application/json',
  8.         'Cookie': self.cookie_val,
  9.         'Host': 'www.kuaishou.com',
  10.         'Origin': 'https://www.kuaishou.com',
  11.         'Referer': 'https://www.kuaishou.com',
  12.         'Sec-Fetch-Dest': 'empty',
  13.         'Sec-Fetch-Mode': 'cors',
  14.         'Sec-Fetch-Site': 'same-origin',
  15.         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
  16.         'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
  17.         'sec-ch-ua-mobile': '?0',
  18.         'sec-ch-ua-platform': '"macOS"',
  19. }
复制代码
此中,cookie是个关键参数,必要填写到软件界面里。
cookie获取方法如下:

加上请求参数,告诉步伐你的爬取条件是什么:
  1. # 请求参数
  2. params = {"operationName": "commentListQuery",
  3.                   "variables": {
  4.                           "photoId": video_id,
  5.                           "pcursor": pcursor,
  6.                   },
  7.                   "query": "query commentListQuery($photoId: String, $pcursor: String) {\n  visionCommentList(photoId: $photoId, pcursor: $pcursor) {\n    commentCount\n    pcursor\n    rootComments {\n      commentId\n      authorId\n      authorName\n      content\n      headurl\n      timestamp\n      likedCount\n      realLikedCount\n      liked\n      status\n      authorLiked\n      subCommentCount\n      subCommentsPcursor\n      subComments {\n        commentId\n        authorId\n        authorName\n        content\n        headurl\n        timestamp\n        likedCount\n        realLikedCount\n        liked\n        status\n        authorLiked\n        replyToUserName\n        replyTo\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}
复制代码
下面就是发送请求和吸收数据:
  1. # 发送请求
  2. r = requests.post(url, json=params, headers=h1)
  3. # 接收json数据
  4. json_data = r.json()
复制代码
定义一些空列表,用于存放解析后字段数据:
  1. content_list = []  # 评论内容
  2. create_time_list = []  # 评论时间
  3. like_count_list = []  # 评论点赞数
  4. author_name_list = []  # 评论者昵称
  5. author_id_list = []  # 评论者id
  6. author_link_list = []  # 评论者链接
  7. comment_level_list = []  # 评论级别
复制代码
循环解析字段数据,以"批评内容"为例:
  1. # 循环解析
  2. for data in json_data['data']['visionCommentList']['rootComments']:
  3.         # 评论内容
  4.         content = data['content']
  5.         self.tk_show('评论内容:' + content)
  6.         content_list.append(content)
复制代码
其他字段同理,不再赘述。
最后,是把数据保存到csv文件:
  1. # 保存数据到DF
  2. df = pd.DataFrame(
  3.         {
  4.                 '目标链接': 'https://www.kuaishou.com/short-video/' + video_id,
  5.                 '页码': page,
  6.                 '评论者昵称': author_name_list,
  7.                 '评论者id': author_id_list,
  8.                 '评论者主页链接': author_link_list,
  9.                 '评论时间': create_time_list,
  10.                 '评论点赞数': like_count_list,
  11.                 '评论级别': comment_level_list,
  12.                 '评论内容': content_list,
  13.         }
  14. )
  15. # 保存到csv
  16. if os.path.exists(self.result_file):  # 如果文件存在,不再设置表头
  17.         header = False
  18. else:  # 否则,设置csv文件表头
  19.         header = True
  20. df.to_csv(self.result_file, mode='a+', index=False, header=header, encoding='utf_8_sig')
  21. self.tk_show('视频[{}]第{}页已保存到csv'.format(video_id, page))
复制代码
完整代码中,还含有:游标控制翻页、判断循环结束条件、时间戳转换、二级批评及二级睁开批评的采集等关键实现逻辑,详见文末。
2.2 软件界面模块

软件界面采用tkinter开发。
主窗口部分:
  1. # 创建主窗口
  2. root = tk.Tk()
  3. root.title('快手评论采集软件v1.0 | 马哥python说')
  4. # 设置窗口大小
  5. 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=185)
  3. video_ids = tk.StringVar()
  4. video_ids.set('')
  5. entry_nt = tk.Text(root, bg='#ffffff', width=110, height=12, )
  6. entry_nt.place(x=30, y=210, 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源码及exe软件,微信公众号"老男孩的平常之路"背景回复"爬快手批评软件"即可获取。点击直达
我是@马哥python说,一名10年步伐猿,持续分享python干货中!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表