words_list = list("".join(words)) ^^^^^^^^^^^^^^^^^^^^ TypeError: 'Series' object is not callable 如何处理生成器对象words
时间: 2023-11-13 18:43:06 浏览: 31
要处理生成器对象 `words`,你需要将其转换为列表。这个错误提示表明你正在尝试将一个 `Series` 对象(一种pandas数据结构)作为函数进行调用,但是 `Series` 对象并不是可调用的。
你可以使用以下方式来处理生成器对象 `words`:
```python
words_list = list(words)
```
将 `list()` 函数应用于生成器对象 `words`,这样就可以将其转换为列表。然后你可以继续使用 `words_list` 进行后续操作。
相关问题
words_list = list("".join(words)) ^^^^^^^^^^^^^^^^^^^^ TypeError: 'Series' object is not callable
这个错误是由于尝试在一个`Series`对象上调用一个不可调用的方法所导致的。根据错误信息,你试图在`words`这个`Series`对象上使用`join`方法,但是`Series`对象本身并没有这个方法。
如果你想将`words`这个`Series`对象中的每个元素连接成一个字符串,你可以尝试使用`str.join()`方法。你可以将`words`转换为一个列表,然后再使用`str.join()`方法来连接其中的字符串元素。下面是一个示例代码:
```python
words_list = list("".join(words.tolist()))
```
这里假设`words`是一个`Series`对象,并且它的元素都是字符串类型。你可以通过调用`tolist()`方法将其转换为一个普通的Python列表,然后再使用`str.join()`方法将其中的字符串元素连接起来。
优化这段代码:import requests from bs4 import BeautifulSoup import jieba url = "http://xc.hfut.edu.cn/1955/list{}.htm" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} news_list = [] for i in range(1, 6): # 爬取前5页的新闻标题 res = requests.get(url.format(i), headers=headers) soup = BeautifulSoup(res.text, "html.parser") news = soup.find_all("span", {"class": "news_title"}) for n in news: news_list.append(n.a.string) # 对新闻标题进行分词 words_list = [] for news in news_list: words = jieba.cut(news) for word in words: words_list.append(word) from wordcloud import WordCloud import matplotlib.pyplot as plt from PIL import Image import numpy as np # 读入背景图片 image = Image.open("C:\\xhktSoft\huahua.jpg") graph = np.array(image) # 设置停用词 stop_words = ["的", "是", "在", "了", "和", "与", "也", "还", "有", "就", "等", "中", "及", "对", "是"] # 生成词云图 wc = WordCloud(font_path="msyh.ttc", background_color='white', max_words=200, mask=graph, stopwords=stop_words, max_font_size=200, random_state=42) wc.generate_from_text(" ".join(words_list)) # 绘制词云图 plt.imshow(wc, interpolation='bilinear') plt.axis("off") plt.show()
import requests
from bs4 import BeautifulSoup
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
# 定义函数获取新闻标题
def get_news_titles(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
news_list = []
for i in range(1, 6):
res = requests.get(url.format(i), headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
news = soup.find_all("span", {"class": "news_title"})
for n in news:
news_list.append(n.a.string)
return news_list
# 定义函数对新闻标题进行分词
def cut_words(news_list):
words_list = []
for news in news_list:
words = jieba.cut(news)
for word in words:
words_list.append(word)
return words_list
# 定义函数生成词云图
def generate_wordcloud(words_list, graph):
stop_words = ["的", "是", "在", "了", "和", "与", "也", "还", "有", "就", "等", "中", "及", "对", "是"]
wc = WordCloud(font_path="msyh.ttc", background_color='white', max_words=200, mask=graph, stopwords=stop_words, max_font_size=200, random_state=42)
wc.generate_from_text(" ".join(words_list))
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.show()
# 主函数
if __name__ == '__main__':
url = "http://xc.hfut.edu.cn/1955/list{}.htm"
news_list = get_news_titles(url)
words_list = cut_words(news_list)
graph = np.array(Image.open("C:\\xhktSoft\huahua.jpg"))
generate_wordcloud(words_list, graph)
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)