Python爬取糗事百科,并利用Wordcloud进行展示

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup

# 糗事百科的url为:https://www.qiushibaike.com/8hr/page/2/ page后面的数字随页面变化。本次爬去第2-9页的数据
url = 'https://www.qiushibaike.com/8hr/page/'
Text_All=[]
for p in range(2,9):
	start_urls = url + str(p)+'/'
	#print(start_urls)
	res = requests.get(start_urls)
	res.encoding = 'utf-8'
	soup = BeautifulSoup(res.text,'html.parser')
	
	for i in soup.select('.content span')[:]:
		Text_All.append(i.text)
将list转换为str
text_all=' '.join(Text_All)
print(text_all)

#写入text.txt文件
with open('test.txt','wb') as f:
	
	f.write(text_all.encode('utf-8'))

结果如下:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
import codecs,sys 
import random
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
#定义图片色彩函数
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
    return "hsl(15, %d%%, %d%%)" % (random.randint(200, 255),random.randint(10, 55))
    #return "hsl(100, %d%%, %d%%)" % (random.randint(200, 255),random.randint(10, 55))
    

d = path.dirname('.')

# 读取整个文档
text = codecs.open("test.txt","r","utf-8").read()
#text = open(path.join(d, '.txt')).read()
text = " ".join(jieba.cut(text))

# 设置背景图片
flower_coloring = np.array(Image.open(path.join(d, "a.jpg")))
stopwords = set(STOPWORDS)
stopwords.add("said")

#设置背景参数
wc = WordCloud(background_color="white", max_words=2000, mask=flower_coloring,
			   font_path = 'C:\Windows\Fonts\simfang.ttf',
               stopwords=stopwords, max_font_size=12, random_state=10)

# generate word cloud
wc.generate(text)

# show
plt.imshow(wc)
plt.axis("off")
plt.figure()
# 保存图片
wc.to_file(path.join(d,"1.png"))
#更换图片颜色
plt.imshow(wc.recolor(color_func=color_func, random_state=3))
wc.to_file("2.png")

结果展示:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值