OSError:无法在 Python 中打开资源(词云)

时间:2021-06-10 15:52:07

标签: python macos fonts word-cloud

我正在尝试获取新闻文章中按单词专门排序的关键字。 我想用韩语显示我的词云图像。 但我只看到随机方块而不是韩语关键字。

这是我的代码。

import requests
import urllib.request
import time
import spacy
from bs4 import BeautifulSoup
!pip install wordcloud
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
from matplotlib import font_manager

topic="한국지능정보사회진흥원"
numResults=100
url ="https://www.google.com/search?q="+topic+"&tbm=nws&hl=kr&num="+str(numResults)

response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

results=soup.find_all('div',attrs={'class': 'ZINbbc'})
descriptions = []
for result in results:
    try:
        description = result.find('div', attrs=
                                 {'class':'s3v9rd'}).get_text()
        if description != '':
            descriptions.append(description)
    except:
        continue
        
descriptions

text = ''.join(descriptions)

text

sp = spacy.load('en_core_web_sm')
doc = sp(text)

for word in doc:
 print(word.text, word.pos_, word.dep_)

newText =''
for word in doc:
    if word.pos_ in ['ADJ']:
        newText = " ".join((newText, word.text.lower()))
        
wordcloud = WordCloud(stopwords=STOPWORDS).generate(newText)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

所以我下载了韩文字体,即NanumGothic.ttf。 我修了

wordcloud = WordCloud(stopwords=STOPWORDS).generate(newText)

这样

wordcloud = WordCloud(
    font_path='/Library/Fonts/NanumBarunGothis.ttf',
    background_color='white',
    width=800,
    height=800).generate(newText)

但是我现在看到OSError:无法打开资源

我花了几个小时来解决这个问题,但我无法解决这个问题。 你能解释一下如何解决这个问题吗? 出现这个错误是因为我的电脑是 Mac 吗?

非常感谢您提前提供帮助:)

0 个答案:

没有答案