# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 15:08:33 2018
@author: wenyun.wxw
"""
import requests
import re
#https://rate.tmall.com/list_detail_rate.htm?
#itemId=567925396518 #商品id
#&spuId=960800008 #SPU比价组件 http://open.alimama.com/api/widgets.php#area1
#&sellerId=3081047815 #卖家id
#&order=3
#¤tPage=1 #评论页码
#&append=0
#&content=1 #评论
#&tagId=
#&posi=
#&picture=
#&ua=098%23E1hvY9vEvbQvUvCkvvvvvjiPPszpljD8PFdUgj1VPmPWsjt8RFMO1j1PPsLZAj3PvphvC9vhvvCvpvyCvhQhLaGvC0RvQRAn%2BbyDCwLvTWeARFxjKOmxfBuKNB3rQjxr%2Bull8j7QD70wd56OfaClff8rakrlIUkt%2BbODCToQD70OVC69D7zyaXTJKphv8vvvvvCvpvvvvvvm6yCv2WgvvUnvphvpgvvv96CvpCCvvvm2phCvhhmEvpvVmvvC9jXCuphvmvvv92bBNpp%2F2QhvCvvvMMGtvpvhvvvvv8wCvvpvvUmm3QhvCvvhvvv%3D&isg=BPLyKgOl_UVDUcERZO6yep7lQzjKgtWR3qS4grzLHqWQT5JJpBNGLfiNO6vWP261&needFold=0
#&_ksTS=1531452327008_411
#&callback=jsonp412
#创建循环链接
urls = []
#替换页面数为i,取前100页评论
for i in list(range(1,99)):
urls.append('https://rate.tmall.com/list_detail_rate.htm?itemId=567925396518&spuId=960800008&sellerId=3081047815&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('颜色分类:(.*?)","auctionTitle"'),content))
ratecontent.extend(re.findall(re.compile('"rateContent":"(.*?)","rateDate"'),content))
#ratedate.extend(re.findall(re.compile('"rateDate":"(.*?)","reply"'),content))
# 写入数据
file = open('天猫精灵评价all.csv','w')
#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(ratecontent[i]+'\n')
file.close()
ratecontent = []
#读取预料 一行预料为一个文档
for line in open('天猫精灵评价all.csv', 'r').readlines():
#print(line)
ratecontent.append(line.strip())