'Attribute has no text in the variable, but the text is printed out
I can't figure out why the code works in one case and gives an error in the other.
Working code:
import xml.etree.ElementTree as ET
import requests
baza=[]
document = ET.parse('xml_.xml')
doc=document.getroot()
x = doc.find('shop')
y = x.find('offers')
for z in y.findall('offer'):
vnd_tmp = z.find('vendorCode')
prc_tmp = z.find('price')
#tmp = vnd_tmp.text, prc_tmp.text
print(vnd_tmp.text, prc_tmp.text)
It is necessary to replace "print" with a variable, an error immediately occurs AttributeError: 'NoneType' object has no attribute 'text':
import xml.etree.ElementTree as ET
import requests
baza=[]
document = ET.parse('xml_.xml')
doc=document.getroot()
x = doc.find('shop')
y = x.find('offers')
for z in y.findall('offer'):
vnd_tmp = z.find('vendorCode')
prc_tmp = z.find('price')
tmp = vnd_tmp.text, prc_tmp.text
#print(vnd_tmp.text, prc_tmp.text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
