使用加入与否? XML Python api
Using join or not? XML Python api
我需要从 XML,
中得到 2 个值
我需要 属性 和 属性 的标题才能获得 property:titel;
有多个 属性 和标题,但并非每个产品都具有相同数量的 属性。
所以我喜欢即时创建它
我已经用 .join 尝试过,但还是觉得它像工作一样。
获取代码:
http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1
我试过的代码:
kenmerkeigenschap = x.findall('properties/prop/property')
kenmerkvalue = x.findall('properties/prop/values/value/title')
basisurl = "%s"
images = ";".join(basisurl % eigenschap.text for eigenschap in kenmerkeigenschap)
basisurl2 = "%s"
images2 = ";".join(basisurl2 % eigenschap.text for eigenschap in kenmerkvalue)
print(images + images2)
我得到
Eindoordeel;Soort garantie;Garantie;Man/Vrouw/Unisex;Verpakking5;Good Warranty Service;Ja;Unisex;Retailverpakking (Fles)
这必须是:
eindoordeel:5;
Soort garantie:Good Warrenty;
Man/Vrouw/Unisex:Unisex;
等等
谢谢大家,我一如既往地喜欢别人的帮助!
好的:答案部分有效,但我现在在线获取 csv 输出中的最后一项。
import requests
import xml.etree.ElementTree as ET
from lxml import etree
import secrets
r = requests.get('http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1')
root = ET.fromstring(r.content)
#\\CSV AANMAKEN INCLUSIEF HEADERS///
#voorbereiding van de csv inclusief headers comma gescheiden
filename = "edc-nieuwe-producten.csv"
headers = "ID,Artikelnr,EAN,Titel,Merk,Categorie,Prijsin,Stukprijs,Belastingregel,Voorraad,Diepte,Hoogte,Breedte,Gewicht,Materiaal,Beschrijving,Samenvatting,Voorraadtekst,Uitverkochttext,Laagniveau,Mail,Images,MetaTitel,MeteDescr,KeyWords, \n"
f = open(filename, "w")
f.write(str(headers))
#Standaard een basis array
records = []
#\\RESULTATEN IN VARIABELEN ZETTEN///
#De resultaten opvragen uit de root/resultaten
for x in root.iter('product'):
maten = x.findall('measures/packing')
for item in (element for element in maten if element is not None):
maatje = item.text
afmeting = maatje.split('x')
doc = etree.XML(r.content)
for prod in doc.xpath('//product'):
for prop in (prod.xpath('.//property')):
expr = './following-sibling::values[1]//title'
if len(prop.xpath(expr))>0:
kenmerken = (prop.text +":"+ prop.xpath(expr)[0].text + ";")
id =x.find('id').text
titel = x.find('title').text
descr = x.find('description').text
price = x.find('price/b2b').text
ean = x.find('variants/variant/ean').text
artikelnr = x.find('variants/variant/subartnr').text
merk = x.find('brand/title').text
cat = x.findall('categories/category/cat/title')
prijsin = x.find('price/b2c').text
stukprijs = x.find('price/minprice').text
btw = x.find('price/vatnl')
voorraad =x.find('variants/variant/stockestimate').text
gewicht = x.find('measures/weight').text
gewichtfloat = (float(gewicht))
gewichtinKg = gewichtfloat * 0.001
kenmerk =x.findall('properties/prop/property')
kenmerktitel =x.findall('properties/prop/values/value/title')
beschrijving =x.find('description').text
maten = x.findall('measures/packing')
pics = x.findall('pics/pic')
basisurl = "https://cdn.edc-internet.nl/800/%s"
images = ";".join(basisurl % picture.text for picture in pics)
#\\GEKOPPELDE FUNCTIES///
#voor de samenvatting, gebruiken we random zinnen, zo blijven we uniek in google zijn ogen. Hier word gebruik gemaakt van de secrets import.
samenvatting = ("Bestel de " + titel + " nu voordeliger in de online " + cat[1].text + " winkel.", titel + " online kopen met korting en discreet in huis.")
samenvattingrand = secrets.choice(samenvatting)
voorraadtext = ("Vandaag " + titel + " besteld is morgen al genieten.", titel + " nu bestellen is morgen al genieten.", "bestel je nu de " + titel + " dan leveren wij hem morgen")
voorraadtextrand = secrets.choice(voorraadtext)
outofstock = "Helaas niet op voorraad de levertijd is 3 tot 5 werkdagen."
laagvoorraad = "2"
mail = "1"
#We zetten alle kenmerken in de juiste opsomming voor de CSV.
functies = kenmerk[0].text + ":" + kenmerktitel[0].text
#\\REPLACES VAN CONTENT EN CATEGORIE///
#Hier gaan we even wat woorden veranderen om te matchen met de woorden in de winkel.
beschrijving2 = beschrijving.replace(",", " ").replace("\n", "").replace("?", "? ")
belastingregel = btw.text.replace("21", "1")
cat2 = cat[0].text.replace("Vibo's", "Vibrators")
#Opbouw van de categorie tree
categorie = (cat[1].text + ";" + cat2 + ";Home")
#\\AFHANDELING VAN DE GEGEVENS///
#Als er een titel is dan gaan we alles in een array zetten.
if titel:
records.append((id,artikelnr,ean,titel,merk,categorie,prijsin,stukprijs,belastingregel,voorraad,afmeting[0],afmeting[1],afmeting[2],gewichtinKg,kenmerken,beschrijving2,samenvattingrand,voorraadtextrand,outofstock,laagvoorraad,mail,images))
#\\HET INVULLEN VAN DE CSV MET DE RESULTATEN///
for record in records:
#Schrijf de resultaten naar de csv file
f.write(str(record[0]) + ", " + str(record[1]) + ", " + str(record[2]) + ", " + str(record[3]) + ", " + str(record[4]) + ", " + str(record[5]) + ", " + str(record[6]) + ", " + str(record[7]) + ", " + str(record[8]) + ", " + str(record[9]) + ", " + str(record[10]) + ", " + str(record[11]) + ", " + str(record[12]) + ", " + str(record[13]) + ", " + str(record[14]) + ", " + str(record[15]) + ", " + str(record[16]) + ", " + str(record[17]) + ", " + str(record[18]) + ", " + str(record[19]) + ", " + str(record[20]) + ", " + str(record[21]) + "\n")
# Sluiten van de csv (Dit moet anders is deze niet te openen.)
f.close()
kenmerken 中的结果现在只有: Verpakking:Retailverpakking (Fles);
答案的实际输出是:
Eindoordeel:5;
Soort garantie:Good Warranty Service;
Lulhannes:Ja;
Man/Vrouw/Unisex:Unisex;
Verpakking:Retailverpakking (Fles);
但我需要 CSV 中的这个 wat:
Eindoordeel:5;Soort garantie:Good Warranty Service;Garantie:Ja;Man/Vrouw/Unisex:Unisex;Verpakking:Retailverpakking (Fles);
property:value:property2:value2;etc
Jack 在我的 csv 中生成的结果对于 csv 中的每个产品都是相同的:
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
以下应该为您提供每个产品的 property/value 对:
from lxml import etree
import requests
url = "http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1"
req = requests.get(url)
doc = etree.XML(req.content)
prods = doc.xpath('//product')
with open('my_file.csv', "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for prod in prods:
line = ''
for prop in (prod.xpath('.//property')):
expr = './following-sibling::values[1]//title'
if len(prop.xpath(expr))>0:
line+=(prop.text+": "+prop.xpath(expr)[0].text)+","
csv_file.write(line+"\n")
我需要从 XML,
中得到 2 个值我需要 属性 和 属性 的标题才能获得 property:titel; 有多个 属性 和标题,但并非每个产品都具有相同数量的 属性。 所以我喜欢即时创建它
我已经用 .join 尝试过,但还是觉得它像工作一样。
获取代码:
http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1
我试过的代码:
kenmerkeigenschap = x.findall('properties/prop/property')
kenmerkvalue = x.findall('properties/prop/values/value/title')
basisurl = "%s"
images = ";".join(basisurl % eigenschap.text for eigenschap in kenmerkeigenschap)
basisurl2 = "%s"
images2 = ";".join(basisurl2 % eigenschap.text for eigenschap in kenmerkvalue)
print(images + images2)
我得到
Eindoordeel;Soort garantie;Garantie;Man/Vrouw/Unisex;Verpakking5;Good Warranty Service;Ja;Unisex;Retailverpakking (Fles)
这必须是:
eindoordeel:5;
Soort garantie:Good Warrenty;
Man/Vrouw/Unisex:Unisex;
等等
谢谢大家,我一如既往地喜欢别人的帮助!
好的:答案部分有效,但我现在在线获取 csv 输出中的最后一项。
import requests
import xml.etree.ElementTree as ET
from lxml import etree
import secrets
r = requests.get('http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1')
root = ET.fromstring(r.content)
#\\CSV AANMAKEN INCLUSIEF HEADERS///
#voorbereiding van de csv inclusief headers comma gescheiden
filename = "edc-nieuwe-producten.csv"
headers = "ID,Artikelnr,EAN,Titel,Merk,Categorie,Prijsin,Stukprijs,Belastingregel,Voorraad,Diepte,Hoogte,Breedte,Gewicht,Materiaal,Beschrijving,Samenvatting,Voorraadtekst,Uitverkochttext,Laagniveau,Mail,Images,MetaTitel,MeteDescr,KeyWords, \n"
f = open(filename, "w")
f.write(str(headers))
#Standaard een basis array
records = []
#\\RESULTATEN IN VARIABELEN ZETTEN///
#De resultaten opvragen uit de root/resultaten
for x in root.iter('product'):
maten = x.findall('measures/packing')
for item in (element for element in maten if element is not None):
maatje = item.text
afmeting = maatje.split('x')
doc = etree.XML(r.content)
for prod in doc.xpath('//product'):
for prop in (prod.xpath('.//property')):
expr = './following-sibling::values[1]//title'
if len(prop.xpath(expr))>0:
kenmerken = (prop.text +":"+ prop.xpath(expr)[0].text + ";")
id =x.find('id').text
titel = x.find('title').text
descr = x.find('description').text
price = x.find('price/b2b').text
ean = x.find('variants/variant/ean').text
artikelnr = x.find('variants/variant/subartnr').text
merk = x.find('brand/title').text
cat = x.findall('categories/category/cat/title')
prijsin = x.find('price/b2c').text
stukprijs = x.find('price/minprice').text
btw = x.find('price/vatnl')
voorraad =x.find('variants/variant/stockestimate').text
gewicht = x.find('measures/weight').text
gewichtfloat = (float(gewicht))
gewichtinKg = gewichtfloat * 0.001
kenmerk =x.findall('properties/prop/property')
kenmerktitel =x.findall('properties/prop/values/value/title')
beschrijving =x.find('description').text
maten = x.findall('measures/packing')
pics = x.findall('pics/pic')
basisurl = "https://cdn.edc-internet.nl/800/%s"
images = ";".join(basisurl % picture.text for picture in pics)
#\\GEKOPPELDE FUNCTIES///
#voor de samenvatting, gebruiken we random zinnen, zo blijven we uniek in google zijn ogen. Hier word gebruik gemaakt van de secrets import.
samenvatting = ("Bestel de " + titel + " nu voordeliger in de online " + cat[1].text + " winkel.", titel + " online kopen met korting en discreet in huis.")
samenvattingrand = secrets.choice(samenvatting)
voorraadtext = ("Vandaag " + titel + " besteld is morgen al genieten.", titel + " nu bestellen is morgen al genieten.", "bestel je nu de " + titel + " dan leveren wij hem morgen")
voorraadtextrand = secrets.choice(voorraadtext)
outofstock = "Helaas niet op voorraad de levertijd is 3 tot 5 werkdagen."
laagvoorraad = "2"
mail = "1"
#We zetten alle kenmerken in de juiste opsomming voor de CSV.
functies = kenmerk[0].text + ":" + kenmerktitel[0].text
#\\REPLACES VAN CONTENT EN CATEGORIE///
#Hier gaan we even wat woorden veranderen om te matchen met de woorden in de winkel.
beschrijving2 = beschrijving.replace(",", " ").replace("\n", "").replace("?", "? ")
belastingregel = btw.text.replace("21", "1")
cat2 = cat[0].text.replace("Vibo's", "Vibrators")
#Opbouw van de categorie tree
categorie = (cat[1].text + ";" + cat2 + ";Home")
#\\AFHANDELING VAN DE GEGEVENS///
#Als er een titel is dan gaan we alles in een array zetten.
if titel:
records.append((id,artikelnr,ean,titel,merk,categorie,prijsin,stukprijs,belastingregel,voorraad,afmeting[0],afmeting[1],afmeting[2],gewichtinKg,kenmerken,beschrijving2,samenvattingrand,voorraadtextrand,outofstock,laagvoorraad,mail,images))
#\\HET INVULLEN VAN DE CSV MET DE RESULTATEN///
for record in records:
#Schrijf de resultaten naar de csv file
f.write(str(record[0]) + ", " + str(record[1]) + ", " + str(record[2]) + ", " + str(record[3]) + ", " + str(record[4]) + ", " + str(record[5]) + ", " + str(record[6]) + ", " + str(record[7]) + ", " + str(record[8]) + ", " + str(record[9]) + ", " + str(record[10]) + ", " + str(record[11]) + ", " + str(record[12]) + ", " + str(record[13]) + ", " + str(record[14]) + ", " + str(record[15]) + ", " + str(record[16]) + ", " + str(record[17]) + ", " + str(record[18]) + ", " + str(record[19]) + ", " + str(record[20]) + ", " + str(record[21]) + "\n")
# Sluiten van de csv (Dit moet anders is deze niet te openen.)
f.close()
kenmerken 中的结果现在只有: Verpakking:Retailverpakking (Fles);
答案的实际输出是:
Eindoordeel:5;
Soort garantie:Good Warranty Service;
Lulhannes:Ja;
Man/Vrouw/Unisex:Unisex;
Verpakking:Retailverpakking (Fles);
但我需要 CSV 中的这个 wat:
Eindoordeel:5;Soort garantie:Good Warranty Service;Garantie:Ja;Man/Vrouw/Unisex:Unisex;Verpakking:Retailverpakking (Fles);
property:value:property2:value2;etc
Jack 在我的 csv 中生成的结果对于 csv 中的每个产品都是相同的:
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
Eindoordeel: 5;Soort garantie: Good Warranty Service;Garantie: Ja;Man/Vrouw/Unisex: Unisex;Verpakking: Retailverpakking (Fles);
以下应该为您提供每个产品的 property/value 对:
from lxml import etree
import requests
url = "http://api.edc.nl/b2b_feed.php?key=1c020r2rcrc694e79538cec24ctcr131&sort=xml&type=xml&lang=nl&version=2015&new=1"
req = requests.get(url)
doc = etree.XML(req.content)
prods = doc.xpath('//product')
with open('my_file.csv', "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for prod in prods:
line = ''
for prop in (prod.xpath('.//property')):
expr = './following-sibling::values[1]//title'
if len(prop.xpath(expr))>0:
line+=(prop.text+": "+prop.xpath(expr)[0].text)+","
csv_file.write(line+"\n")