爬取网易云音乐歌曲评论并生成特定词云图

import requests
import json
import jsonpath
import csv
import time
import jieba
import numpy # 图片的转换
from scipy.misc import imread
from PIL import Image # 图片处理
from wordcloud import WordCloud # 词云制作

网页请求

def get_one_comment(offset):
# 设置请求头
headers = {
‘Cookie’: ‘iuqxldmzr=32; _ntes_nnid=bdaab01e87ee929b3a9a91ea44b5cd45,1534172699282; _ntes_nuid=bdaab01e87ee929b3a9a91ea44b5cd45; __utmc=94650624; WM_TID=M4E4ToHGUg4EetTbOjxEC5J%2BuODh%2B0jj; abt=66; WM_NI=cRw1E4mJtjv9dwKem8xCMaYzUgNNyu8qqM25igmzBYDj%2FJGjHnYTJFFFqen2XIq%2FlCdRUdQxmdIvxSl84%2BvraOwnH1lJboEwOdL6UrZhnx030tzRng9NfOIBNXgIUx7GMUI%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb6b15cf88bb8ade56a8eb48291f97ca5b9e1d2c45bf6ed9cb9e659b1be8e89ca2af0fea7c3b92aa18eb9d2c840af96bc8bf533a8a98586f034bc9d8382dc7297b982affc7ffcafbfaeb13fabb9a39bc15388b6e1abc6628cb297b5c94e869abf86ed3a9c97bfd0ef49a88e9b85d474afbc8797fb59b0e8fcccf57aa391b98fcb3bb096ae90c87d8dbc84d7d87a9ab8a299b339f4acb6b3ed6dfb92aab0cc4a8e88a9aad874f59983b6cc37e2a3; __utma=94650624.827593374.1534172700.1535852507.1535857189.3; __utmz=94650624.1535857189.3.3.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; JSESSIONID-WYYY=kgbbgMKEcRf18SvvxZVqNTmWZD%2Fdn8BpA%2F7aMH7vv4mSpiDaE%5CfkC5xPu5hFv0nk5X7PpvlEJJ97%2BC3WyE5Qv50EW%2FdNPQQPenibqq%2F5IyHkuuMlCTkpkb7TRMl9oBEdFi68ktMI8m%2F5Ilyub4P204bpG0qBv4yx9vvw8CmCJ%2B9vCaSd%3A1535859527007; __utmb=94650624.7.10.1535857189’,
‘Referer’: ‘https://music.163.com/song?id=1299557768’,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36’
}
# 字符串拼接
url = ‘http://music.163.com/api/v1/resource/comments/R_SO_4_108740?offset=’ + str(offset) + ‘&limit=20’
try:
time.sleep(2)
response = requests.get(url, headers=headers)
# 状态码判断
if response.status_code == 200:
return response.text

except Exception as e:
    print('出错啦!')
    return None

解析数据

def parse_json_data(contents):
comments = json.loads(contents)
content = jsonpath.jsonpath(comments,’$…content’)
#print(content)
return content

csv保存数据

def save_csv_comments(content):
# encoding=utf_8_sig只能转换中文乱码和字母乱码,不能支持数字的乱码
for i in content:
#print(i)
with open(‘comment_csv.csv’, ‘a’, encoding=‘utf-8’)as fp:
fp.write(i + ‘\n’)

词云生成

def make_word_cloud():
with open(‘comment_csv.csv’,encoding=‘utf-8’)as fp:
text = fp.read()
#print(text)

result= ' '.join(jieba.cut(text))
print(result)
mask = imread('nezha2.jpg')
wc= WordCloud(mask = mask,font_path= 'C:/Windows/Fonts/simsun.ttc',background_color='black',max_words=500,max_font_size=80)
# 生成词云
wc.generate(result)
#展示生产的词云图
image_produce = wc.to_image()
image_produce.show()
# 保存到本地
wc.to_file("animal2.png")

if name == ‘main’:
offset = 40
contents = get_one_comment(offset)
content = parse_json_data(contents)
save_csv_comments(content)
make_word_cloud()
#parse_json_data(contents)

你可能感兴趣的:(爬虫实战)