请问哪里有问题呀?为啥会报错呀?谢谢!
import jieba
from collections import Counter
from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np
from imageio import imread
import requests
import re
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81'}
ur1 = 'https://s.weibo.com/weibo?q=阿里巴巴'
res = requests.get(ur1, headers=headers, timeout=10).text
p_source = '<p class="txt" node_type="feed_list_content" nick-name="(.*?)">'
source =re.findall(p_source, res)
p_title = '<p class="txt" node-type="feed_list_content" nick-name=".*?">(.*?)</p>'
title = re.findall(p_title,res,re.S)
title_all = ''
for i in range(len(title)):
title[i] = title[i].strip()
title[i] = re.sub('<.*?>', '', title[i])
title_all = title_all + title[i]
print(str(i+1)+'.'+title[i]+'-'+source[i])
words = jieba.cut(title_all)
report_words = []
for word in words:
if len(word) >= 2:
report_words.append(word)
print(report_words)
result = Counter(report_words).most_common(50)
print(result)
background_pic = 'C:\\Users\\15855\\Desktop\\第5章源代码汇总\\5.爬虫数据可视化\\微博.jpg'
images = Image.open(background_pic)
maskImages = np.array(images)
content = ''.join(report_words)
wc = WordCloud(font_path = 'C:\\Users\\15855\\Desktop\\SimHei.ttf',
background_color = 'white',
width = 1000,
height = 600,
mask = maskImages
).generate(content)
back_color = imread(background_pic)
image_colors =ImageColorGenerator(back_color)
wc.recolor(color_func=image_colors)
wc.to_file('C:\\Users\\15855\\Desktop\\微博内容词云图.png')
ValueError Traceback (most recent call last)
Cell In[7], line 39
32 maskImages = np.array(images)
33 content = ''.join(report_words)
34 wc = WordCloud(font_path = 'C:\\Users\\15855\\Desktop\\SimHei.ttf',
35 background_color = 'white',
36 width = 1000,
37 height = 600,
38 mask = maskImages
---> 39 ).generate(content)
40 back_color = imread(background_pic)
41 image_colors =ImageColorGenerator(back_color)
File D:\anaconda\Lib\site-packages\wordcloud\wordcloud.py:639, in WordCloud.generate(self, text)
624 def generate(self, text):
625 """Generate wordcloud from text.
626
627 The input "text" is expected to be a natural text. If you pass a sorted
(...)
637 self
638 """
--> 639 return self.generate_from_text(text)
File D:\anaconda\Lib\site-packages\wordcloud\wordcloud.py:621, in WordCloud.generate_from_text(self, text)
604 """Generate wordcloud from text.
605
606 The input "text" is expected to be a natural text. If you pass a sorted
(...)
618 self
619 """
620 words = self.process_text(text)
--> 621 self.generate_from_frequencies(words)
622 return self
File D:\anaconda\Lib\site-packages\wordcloud\wordcloud.py:410, in WordCloud.generate_from_frequencies(self, frequencies, max_font_size)
408 frequencies = sorted(frequencies.items(), key=itemgetter(1), reverse=True)
409 if len(frequencies) <= 0:
--> 410 raise ValueError("We need at least 1 word to plot a word cloud, "
411 "got %d." % len(frequencies))
412 frequencies = frequencies[:self.max_words]
414 # largest entry will be 1
ValueError: We need at least 1 word to plot a word cloud, got 0.
【以下回答由 GPT 生成】
在查看你的代码后,我发现可能有以下问题和潜在错误导致报错:
第三方库未安装:
jieba
、wordcloud
、Pillow
等库。可以使用pip install
命令进行安装。python import jieba import wordcloud from PIL import Image
请求错误:
https://s.weibo.com/weibo?q=阿里巴巴
是可访问的,并且你的网络连接是正常的。正则表达式使用错误:
文件路径错误:
C:\\Users\\15855\\Desktop\\第5章源代码汇总\\5.爬虫数据可视化\\微博.jpg
是正确的,并且该文件存在。C:\\Users\\15855\\Desktop\\SimHei.ttf
也是正确的。代码逻辑错误:
imread(background_pic)
来读取背景图片,但是之前并没有导入imread
函数,你可以使用Image.open(background_pic)
来替代。请尝试解决上述问题,并提供错误信息和环境配置信息以获得更准确的帮助。如果你仍然遇到困难,可以提供更详细的错误信息和相关环境配置,我将竭诚帮助你解决问题。
【相关推荐】