爬取新闻页面

import jieba
import os
import chardet
import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator
from imageio.v2 import imread

newsTextdir=r'd:\doc\sinenews'
img_path=r'.\background.jpg'
stop_word_path=r'.\stopword.txt'
my_word_path=r'.\myword.txt'

def add_stop_words(list):
    stop_words=set()
    for item in list:
        stop_words.add(item.strip())
    return stop_words

def getnewstext(newsdir):
    news_text=""
    sd=os.walk(newsdir)
    for d,s,fns in sd:
        for fn in fns:
            if fn[-3:]=='txt':
                file=d+os.sep+fn
                print(file)
                try:
                    f=open(file)
                    lines=f.readlines()
                except:
                    ft=open(file,"rb")
                    cs=chardet.detect(ft.read())
                    ft.close()
                    f=open(file,encoding=cs['encoding'])
                    lines=f.readlines()
                for i in range(len(lines)):
                    news_text+='.'.join(lines)
    return news_text

stopword_list=open(stop_word_path,encoding='utf-8').readlines()
myword_list=open(my_word_path,encoding='utf-8').readlines()
bg_img=imread(img_path)
news_text=getnewstext(newsTextdir)

stop_words=add_stop_words(stopword_list)
print('停用词共:',len(stop_words))

jieba.load_userdict(my_word_path)

seg_list=jieba.cut(news_text)
seg_space=''.join(seg_list)

wc=WordCloud(font_path='C:\Windows\Fonts\simfang.ttf',max_words=40,random_state=42,background_color='white',stopwords=stop_words,mask=bg_img,max_font_size=100,scale=5,collocations=False).generate(seg_space)
plt.imshow(wc)
plt.axis("off")
plt.show()
wc.to_file('.wordcloud_res.jpg')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值