爬取淘宝评论并生成词云

39 篇文章 3 订阅
import requests
import json
from lxml import etree
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator
import numpy as np
import PIL.Image as Image

def getCommodityComments(url):
    if url[url.find('id=')+14] != '&':
        id = url[url.find('id=')+3:url.find('id=')+15]
    else:
        id = url[url.find('id=')+3:url.find('id=')+14]
    url = 'https://rate.taobao.com/feedRateList.htm?auctionNumId='+id+'&currentPageNum=1'
    res = requests.get(url)
    jc = json.loads(res.text.strip().strip('()'))
    max = jc['total']
    comments = []
    count = 0
    page = 1
    print('该商品共有评论'+str(max)+'条,具体如下: loading...')
    while count<max:
        res = requests.get(url[:-1]+str(page))
        page = page + 1
        jc = json.loads(res.text.strip().strip('()'))
        jc = jc['comments']
        for j in jc:
            comments.append(j['content'])
            print(comments[count])
            count = count + 1
    return comments

goods=input('宝贝链接:')
comments=getCommodityComments(goods)

siglist=[]
for i in comments:
    signature=i.strip().replace('评价方未及时做出评价,系统默认好评!','').replace('此用户没有填写评价。','').replace('span','').replace('class','').replace('emoji','')
    rep=re.compile('1f\d+\w*|[<>/=]')
    signature=rep.sub('',signature)
    siglist.append(signature)
text=''.join(siglist)

wordlist=jieba.cut(text,cut_all=True)
word_space_split=" ".join(wordlist)

coloring=np.array(Image.open("1.jpg"))
my_wordcloud=WordCloud(background_color='white',width=2400,height=2400,max_words=2000,
                       mask=coloring,max_font_size=60,
                       random_state=42,scale=2,
                       font_path="simfang.ttf").generate(
                               word_space_split)
image_colors=ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis('off')
plt.show
name=input('图片名称:')
my_wordcloud.to_file('%s.png'%name)
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值