嗨喽~大家好呀,这里是魔王呐 ❤ ~!
python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取
环境介绍:
-
python 3.8
-
pycharm 专业版
第三方模块使用
-
requests ——> 发送 HTTP 请求
-
execjs ——> 用来执行JavaScript代码
-
pyecharts ——> 生成 Echarts 图表
-
snapshot_selenium ——>
-
PIL ——> 图像处理
模块安装:
win + R 输入cmd 输入安装命令 pip install 模块名 (如果你觉得安装速度比较慢, 你可以切换国内镜像源)
代码展示
导入模块
import hashlib
import re
import requests
import execjs
from urllib.parse import quote
import time
import datetime
import tkinter as tk
from tkinter import ttk
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from PIL import Image, ImageTk
“”"
获取ticket值
“”"
def get_content(word):link = f'https://tool.manmanbuy.com/HistoryLowest.aspx?url={word}'headers_1 = {'Cookie': 'Hm_lvt_85f48cee3e51cd48eaba80781b243db3=1703053522; _gid=GA1.2.806354424.1703053522; 60014_mmbuser=U1cNVFIHBj1UVwdcUwENUgUAVlAOUwJSAgUHUANQAlQGBwZUBgcLAA%3d%3d; acw_tc=784e2c9117030535779577779e37084d526e4cc1c30239a3bde271b0cddf65; ASP.NET_SessionId=e210i5v0rdntglshjd1kqpdn; Hm_lvt_01a310dc95b71311522403c3237671ae=1703053579; Hm_lpvt_85f48cee3e51cd48eaba80781b243db3=1703053740; _ga=GA1.1.1944431715.1689754950; _ga_1Y4573NPRY=GS1.1.1703053521.5.1.1703053818.0.0.0; Hm_lpvt_01a310dc95b71311522403c3237671ae=1703053818','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',}html_data = requests.get(url=link, headers=headers_1).textBasicAuth = re.findall('id="ticket" value="(.*?)"', html_data)[0]
“”"
获取加密的BasicAuth值
“”"
f_1 = open('demo.js', encoding='utf-8').read()js_code_1 = execjs.compile(f_1)num = js_code_1.call('getTicket', BasicAuth)
“”"
获取token值
“”"
t = int(time.time() * 1000)html = quote(word).upper().replace('/', '%2F')string = f"C5C3F201A8E8FC634D37A766A0299218KEY{html}METHODGETHISTORYTRENDT{t}C5C3F201A8E8FC634D37A766A0299218"MD5 = hashlib.md5()MD5.update(string.encode('utf-8'))token = MD5.hexdigest().upper()print('token: ', token)data = {'method': 'getHistoryTrend','key': word,'t': t,'token': token,}url = 'https://tool.manmanbuy.com/api.ashx'headers = {'Authorization': f'BasicAuth {num}','Cookie': 'Hm_lvt_85f48cee3e51cd48eaba80781b243db3=1703053522; _gid=GA1.2.806354424.1703053522; 60014_mmbuser=U1cNVFIHBj1UVwdcUwENUgUAVlAOUwJSAgUHUANQAlQGBwZUBgcLAA%3d%3d; ASP.NET_SessionId=e210i5v0rdntglshjd1kqpdn; Hm_lvt_01a310dc95b71311522403c3237671ae=1703053579; acw_tc=784e2cad17030688190068163e4f4bfbc2c4c6b6588f89462ab30aa8e802a3; Hm_lpvt_85f48cee3e51cd48eaba80781b243db3=1703069309; _gat_gtag_UA_145348783_1=1; _ga_1Y4573NPRY=GS1.1.1703068787.6.1.1703069309.0.0.0; _ga=GA1.1.1944431715.1689754950; Hm_lpvt_01a310dc95b71311522403c3237671ae=1703069315','Host': 'tool.manmanbuy.com','Origin': 'https://tool.manmanbuy.com','Pragma': 'no-cache','Referer': 'https://tool.manmanbuy.com/HistoryLowest.aspx?url=https%3A%2F%2Fitem.jd.com%2F10061379759087.html','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',}response = requests.post(url=url, data=data, headers=headers)json_data = response.json()title = json_data['data']['spName'] # 标题siteName = json_data['data']['siteName'] # 商城lowerPrice = json_data['data']['lowerPrice'] # 最低价lowerDate = json_data['data']['lowerDate'].split('T')[0] # 最低价时间currentPrice = json_data['data']['currentPrice'] # 当前价info_list = json_data['data']['datePrice'].split('],[') # 历史价dit = {'title': title,'siteName': siteName,'lowerPrice': lowerPrice,'lowerDate': lowerDate,'currentPrice': currentPrice,}print(dit)date_list = []price_list = []for info in info_list:index = info.split(',')num = index[0].replace('[', '')[:-3]date = str(datetime.datetime.fromtimestamp(int(num))).split(' ')[0]price = index[1]# prefer = ''.join(index[2:])date_list.append(date)price_list.append(price)return dit, date_list, price_listdef View(dit, date_list, price_list):print('可视化')c = (Line().add_xaxis(date_list).add_yaxis("", price_list).set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),title_opts=opts.TitleOpts(title=f"{dit['title']}", subtitle="价格走势"),)# .render("line_base.html"))# webbrowser.open('line_base.html')make_snapshot(snapshot, c.render(), "折线图.png")# 显示图片的代码img = Image.open('折线图.png')img = img.resize((962, 566), Image.LANCZOS)img = ImageTk.PhotoImage(img)panel = tk.Label(root, image=img)panel.image = imgpanel.pack(pady=20)def Show():word = key_va.get()dit, date_list, price_list = get_content(word)tree_view.insert('', 1,values=(dit['title'], dit['siteName'], dit['lowerPrice'], dit['lowerDate'], dit['currentPrice']))View(dit, date_list, price_list)root = tk.Tk()
root.title('商品历史价格走势')
root.geometry('989x906+200+200')frame = tk.Frame(root)
frame.pack(anchor=tk.W, pady=5)
tk.Label(frame, text='查商品历史价格走势', font=('微软雅黑', 20)).pack()input_frame = tk.LabelFrame(root,text='京东、天猫、淘宝、拼多多、亚马逊、苏宁、当当、考拉、严选、国美等商品网址',font=('微软雅黑', 15)
)
input_frame.pack(fill=tk.BOTH, pady=10)
key_va = tk.StringVar()
tk.Entry(input_frame, width=100, relief='flat', textvariable=key_va).pack(anchor=tk.W, padx=20, pady=10)
tk.Button(input_frame, relief='flat', text='商品历史价格查询', font=('微软雅黑', 12), bg='#fbc21a',command=Show).pack(anchor=tk.W, padx=20, pady=10)
“”"
输入效果表格
“”"
show_frame = tk.Frame()
show_frame.pack(fill=tk.BOTH)
- 创建字段
columns = ("title", "siteName", "lowerPrice", "lowerDate", 'currentPrice')
columns_value = ('商品', '商城', '最低价', '最低价日期', '当前价')
- 创建表格对象
tree_view = ttk.Treeview(show_frame, show="headings", columns=columns, height=1)
- 给表格添加字段名
tree_view.column('title', width=80, anchor='center')
tree_view.column('siteName', width=80, anchor='center')
tree_view.column('lowerPrice', width=80, anchor='center')
tree_view.column('lowerDate', width=80, anchor='center')
tree_view.column('currentPrice', width=80, anchor='center')
- 设置字段在页面上显示的内容
tree_view.heading('title', text='商品')
tree_view.heading('siteName', text='商城')
tree_view.heading('lowerPrice', text='最低价')
tree_view.heading('lowerDate', text='最低价日期')
tree_view.heading('currentPrice', text='当前价')
- 将表格对象布局到页面上
tree_view.pack(fill=tk.BOTH, expand=True)root.mainloop()
尾语
最后感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。