增补博客 第九篇 python 图书评论数据分析与可视化
1.增补博客 第二篇 python 谢宾斯基三角型字符分形图形输出2.增补博客 第三篇 python 英文统计3.增补博客 第四篇 python 中文级联菜单4.增补博客 第五篇 python 电子算盘5.增补博客 第六篇 python 电子算盘6.增补博客 第七篇 python 比较不同Python图形处理库或图像处理库的异同点7.增补博客 第八篇 python 中国大学排名数据分析与可视化
8.增补博客 第九篇 python 图书评论数据分析与可视化
9.增补博客 第十篇 python 函数图形绘制10.增补博客 第十一篇 python 分段函数图形绘制11.增补博客 第十二篇 python大作业小说阅读器(1)爬取12.增补博客 第十三篇 python大作业小说阅读器(2)爬取13.增补博客 第十四篇 python大作业小说阅读器(3)显示文字函数14.增补博客 第十五篇 python大作业小说阅读器(4)html页面15.增补博客 第十七篇 python 模拟页面调度LRU算法16.增补博客 第十八篇 python 杨辉三角形17.增补博客 第二十篇 python 筛法求素数18.增补博客 第二十一篇 python 查找鞍点19.增补博客 第二十四篇 python 正整数的因子展开式20.增补博客 第二十二篇 python 牛顿迭代法21.增补博客 第二十三篇 python 对比Python中的列表、元组、字典、集合、字符串等之间异同22.增补博客 第十九篇 python 爬楼梯23.增补博客 第一篇 python 简易带参计算器24.增补博客 第十六篇 python 排列组合序列25.增补博客 第二十五篇 python 列举说明Python同Java及C++的不同之处【题目描述】豆瓣图书评论数据爬取。以《平凡的世界》、《都挺好》等为分析对象,编写程序爬取豆瓣读书上针对该图书的短评信息,要求:
(1)对前3页短评信息进行跨页连续爬取;
(2)爬取的数据包含用户名、短评内容、评论时间、评分和点赞数(有用数);
(3)能够根据选择的排序方式(热门或最新)进行爬取,并分别针对热门和最新排序,输出前10位短评信息(包括用户名、短评内容、评论时间、评分和点赞数)。
(4)根据点赞数的多少,按照从多到少的顺序将排名前10位的短评信息输出;
(5附加)结合中文分词和词云生成,对前3页的短评内容进行文本分析:按照词语出现的次数从高到低排序,输出前10位排序结果;并生成一个属于自己的词云图形。
【练习要求】请给出源代码程序和运行测试结果,源代码程序要求添加必要的注释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import re from collections import Counter import requests from lxml import etree import pandas as pd import jieba import matplotlib.pyplot as plt from wordcloud import WordCloud headers = { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39" } comments = [] words = [] def regex_change(line): # 前缀的正则 username_regex = re. compile (r "^\d+::" ) # URL,为了防止对中文的过滤,所以使用[a-zA-Z0-9]而不是\w url_regex = re. compile (r """ (https?://)? ([a-zA-Z0-9]+) (\.[a-zA-Z0-9]+) (\.[a-zA-Z0-9]+)* (/[a-zA-Z0-9]+)* """ , re.VERBOSE | re.IGNORECASE) # 剔除日期 data_regex = re. compile (u """ #utf-8编码 年 | 月 | 日 | (周一) | (周二) | (周三) | (周四) | (周五) | (周六) """ , re.VERBOSE) # 剔除所有数字 decimal_regex = re. compile (r "[^a-zA-Z]\d+" ) # 剔除空格 space_regex = re. compile (r "\s+" ) regEx = "[\n”“|,,;;''/?! 。的了是]" # 去除字符串中的换行符、中文冒号、|,需要去除什么字符就在里面写什么字符 line = re.sub(regEx, "", line) line = username_regex.sub(r"", line) line = url_regex.sub(r"", line) line = data_regex.sub(r"", line) line = decimal_regex.sub(r"", line) line = space_regex.sub(r"", line) return line def getComments(url): score = 0 resp = requests.get(url, headers = headers).text html = etree.HTML(resp) comment_list = html.xpath( ".//div[@class='comment']" ) for comment in comment_list: status = "" name = comment.xpath( ".//span[@class='comment-info']/a/text()" )[ 0 ] # 用户名 content = comment.xpath( ".//p[@class='comment-content']/span[@class='short']/text()" )[ 0 ] # 短评内容 content = str (content).strip() word = jieba.cut(content, cut_all = False , HMM = False ) time = comment.xpath( ".//span[@class='comment-info']/a/text()" )[ 1 ] # 评论时间 mark = comment.xpath( ".//span[@class='comment-info']/span/@title" ) # 评分 if len (mark) = = 0 : score = 0 else : for i in mark: status = str (i) if status = = "力荐" : score = 5 elif status = = "推荐" : score = 4 elif status = = "还行" : score = 3 elif status = = "较差" : score = 2 elif status = = "很差" : score = 1 good = comment.xpath( ".//span[@class='comment-vote']/span[@class='vote-count']/text()" )[ 0 ] # 点赞数(有用数) comments.append([ str (name), content, str (time), score, int (good)]) for i in word: if len (regex_change(i)) > = 2 : words.append(regex_change(i)) def getWordCloud(words): # 生成词云 all_words = [] all_words + = [word for word in words] dict_words = dict (Counter(all_words)) bow_words = sorted (dict_words.items(), key = lambda d: d[ 1 ], reverse = True ) print ( "热词前10位:" ) for i in range ( 10 ): print (bow_words[i]) text = ' ' .join(words) w = WordCloud(background_color = 'white' , width = 1000 , height = 700 , font_path = 'simhei.ttf' , margin = 10 ).generate(text) plt.show() plt.imshow(w) w.to_file( 'wordcloud.png' ) print ( "请选择以下选项:" ) print ( " 1.热门评论" ) print ( " 2.最新评论" ) info = int ( input ()) print ( "前10位短评信息:" ) title = [ '用户名' , '短评内容' , '评论时间' , '评分' , '点赞数' ] if info = = 1 : comments = [] words = [] for i in range ( 0 , 60 , 20 ): url = "https://book.douban.com/subject/10517238/comments/?start={}&limit=20&status=P&sort=new_score" . format ( i) # 前3页短评信息(热门) getComments(url) df = pd.DataFrame(comments, columns = title) print (df.head( 10 )) print ( "点赞数前10位的短评信息:" ) df = df.sort_values(by = '点赞数' , ascending = False ) print (df.head( 10 )) getWordCloud(words) elif info = = 2 : comments = [] words = [] for i in range ( 0 , 60 , 20 ): url = "https://book.douban.com/subject/10517238/comments/?start={}&limit=20&status=P&sort=time" . format ( i) # 前3页短评信息(最新) getComments(url) df = pd.DataFrame(comments, columns = title) print (df.head( 10 )) print ( "点赞数前10位的短评信息:" ) df = df.sort_values(by = '点赞数' , ascending = False ) print (df.head( 10 )) getWordCloud(words) |
合集:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix