爬 取 豆 瓣 天 龙 八 部 的 短 评 数 据 , 网 址 为
https://book.douban.com/subject/1255625/comments/。要求:
(1)抓取所有的短评,将评论信息存储至文本文件中;
(2)将评论生成词云图片。效果如下:
一、分析
1、 豆 瓣 天 龙 八 部 的 短 评网页:https://book.douban.com/subject/1255625/comments/结果为
我们需要爬取每一个短评内容,并保存起来
2、通过审查元素,我们得知每一个短评都存放在<li>
标签里的<span>
标签里面
3、将评论信息存储至文本文件中
简单的文件读写就可以实现
4、将评论生成词云图片
1)需要将所有评论采用jieba库进行分词
2)Numpy 库处理原图
3)WordCloud词云库,生成对应的图片
注意:原图要选择背景颜色单一的,如:
二、爬取短评
爬取短评并保存在文本文件中
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
url = 'https://book.douban.com/subject/1255625/comments/?start=%d&limit=20&status=P&sort=new_score'
pl_list = []
for pageNum in range(0, 10):
pl = pageNum*20
new_url = format(url % pl)
page_text = requests.get(url=new_url, headers=headers).text
soup = BeautifulSoup(page_text, 'html.parser')
li_list = soup.find('div', attrs={"id": "comments"}).find_all('li')
for li in li_list:
p = li.find('p', attrs={"class": "comment-content"}).text
pl_list = pl_list + [p]
fw = open('天龙八部.txt', 'w+', encoding='utf-8')
count = 1
for i in pl_list:
fw.write("{}.".format(count)+i+'\n')
count = count+1
fw.close()
注意:
1)考虑到要翻页,网址的选取有一定的不同,有一定的规律(应该是原网页的问题,只能够爬取前200条短评,但是也足够了)
2)pl_list是一个列表,里面存的是每一条短评
三、词云的制作
def generateWordCloud():
finalComment=''
comments=pl_list
for comment in comments:
finalComment+=comment
finalComment=' '.join(jieba.cut(finalComment))
image=numpy.array(Image.open('1.jpg'))
word=WordCloud(
font_path="msyh.ttc",
background_color='white',
mask=image
).generate(finalComment)
word.to_file('天龙八部.jpg')
注意:
1)原图的选择,背景颜色单一,才好制作(background_color=‘white’,根据背景颜色变化)
2)1.jpg是原图
四、完整代码
import requests
from bs4 import BeautifulSoup
import jieba
from wordcloud import WordCloud
import numpy
from PIL import Image
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
url = 'https://book.douban.com/subject/1255625/comments/?start=%d&limit=20&status=P&sort=new_score'
pl_list = []
for pageNum in range(0, 10):
pl = pageNum*20
new_url = format(url % pl)
page_text = requests.get(url=new_url, headers=headers).text
soup = BeautifulSoup(page_text, 'html.parser')
li_list = soup.find('div', attrs={"id": "comments"}).find_all('li')
for li in li_list:
p = li.find('p', attrs={"class": "comment-content"}).text
pl_list = pl_list + [p]
fw = open('天龙八部.txt', 'w+', encoding='utf-8')
count = 1
for i in pl_list:
fw.write("{}.".format(count)+i+'\n')
count = count+1
fw.close()
def generateWordCloud():
finalComment=''
comments=pl_list
for comment in comments:
finalComment+=comment
finalComment=' '.join(jieba.cut(finalComment))
image=numpy.array(Image.open('1.jpg'))
word=WordCloud(
font_path="msyh.ttc",
background_color='white',
mask=image
).generate(finalComment)
word.to_file('天龙八部.jpg')
generateWordCloud()