# -*- coding: utf-8 -*-
"""
Created on Mon Aug 27 14:42:09 2018
@author: wenyun.wxw
"""
import requests
import re
#创建循环链接
urls = []
#替换页面数为i,取前100页评论
for i in list(range(1,99)):
urls.append('https://rate.tmall.com/list_detail_rate.htm?itemId=566737021294&spuId=946907732&sellerId=3876960410&order=3¤tPage=%s' %i)
# 构建字段容器
#nickname = []
#saletype =[]
color = []
ratecontent = []
#ratedate = []
# 循环抓取数据
for url in urls:
content = requests.get(url).text
# 借助正则表达式使用findall进行匹配查询
#nickname.extend(re.findall('"displayUserNick":"(.*?)"',content))
#saletype.extend(re.findall(re.compile('套餐类型:(.*?);'),content))
color.extend(re.findall(re.compile('颜色分类:(.*?)","anony"'),content))
ratecontent.extend(re.findall(re.compile('"rateContent":"(.*?)","fromMall"'),content))
ratecontent.extend(re.findall(re.compile('"content":"(.*?)","fromMemory"'),content))#追评
#ratedate.extend(re.findall(re.compile('"rateDate":"(.*?)","reply"'),content))
#去重
ratecontent_less=list(set(ratecontent))
# 写入数据
file = open('小度在家评价-天猫.csv','w',encoding='gbk',errors='ignore')
#for i in list(range(len(nickname))):
# file.write(','.join((nickname[i],ratedate[i],saletype[i],color[i],ratecontent[i]))+'\n')
for i in list(range(len(ratecontent))):
file.write(','.join((color[i],ratecontent[i]))+'\n')
file.close()