使用ElementTreee Django迭代XML响应 [英] Iterate Over XML Response With ElementTreee Django
问题描述
我的目标是采取xml响应并将其保存到我的数据库。我可以使用 Elementtree
访问第一个元素,这是产品
My goal is to take the xml response and save it to my database. I was able to access the first element with Elementtree
which was products
xml看起来像这样,
The xml looks like this,
<?xml version="1.0" encoding="UTF-8"?>
<products>
<item id="0">
<product_id> ...
<product_name> ...
<product_url> ...
<advertiser> ...
<designer> ...
<image_url> ...
<price> ...
<commission> ...
</item>
当我尝试并遍历它时,我得到元素,但不是打印出来的元素中的数据每个元素。
When I try and iterate over it I get the element but not the data in the element printed out for each element.
def advertisers(request):
url = 'https://api.example.com/111'
response = requests.get(url, stream=True)
response.raw.decode_content = True
tree = ElementTree.parse(response.raw)
root = tree.getroot()
for item in root:
product_id = item.find('product_id')
product_name = item.find('product_name')
product_url = item.find('product_url')
advertiser = item.find('advertiser')
designer = item.find('designer')
image_url = item.find('image_url')
price = item.find('price')
commission = item.find('commission')
print (product_id, product_name, product_url, advertiser, designer, image_url, price, commission)
return HttpResponse()
Output
<Element 'product_id' at 0x107bba6d8> <Element 'product_name' at 0x107bba728> <Element 'product_url' at 0x107bba778> <Element 'advertiser' at 0x107bba7c8> <Element 'designer' at 0x107bba818> <Element 'image_url' at 0x107bba868> <Element 'price' at 0x107bba8b8> <Element 'commission' at 0x107bba908>
如果我的模型看起来像这样,有人请告诉我如何构造这个循环来保存元素到数据库。我知道如果我循环遍历它们,并附加每个集合,我可以调用 items.save()
,一切都应该很好。我只是想确保我正确访问正确。
If my model looks like this, would someone please show me how to structure this loop to save the elements to the database. I know if I loop through them and append each set I can call items.save()
and all should be well. I just want to make sure I am accessing the correctly first.
class Products(models.Model):
product_id = models.CharField(max_length=100)
product_name = models.CharField(max_length=100)
product_url = models.CharField(max_length=100)
advertiser = models.CharField(max_length=100)
designer = models.CharField(max_length=100)
image_url = models.CharField(max_length=100)
price = models.CharField(max_length=100)
commission = models.CharField(max_length=100)
def __str__(self):
return self.products
推荐答案
如中所述文档, iterparse()
返回(event,elem)
对(注意顺序)。您的代码以错误的顺序具有事件
和 elem
变量,这就是为什么它总是打印 end
从结束事件
。更正顺序,然后可以从 elem.tag
中检查当前元素名称,并从 elem.text
中获取元素的值>:
打印(elem.tag,elem.text)$ b $($)
As mentioned in the documentation, iterparse()
returns (event, elem)
pairs (notice the order). Your code has event
and elem
variables in the wrong order, that's why it always print end
from "end" event
. Correct the ordering, then you can check current element name from elem.tag
and get value of the element from elem.text
:
for event, elem in items:
print(elem.tag, elem.text)
这篇关于使用ElementTreee Django迭代XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!