基于Python爬取京东双十一商品价格曲线
作者:松鼠爱吃饼干 时间:2022-10-22 04:04:42
一年一度的双十一就快到了,各种砍价、盖楼、挖现金的口令将在未来一个月内充斥朋友圈、微信群中。玩过多次双十一活动的小编表示一顿操作猛如虎,一看结果2毛5。浪费时间不说而且未必得到真正的优惠,双十一电商的“明降暗升”已经是默认的潜规则了。打破这种规则很简单,可以用 Python 写一个定时监控商品价格的小工具。
思路第一步抓取商品的价格存入 Python 自带的 SQLite 数据库每天定时抓取商品价格使用 pyecharts 模块绘制价格折线图,让低价一目了然
抓取京东价格
从商品详情的页面中打开 F12 控制面板,找到包含 p.3 的链接,在旁边的 preview 面板中可以看到当前商品价格
defget_jd_price(skuId):
sku_detail_url = 'http://item.jd.com/{}.html'
sku_price_url = 'https://p.3.cn/prices/get?type=1&skuid=J_{}'
r = requests.get(sku_detail_url.format(skuId)).content
soup = BeautifulSoup(r, 'html.parser', from_encoding='utf-8')
sku_name_div = soup.find('div', class_="sku-name")
if not sku_name_div:
print('您输入的商品ID有误!')
return
else:
sku_name = sku_name_div.text.strip()
r = requests.get(sku_price_url.format(skuId))
price = json.loads(r.text)[0]['p']
data = {
'sku_id': skuId,
'sku_name': sku_name,
'price': price
}
return data
把抓取的价格存入 sqlite 数据库,使用 PyCharm 的 Database 功能创建一个 sqlite 数据库
最终将数据插入到数据库
# 新增
def insert(data):
conn = sqlite3.connect('price.db')
c = conn.cursor()
sql = 'INSERT INTO price (sku_id,sku_name,price) VALUES ("{}", "{}", "{}")'.format(data.get("sku_id"), data.get("sku_name"), data.get('price') )
c.execute(sql)
conn.commit()
conn.close()
# 查询
def select(sku_id):
conn = sqlite3.connect('price.db')
c = conn.cursor()
sql = 'select sku_id, sku_name, price, time from price where sku_id = "{}" order by time asc'.format(sku_id)
cursor = c.execute(sql)
datas = []
for row in cursor:
data = {
'sku_id': row[0],
'sku_name': row[1],
'price': row[2],
'time': row[3]
}
datas.append(data)
conn.close()
return datas
示例结果
计划任务
使用轻量级的 schedule 模块每天早上 10 点抓取京东价格这一步骤
安装 schedule 模块
pip install schedule
def run_price_job(skuId):
# 使用不占主线程的方式启动 计划任务
def run_continuously(interval=1):
cease_continuous_run = threading.Event()
class ScheduleThread(threading.Thread):
@classmethod
def run(cls):
while not cease_continuous_run.is_set():
schedule.run_pending()
time.sleep(interval)
continuous_thread = ScheduleThread()
continuous_thread.start()
return cease_continuous_run
# 每天10点运行,get_jd_price:任务方法,skuId:任务方法的参数
schedule.every().day.at("10:00").do(get_jd_price, skuId=skuId)
run_continuously()
查看历史价格
使用 pytharts 模块绘制折线图,直观的查看每一天的价格差异
datas = select(skuId)
def line(datas):
x_data = []
y_data = []
for data in datas:
x_data.append(data.get('time'))
y_data.append(data.get('price'))
(
Line()
.add_xaxis(x_data)
.add_yaxis(datas[0].get('sku_name'), y_data, is_connect_nones=True)
.render("商品历史价格.html")
)
总结
本文抓取了京东商城的价格,小伙伴们也可以修个脚本抓取淘宝的价格。使用 Python 解决生活中的小小痛点,让钱包不再干瘪。
来源:https://www.cnblogs.com/hhh188764/p/13857926.html
标签:Python,爬取,双十一,价格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python将图片转base64,实现前端显示
2021-04-26 14:03:04
![](https://img.aspxhome.com/file/2023/0/97290_0s.jpg)
解决Windows10下mysql5.5数据库命令行中文乱码问题
2024-01-16 15:04:29
python列表与列表算法详解(2)
2023-06-10 21:11:10
![](https://img.aspxhome.com/file/2023/7/110237_0s.jpg)
在go文件服务器加入http.StripPrefix的用途介绍
2023-06-26 07:19:07
![](https://img.aspxhome.com/file/2023/1/92031_0s.jpg)
jupyter notebook远程访问不了的问题解决方法
2022-10-30 08:41:06
![](https://img.aspxhome.com/file/2023/4/118634_0s.png)
全透视:CSS Z-index 属性
2009-09-21 12:52:00
![](https://img.aspxhome.com/file/UploadPic/up/2009092113092072.gif)
Python如何处理大数据?3个技巧效率提升攻略(推荐)
2022-04-02 10:03:03
Python:type、object、class与内置类型实例
2023-09-27 08:51:27
如何修改被表单引用的ASP页面?
2010-06-10 18:32:00
一篇文章搞懂Python反斜杠的相关问题
2021-11-26 17:45:19
![](https://img.aspxhome.com/file/2023/2/97662_0s.png)
python实现按首字母分类查找功能
2023-10-13 11:05:09
利用Python写一个爬妹子的爬虫
2021-07-22 12:44:51
![](https://img.aspxhome.com/file/2023/1/72931_0s.png)
解决python2中unicode()函数在python3中报错的问题
2023-07-15 03:15:12
![](https://img.aspxhome.com/file/2023/0/60980_0s.png)
django创建css文件夹的具体方法
2021-08-29 06:04:08
![](https://img.aspxhome.com/file/2023/4/95684_0s.png)
python图像处理之反色实现方法
2021-06-28 20:35:43
![](https://img.aspxhome.com/file/2023/4/81714_0s.jpg)
解决anaconda安装pytorch报错找不到包的问题
2021-06-17 03:43:31
使用python创建Excel工作簿及工作表过程图解
2021-10-05 03:57:34
![](https://img.aspxhome.com/file/2023/1/121761_0s.png)
Python使用scrapy抓取网站sitemap信息的方法
2023-04-02 20:03:18
详解vue-router和vue-cli以及组件之间的传值
2024-05-21 10:31:10
![](https://img.aspxhome.com/file/2023/1/123921_0s.jpg)
浅析Go语言编程当中映射和方法的基本使用
2024-04-28 09:13:16