首页 > 其他分享 >2024.06.06

2024.06.06

时间:2024-06-12 14:44:53浏览次数:8  
标签:comment regex 2024.06 06 10 words print line

完成概率论、计网作业并提交

python实验:

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)

标签:comment,regex,2024.06,06,10,words,print,line
From: https://www.cnblogs.com/dmx-03/p/18243912

相关文章

  • 【CMake系列】06-项目结构与输出路径管理
    为了对大型项目实现更好的管理【模块化协作开发等等】,cmake提供了很多指令,可以对项目的结构进行调整、管理,便于项目的合理规划。本文我们要学习的就是项目结构的设置,以及构建程序等输出路径的设置本专栏的实践代码全部放在github上,欢迎star!!!如有问题,欢迎留言、或加群......
  • 计算机毕业设计项目推荐,32006 node 中国传统节日介绍网站(开题答辩+程序定制+全套文案
    基于node.js中国传统节日介绍网站 摘 要随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,中国传统节日介绍网站当然也不能排除在外。中国传统节日介绍网站是以实际运用为开发背景,运用软件工程原理和开发方法,采......
  • [20240607]PL/SQL中sql语句的注解.txt
    [20240607]PL/SQL中sql语句的注解.txt--//别人测试遇到的问题,重复测试说明问题.1.环境:SCOTT@test01p>@verBANNER                                                                           ......
  • [20240601]简单探究free list chunk size的分布.txt
    [20240601]简单探究freelistchunksize的分布.txt--//前几天探究探究freelist,无意中发现12c版本freelistchunksize的发生了变化.单独另外写一篇blog.--//我开始分析以为脚本执行有问题,仔细查看12c版本freelistchunksize分布发生了变化.--//我找了以前的11g下的转储,发......
  • [20240604]简单探究RESERVED FREE LISTS chunk size的分布.txt
    [20240604]简单探究RESERVEDFREELISTSchunksize的分布.txt--//前几天探究探究freelist,无意中发现12c版本freelistchunksize的发生了变化.单独另外写一篇blog.--//我开始分析以为脚本执行有问题,仔细查看12c版本freelistchunksize分布发生了变化.--//我找了以前的11g下......
  • CH06_函数
    CH06_函数概述作用:将一段可复用的代码封装起来,减少代码重复。一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。函数的定义函数的定义一般主要有5个步骤:返回值类型函数名参数列表函数体语句返回值语法:返回值类型函数名(参数列表){函数体语句......
  • 伺服电机A06B-0115-B075
    伺服电机A06B-0115-B075数字控制系统简称数控系统,英文名称为Numerical Control System,早期是与计算机并行发展演化的,用于控制自动化加工设备的,由电子管和继电器等硬件构成具有计算能力的专用控制器的称为硬件数控(Hard NC)。20世纪70年代以后,分离的硬件电子元件逐步由集成度......
  • 【2024-06-08】连岳摘抄
    23:59正因为是一张白纸,才可以随心所欲地描绘地图。一切全在你自己。对你来说,一切都是自由的,在你面前是无限的可能。这可是很棒的事啊。我衷心祈祷你可以相信自己,无悔地燃烧自己的人生。                            ......
  • 【2024-06-07】思想变化
    20:00宝剑动连星,金鞍别马鸣。持将五色笔,夺取锦标名。                                                 ——《送人赴举》明·李梦阳昨晚我跟何太都没回家睡,从她公司出......
  • 笔记本电脑(win11+3060+conda)安装PyTorch踩坑记录
    简而言之,先看你的显卡,打开CMD,输入nvidia-smi,右上角有一个CUDA:XX.X表示当前显卡及当前驱动支持的最高版本CUDA。输入nvidia-smi-q可以看到显卡架构(或者直接去Nvidia官网找你的显卡)。再打开这个连接,查看你显卡架构支持的最低版本CUDA。从中选择一个cuda版本torch是自带了cu......