Python 如何在Django中将JSON数据插入数据库?
我已经建立了一个从不同的购物网站获取产品数据的刮板 当我运行Python 如何在Django中将JSON数据插入数据库?,python,django,Python,Django,我已经建立了一个从不同的购物网站获取产品数据的刮板 当我运行python scraper.py时,程序将打印一个包含所有数据的JSON对象,如下所示: { 'ebay': [ { 'advertiser': 'ebay', 'advertiser_url': 'https://rover.ebay.com/rover/1/711-53200-19255-0/1?ff3=2&toolid=10041&campid=53384826
python scraper.py
时,程序将打印一个包含所有数据的JSON对象,如下所示:
{ 'ebay': [ { 'advertiser': 'ebay',
'advertiser_url': 'https://rover.ebay.com/rover/1/711-53200-19255-0/1?ff3=2&toolid=10041&campid=5338482617&customid=&lgeo=1&vectorid=229466&item=302847614914',
'description': '30-Day Warranty - Free Charger & Cable - '
'Easy Returns!',
'main_image': 'https://thumbs1.ebaystatic.com/pict/04040_0.jpg',
'price': '290.0',
'title': 'Apple iPhone 8 Plus Smartphone AT&T Sprint '
'T-Mobile Verizon or Unlocked 4G LTE'}
]}
我希望在每次运行scraper时自动将这些数据添加到数据库中
以下是我的数据库结构:
型号.py
class Product(models.Model):
similarity_id = models.CharField(max_length=255, blank=True, null=True)
name = models.CharField(max_length=255, blank=True, null=True)
url = models.SlugField(blank=True, unique=True, allow_unicode=True)
advertiser_url = models.TextField(blank=True, null=True)
main_image = models.TextField(blank=True, null=True)
second_image = models.TextField(blank=True, null=True)
third_image = models.TextField(blank=True, null=True)
old_price = models.FloatField(default=0.00)
price = models.FloatField(default=0.00)
discount = models.FloatField(default=0.00)
currency = models.CharField(max_length=255, default="$")
description = models.TextField(blank=True, null=True)
keywords = models.CharField(max_length=255, blank=True, null=True)
asin = models.CharField(max_length=80, blank=True, null=True)
iban = models.CharField(max_length=255, blank=True, null=True)
sku = models.CharField(max_length=255, blank=True, null=True)
seller = models.CharField(max_length=255, blank=True, null=True)
free_shipping = models.BooleanField(default=False)
in_stock = models.BooleanField(default=True)
sold_items = models.IntegerField(default=0)
likes_count = models.IntegerField(default=0)
category = models.CharField(max_length=255, blank=True, null=True)
sub_category = models.CharField(max_length=255, blank=True, null=True)
reviews_count = models.IntegerField(default=0)
rating = models.FloatField(default=0)
active = models.BooleanField(default=True)
is_prime = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add=True)
advertiser = models.CharField(max_length=255, blank=True, null=True)
objects = ProductManager()
class Meta:
verbose_name_plural = "products"
def __str__(self):
return self.name
将此添加到
scraster.py
:
导入path.to.model
产品=产品()
产品#其中key是字段,value是需要填充的值
分配每个字段后,添加
product.save()
技巧
如果json响应中的所有键与模型中的字段匹配,则可以执行以下操作:
响应中k,v的。项()
setattr(产品,k,v)
product.save()
这将为您节省大量的行和时间:)我经常使用json;我有API缓存,接收大量基于json的API数据,并希望将其存储在数据库中以进行查询和缓存。如果您使用postgres(例如),您将看到If具有json扩展。这意味着您可以将json数据保存在一个特殊的json字段中。但更好的是,有一些sql扩展可以让您对json数据运行查询。也就是说,postgres具有“无sql”功能。这使您能够以本机方式使用json。我觉得它非常吸引人,我强烈推荐它。这是一个学习曲线,因为它使用非传统sql,但见鬼,我们有stackoverflow 见: 下面是一个小例子:
product_onhand_rows = DearCache.objects.filter(
object_type=DearObjectType.PRODUCT_AVAILABILITY.value).filter(
dear_account_id=self.dear_api.account_id).filter(jdata__Location=warehouse).filter(jdata__SKU=sku).all()
在本例中,json存储在字段jdata中。
jdata_uu位置访问json中的键位置。
它筑巢等等。对于高级查询,我求助于sql
select object_type,last_modified, jdata
from cached_dear_dearcache
where object_type = 'orders'
and jdata->>'Status' in ('ESTIMATING','ESTIMATED')
order by last_modified;
还有更多,您可以“展开”列表(这是一个复杂的示例,我的json有发票列表,每个发票都有一个行列表…)
您的方法是将json数据转换为传统的sql模型。那也行。它不是很灵活。。。如果json“模式”更改,您的数据库模式可能需要更改。从理论上讲,我认为最好遵循流程,使用json扩展,这是两全其美的。顺便说一下,性能很好 酷。那么,您试图在哪里执行对数据库的保存?让我说清楚。我有一个价格比较网站,当用户在搜索表单中键入产品名称时,应用程序将向他显示数据库中的产品。当我在shell中键入“python scraper.py”时,产品将自动添加到DB中。
/* 1. listing invoice lines. We have to iterate over the array of invoices to get each invoice, and then inside the invoice object find the array of lines */
select object_type,last_modified, jsonb_array_elements(jsonb_array_elements(cached_dear_dearcache.jdata#>'{Invoices}')->'Lines') as lines,
jsonb_array_elements(cached_dear_dearcache.jdata#>'{Invoices}')->'InvoiceDate' as invoice_date,
jsonb_array_elements(cached_dear_dearcache.jdata#>'{Invoices}')->'InvoiceNumber' as invoice_number
from cached_dear_dearcache
where object_type = 'orders' order by last_modified;