forked from queensun/Nyspider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomments.py
145 lines (134 loc) · 4.6 KB
/
comments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
import time
import json
import re
import openpyxl
import os
from selenium import webdriver
from bs4 import BeautifulSoup
def get_product_comments(itemId, sellerId, product):
url = 'https://rate.tmall.com/list_detail_rate.htm?itemId={}&sellerId={}&order=1¤tPage={}&append=0&content=1&tagId=&posi=&picture='
page = 1
rateCount = ''
while True:
try:
res_text = requests.get(url.format(itemId, sellerId, page)).text
res_text = res_text.replace('"rateDetail":', '')
data = json.loads(res_text)
if rateCount == '':
rateCount = data['rateCount']
rateList = data['rateList']
except Exception as e:
print(e)
continue
with open('result/%s.txt' % (itemId), 'a') as f:
for item in rateList:
item['rateCount'] = rateCount
item['itemId'] = itemId
item['product'] = product
rateDate = item['rateDate']
if '2017-12' not in rateDate:
break
f.write(str(item) + '\n')
if '2017-12' not in rateDate:
break
if page == data['paginator']['lastPage']:
break
print(itemId, sellerId, page, len(rateList), 'OK')
if(page == 99):
break
page += 1
time.sleep(1)
def crawl_comments():
for line in open('./products.txt', 'r'):
line = eval(line)
get_product_comments(line[2], line[1].split('=')[-1], line)
def write_comments_to_excel():
excel = openpyxl.Workbook()
sheet = excel.create_sheet()
for line in load_txt():
# line=ILLEGAL_CHARACTERS_RE.sub(r'', line)
# line=eval(line)
try:
sheet.append(line)
except Exception as e:
print(e)
excel.save('评论.xlsx')
def write_product_to_excel():
excel = openpyxl.Workbook()
sheet = excel.create_sheet()
for line in open('result.txt','r'):
#line=ILLEGAL_CHARACTERS_RE.sub(r'', line)
try:
line=eval(line)
except Exception as e:
line=line.replace('[','').replace(']','').replace("'",'').split(',')
try:
sheet.append(line)
except Exception as e:
print(e)
excel.save('产品信息.xlsx')
def load_txt():
need_keys = ['itemId','id','auctionSku','displayUserNick','userVipLevel','rateDate','rateContent']
exists = {}
for ID in [eval(line)[2] for line in open('./products.1.txt','r')]:
filename='result/%s.txt'%ID
try:
comment_f=open(filename,'r')
except:
print(ID,'FAILED')
continue
for line in comment_f:
line = eval(line)
product = line['product']
product_id = product[2]
if product_id not in exists:
comment_item = product
pic_num=line['rateCount']['picNum']
append_num=line['rateCount']['used']
product+=[pic_num,append_num]
with open('products.txt', 'a') as f:
f.write(str(comment_item) + '\n')
exists[product_id] = 1
item = []
for key in need_keys:
try:
item.append(line[key])
except:
item.append('')
try:
image_num = len(line['pics'])
except:
image_num = 0
item.append(image_num)
yield [product[4],product[2]]+item
def get_product_info():
browser=webdriver.Chrome('../chromedriver')
browser.get('https://tmall.com')
input("OK?")
for line in open('./products.txt','r'):
item=eval(line)
url='https:'+item[5]
browser.get(url)
input('OK?')
html=browser.page_source
soup=BeautifulSoup(html,'lxml')
try:
J_CollectCount=soup.find('span',id='J_CollectCount').get_text()
except:
J_CollectCount='-'
try:
J_PostageToggleCont=soup.find('div',id='J_PostageToggleCont').get_text()
except:
J_PostageToggleCont='-'
try:
tm_count=soup.find('li',{'class':'tm-ind-emPointCount'}).find('span',{'class':'tm-count'}).get_text()
except:
tm_count='-'
print([J_CollectCount,J_PostageToggleCont,tm_count])
item+=[J_CollectCount,J_PostageToggleCont,tm_count]
f=open('result.txt','a')
f.write(str(item)+'\n')
f.close()
if __name__ == '__main__':
write_product_to_excel()