我正在尝试创建一个脚本,用于监视this link网站上加载的新产品,如果发现具有唯一标题的新产品,则将其连同大小一起存储在SQLite数据库中它的。但是,当我存储它时,它仅存储尺寸,而不是产品的所有尺寸,this就是它的外观。我不明白如何用一把钥匙存储所有尺寸。这样,如果加载了新产品,则会添加产品标题和所有尺寸。
我尝试将大小添加到列表中,并在该列表中运行for循环,但这没用。
import requests
from bs4 import BeautifulSoup
import json
import random
import sqlite3
import discord
import time
from discord_webhook import DiscordWebhook, DiscordEmbed
link = 'https://deadstock.ca/products.json'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
def add_to_db(title, size):
conn = sqlite3.connect('testing.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS Keywordss (titles TEXT UNIQUE, sizes TEXT)''')
alert = False
try:
cur.execute('INSERT INTO Keywordss (titles, sizes) VALUES (?, ?)', (title, size))
print(title + ' JUST DROPPED' + time.asctime())
except sqlite3.IntegrityError:
pass
conn.commit()
cur.close()
def get_product():
size_lst = []
page = requests.get(link, headers=headers)
try:
jsn = page.json()
except:
print('the site has temp banned you')
for product in jsn['products']:
id = product['id']
title = product['title']
sizes = product['variants']
for size in sizes:
print(size)
size = size['title']
add_to_db(title, size)
get_product()
我希望它可以将产品标题以及所有尺寸添加到数据库中。