Skip to content

Commit 3b93d56

Browse files
committed
no message
1 parent c9ee414 commit 3b93d56

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# -*- coding: UTF-8 -*-
2+
import json
3+
import re
4+
import sqlite3
5+
import threading
6+
import time
7+
8+
import schedule
9+
10+
import requests
11+
from bs4 import BeautifulSoup
12+
import pyecharts.options as opts
13+
from pyecharts.charts import Line
14+
15+
16+
17+
def insert(data):
18+
conn = sqlite3.connect('price.db')
19+
c = conn.cursor()
20+
sql = 'INSERT INTO price (sku_id,sku_name,price) VALUES ("{}", "{}", "{}")'.format(data.get("sku_id"), data.get("sku_name"), data.get('price') )
21+
c.execute(sql)
22+
conn.commit()
23+
conn.close()
24+
25+
def select(sku_id):
26+
conn = sqlite3.connect('price.db')
27+
c = conn.cursor()
28+
sql = 'select sku_id, sku_name, price, time from price where sku_id = "{}" order by time asc'.format(sku_id)
29+
cursor = c.execute(sql)
30+
31+
datas = []
32+
for row in cursor:
33+
data = {
34+
'sku_id': row[0],
35+
'sku_name': row[1],
36+
'price': row[2],
37+
'time': row[3]
38+
}
39+
datas.append(data)
40+
conn.close()
41+
42+
return datas
43+
44+
45+
46+
47+
def get_jd_price(skuId):
48+
49+
sku_detail_url = 'http://item.jd.com/{}.html'
50+
sku_price_url = 'https://p.3.cn/prices/get?type=1&skuid=J_{}'
51+
52+
r = requests.get(sku_detail_url.format(skuId)).content
53+
54+
soup = BeautifulSoup(r, 'html.parser', from_encoding='utf-8')
55+
sku_name_div = soup.find('div', class_="sku-name")
56+
57+
if not sku_name_div:
58+
print('您输入的商品ID有误!')
59+
return
60+
else:
61+
sku_name = sku_name_div.text.strip()
62+
63+
r = requests.get(sku_price_url.format(skuId))
64+
price = json.loads(r.text)[0]['p']
65+
66+
data = {
67+
'sku_id': skuId,
68+
'sku_name': sku_name,
69+
'price': price
70+
}
71+
72+
insert(data)
73+
74+
def run_price_job(skuId):
75+
76+
# 使用不占主线程的方式启动 计划任务
77+
def run_continuously(interval=1):
78+
cease_continuous_run = threading.Event()
79+
80+
class ScheduleThread(threading.Thread):
81+
@classmethod
82+
def run(cls):
83+
while not cease_continuous_run.is_set():
84+
schedule.run_pending()
85+
time.sleep(interval)
86+
87+
continuous_thread = ScheduleThread()
88+
continuous_thread.start()
89+
return cease_continuous_run
90+
91+
# 每天10点运行
92+
schedule.every().day.at("00:41").do(get_jd_price, skuId=skuId)
93+
run_continuously()
94+
95+
def line(datas):
96+
x_data = []
97+
y_data = []
98+
for data in datas:
99+
x_data.append(data.get('time'))
100+
y_data.append(data.get('price'))
101+
102+
(
103+
Line()
104+
.add_xaxis(x_data)
105+
.add_yaxis(datas[0].get('sku_name'), y_data, is_connect_nones=True)
106+
.render("商品历史价格.html")
107+
)
108+
109+
110+
111+
if __name__ == '__main__':
112+
113+
skuId = input('请输入商品ID:')
114+
115+
run_price_job(skuId)
116+
datas = select(skuId)
117+
line(datas)

0 commit comments

Comments
 (0)