python jupyter爬虫问题

请问哪里有问题呀?为啥会报错呀?谢谢!

img


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 生成】

在查看你的代码后,我发现可能有以下问题和潜在错误导致报错:

  1. 第三方库未安装:

    • 请确保你已经成功安装了jiebawordcloudPillow等库。可以使用pip install命令进行安装。
    • 你还可以通过在代码中使用如下方式导入库来检查是否安装成功: python import jieba import wordcloud from PIL import Image
  2. 请求错误:

    • 请确认你提供的URL https://s.weibo.com/weibo?q=阿里巴巴 是可访问的,并且你的网络连接是正常的。
    • 可以尝试使用浏览器访问该URL,检查是否能正常获取到内容。
  3. 正则表达式使用错误:

    • 你使用了正则表达式来提取网页内容中的关键信息,但是正则表达式可能存在问题。
    • 请检查你的正则表达式是否正确,并且确保你提供的网页内容和你的匹配规则是匹配的。
  4. 文件路径错误:

    • 请确保你提供的背景图片路径 C:\\Users\\15855\\Desktop\\第5章源代码汇总\\5.爬虫数据可视化\\微博.jpg 是正确的,并且该文件存在。
    • 同样,请确保字体文件路径 C:\\Users\\15855\\Desktop\\SimHei.ttf 也是正确的。
  5. 代码逻辑错误:

    • 你的代码中存在一些可能导致报错的逻辑错误。
    • 例如,你尝试打开背景图片并转换为NumPy数组,但是在之后并没有使用该数组。
    • 另外,在生成词云图像时,你使用了imread(background_pic)来读取背景图片,但是之前并没有导入imread函数,你可以使用Image.open(background_pic)来替代。

请尝试解决上述问题,并提供错误信息和环境配置信息以获得更准确的帮助。如果你仍然遇到困难,可以提供更详细的错误信息和相关环境配置,我将竭诚帮助你解决问题。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^