From 000dc0f0a4746be427ec988e9de018c872681526 Mon Sep 17 00:00:00 2001 From: pu6f8e95t Date: Wed, 13 Apr 2022 18:53:43 +0800 Subject: [PATCH] ADD file via upload --- test1.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test1.py diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..3155c59 --- /dev/null +++ b/test1.py @@ -0,0 +1,90 @@ +import json + +import requests + +import math + +import xlwt + +str1 = 'KRPVTQ2K' +str2 = '31556' +header = { # user-agent伪装 + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36 ' +} + + +def get_result(url): # 获取响应 + response = requests.get(url, header) # 构造一个向服务器请求资源的url对象 + + result = json.loads(response.text) # 解析获取的JSON字符串并将其转换为Python字典 + + return result # 返回Python字典 + + +def get_content(result): # 获取评论内容 + contentList = [] + for item in result['data']['replies']: + data = {} + # 评论者id + data['id'] = item['author']['id'] + # 评论者昵称 + data['name'] = item['author']['name'] + # 评论者手机号 + data['phone'] = item['author']['phone'] + # 评论发表时间 + data['date'] = (item['created_on'].replace("T", " "))[0:18] + # 评论内容 + data['content'] = item['content'].replace("\n", " ") + contentList.append(data) + return contentList + + +def save_cotent(contentList, page, commentName): # 储存评论内容 + for i in range(0, len(contentList)): + for row_index, temp1 in enumerate(contentList): + for col_index, item in enumerate(temp1.values()): + worksheet.write(row_index + 1 + (page - 1) * 10, col_index, item) + workbook.save(commentName + '.xls') + + +discussionUrl = 'https://data.educoder.net/api/boards/{1}/messages.json?coursesId={0}&categoryId=31556&id={0}'.format( + str1, str2) # 向服务器请求‘讨论’资源的url + +discussionResult = get_result(discussionUrl) + +discussionNum = discussionResult['data']['total_count'] # 获得讨论的帖子个数 + +discussionPage = math.ceil(discussionNum / 15) # 计算讨论的帖子页数 + +idList = [] # 用于储存每个帖子的id +for i in range(1, discussionPage + 1): # 获取每个帖子的id + discussionUrl2 = 'https://data.educoder.net/api/boards/{1}/messages.json?coursesId={0}&categoryId=31556&id={0}&page={2}'.format( + str1, str2, str(i)) # 每页讨论的url + discussionResult2 = get_result(discussionUrl2) + discussionList = discussionResult2['data']['messages'] # 获取包含每个帖子详情的列表 + for index, item in enumerate(discussionList): # 储存每个帖子的id + idList.append(item['id']) +for id in idList: + commentUrl = 'https://data.educoder.net/api/messages/{0}.json?coursesId={1}&categoryId={2}&boardId={0}&page_size=10'.format( + str(id), str1, str2) + commentResult = get_result(commentUrl) + commentNum = commentResult['data']['total_replies_count'] # 获得评论个数 + commentName = commentResult['data']['subject'] # 获得帖子名称 + commentPage = math.ceil(commentNum / 10) # 计算每个帖子评论页数 + fieldNameList = ["评论者id", "评论者姓名", "手机号", "评论发表时间", "评论内容"] + workbook = xlwt.Workbook(encoding="utf-8") + worksheet = workbook.add_sheet("评论", cell_overwrite_ok=True) # 创建工作表 + for index2, item2 in enumerate(fieldNameList): + worksheet.write(0, index2, item2) + if (commentNum == 0): # 判断评论是否为0 + worksheet.write(1, 0, '暂无评论') + workbook.save(commentName + '.xls') + else: + for j in range(1, commentPage + 1): + contentUrl = 'https://data.educoder.net/api/messages/{0}/reply_list.json?coursesId={1}&categoryId={2}&boardId={0}&page_size=10&page={3}'.format( + str(id), str1, str2, str(j)) + contentResult = get_result(contentUrl) # 获取评论响应 + contentList = get_content(contentResult) # 获取评论内容 + save_cotent(contentList, j, commentName) # 储存评论内容 + print(commentName + '下载成功!')