搭建环境
import scrapy
import numpy as np
import matplotlib.pyplot as plt
加载scrapy框架
cd …
scrapy startproject
setting配置
ROBOTSTXT_OBEY = False
USER_AGENT = ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36’
DOWNLOAD_DELAY = 0.5
spider编写并保存数据
class Sanguospider(scrapy.Spider):
name = “threespider”
allowed_domains=[“book.douban.com”]
start_urls=[“https://book.douban.com/subject/1019568/comments/”]
def parse(self, response, **kwargs):
data=response.css(“div.comment span.short::text”).extract()
data=" ".join(data)
open(“result.txt”,“a+”,encoding=“utf-8”).write(data)
测试
scrapy crawl 爬虫名
(新demo)
安装词云和jieba
pip install wordcloud
pip install jieba
import jieba from wordcloud
import WordCloud,STOPWORDS
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
读取数据源 txt=open(‘result.txt’,‘r’,encoding=‘utf-8’).read()
分词 结果是list类型
result=jieba.lcut(txt)
组合成字符串,用空格隔开
text=’ '.join(result)
color_mask =
np.array(Image.open(“heart.png”))
wc = WordCloud(background_color=‘white’, font_path=‘msyh.ttf’, (格式同上空一格)
mask=color_mask)
创建词云图
wc.generate(text)
保存词云图
wc.to_file(“1.png”)
plt 显示词云图
plt.imshow(wc)
去掉坐标轴
plt.axis(‘off’)
plt.show()