Tengo una web de venta Online y quiero poder extraer datos de los pedidos. EL archivo que obtengo de la web es XML o texto. La idea es pasarlo a CSV para poder trabajarlo con Pandas He probado mil maneras distintas, con Beautiful Soup, con lxml, con ElementTree, etc, etc, etc.
Los elementos que tienen un nombre único(OrderNumber) no tengo problema. El problema es con los productos(LineItems). Cada pedido tiene un número diferente de producto(Id) de nombre(Name), catidad(Quantity), etc. Consigo extraer todos los productos, pero todos seguidos, sin saber el número de productos que corresponden a cada pedido.
He intentado pasarlo a csv y a un DataFrame directamente y siempre tengo el problema de los productos.
Cualquier ayuda será agradecida profundamente.
El texto XML es el siguiente:
<?xml version='1.0' encoding='utf-8'?>
<Orders>
<Shop>
<GUID>49A44A11-B3C983C3-A1C85211B59D</GUID>
</Shop>
<Order>
<OrderNumber>1089</OrderNumber>
<CustomerNumber>1028</CustomerNumber>
<Currency>EUR</Currency>
<Language>es</Language>
<Locale>es_ES</Locale>
<TaxArea>EU</TaxArea>
<TaxModel>gross</TaxModel>
<GrandTotal>27.37</GrandTotal>
<TotalBeforeTax>23.59</TotalBeforeTax>
<TotalTax>3.78</TotalTax>
<Addresses>
<BillingAddress>
<FirstName>Pedro</FirstName>
<LastName>Lopez</LastName>
<Gender>1</Gender>
<Street>Mayor, n17</Street>
<Zipcode>10932</Zipcode>
<State>Burgos</State>
<City>Burgos</City>
<EMail>[email protected]</EMail>
<Phone>3585859</Phone>
<Country>ES</Country>
</BillingAddress>
<ShippingAddress>
<FirstName>Pedro</FirstName>
<LastName>Lopez</LastName>
<Street>Mayor, n17</Street>
<Zipcode>10932</Zipcode>
<State>Burgos</State>
<City>Burgos</City>
<EMail>[email protected]</EMail>
<Phone>3585859</Phone>
<Country>ES</Country>
</ShippingAddress>
</Addresses>
<LineItems>
<LineItem>
<Id>POT-16MMLIN-B1K</Id>
<Name>Potenciometro Lineal 16mm Alpha</Name>
<Quantity>1</Quantity>
<UnitPrice>1.55</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.55</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>K-FLUTED-0002</Id>
<Name>Botones de control enflautados.</Name>
<Quantity>2</Quantity>
<UnitPrice>0.75</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.50</TotalPrice>
<TaxRate>0.16</TaxRate>
<VariationString>Capacitancia: 0.010uF, Voltaje: 100V
</LineItem>
<LineItem>
<Id>ENC-1590WB</Id>
<Name>Caja Aluminio Hammond 1590B</Name>
<Quantity>1</Quantity>
<UnitPrice>7.55</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>7.55</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>J-JACKNE-MON</Id>
<Name>Jack Mono Neutrik</Name>
<Quantity>1</Quantity>
<UnitPrice>1.25</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.25</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>J-JACKNE-ST</Id>
<Name>Jack Stereo Neutrik</Name>
<Quantity>1</Quantity>
<UnitPrice>1.58</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.58</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>J-DC2.1-INT</Id>
<Name>Jack DC 2.1mm</Name>
<Quantity>2</Quantity>
<UnitPrice>0.55</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.10</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>SW-3PDT</Id>
<Name>Interruptor pie 3PDT</Name>
<Quantity>1</Quantity>
<UnitPrice>5.15</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>5.15</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>POT-16MMLOG-A500K</Id>
<Name>Potenciometro Logaritmico 16mm Alpha</Name>
<Quantity>1</Quantity>
<UnitPrice>1.65</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>1.65</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItemShipping>
<TotalPrice>4.86</TotalPrice>
<Name>Certificado Nacional / Paquete Azul</Name>
<TaxRate>0.16</TaxRate>
<TaxComponent>
<TaxRate>0.21</TaxRate>
<Value>4.86</Value>
</TaxComponent>
</LineItemShipping>
<LineItemPayment>
<Id>PayPal Standard</Id>
<Name>PayPal</Name>
<TaxRate>0.16</TaxRate>
</LineItemPayment>
<LineItemPaymentDiscount>
<TotalPrice>1.18</TotalPrice>
</LineItemPaymentDiscount>
</LineItems>
<CreationDate>2010-03-15T01:07:29</CreationDate>
<ViewedOn>2010-03-16T12:11:05</ViewedOn>
<DispatchedOn>2010-03-16T12:11:51</DispatchedOn>
<PaidOn>2010-03-15T01:10:07</PaidOn>
</Order>
<Order>
<OrderNumber>1088</OrderNumber>
<CustomerNumber>1136</CustomerNumber>
<Currency>EUR</Currency>
<Language>es</Language>
<Locale>es_ES</Locale>
<TaxArea>EU</TaxArea>
<TaxModel>gross</TaxModel>
<GrandTotal>12.90</GrandTotal>
<TotalBeforeTax>11.12</TotalBeforeTax>
<TotalTax>1.78</TotalTax>
<Addresses>
<BillingAddress>
<FirstName>Luis</FirstName>
<LastName>Lopes</LastName>
<Gender>1</Gender>
<Street>c/Juan Encina</Street>
<Zipcode>56012</Zipcode>
<State>Madrid</State>
<City>Madrid</City>
<EMail>[email protected]</EMail>
<Phone>54223422</Phone>
<PhoneCell>23334342</PhoneCell>
<Country>ES</Country>
</BillingAddress>
</Addresses>
<LineItems>
<LineItem>
<Id>S-PINSTRIP-SIL10</Id>
<Name>Tira de pines en lÃnea SIL</Name>
<Quantity>1</Quantity>
<UnitPrice>0.75</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>0.75</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>ACC-BATT9V-TW-T</Id>
<Name>Conector Pila 9V Tipo-T</Name>
<Quantity>1</Quantity>
<UnitPrice>0.12</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>0.12</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>SW-3PDT</Id>
<Name>Interruptor pie 3PDT</Name>
<Quantity>1</Quantity>
<UnitPrice>5.15</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>5.15</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>LED-BEZEL5MM</Id>
<Name>Zócalo portaled 5mm</Name>
<Quantity>3</Quantity>
<UnitPrice>0.75</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>2.25</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>R-CARBON1/4-2-1M</Id>
<Name>Resistencia de PelÃcula de Carbon Xicon 1/4W, 1%</Name>
<Quantity>2</Quantity>
<UnitPrice>0.12</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>0.24</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>R-CARBON1/4-2-68K</Id>
<Name>Resistencia de PelÃcula de Carbon Xicon 1/4W, 1%</Name>
<Quantity>1</Quantity>
<UnitPrice>0.12</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>0.12</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItem>
<Id>R-METAL1/4-1.2K</Id>
<Name>Resistencia PelÃcula Metálica Xicon 1/4W, 1%</Name>
<Quantity>5</Quantity>
<UnitPrice>0.13</UnitPrice>
<Discount>0.00</Discount>
<TotalPrice>0.65</TotalPrice>
<TaxRate>0.16</TaxRate>
</LineItem>
<LineItemShipping>
<TotalPrice>3.62</TotalPrice>
<Name>Certificado Nacional / Paquete Azul</Name>
<TaxRate>0.16</TaxRate>
<TaxComponent>
<TaxRate>0.21</TaxRate>
<Value>3.62</Value>
</TaxComponent>
</LineItemShipping>
<LineItemPayment>
<Id>CashInAdvance</Id>
<TotalPrice>0.00</TotalPrice>
<Name>Transferencia Bancaria</Name>
<TaxRate>0.16</TaxRate>
</LineItemPayment>
</LineItems>
<CreationDate>2010-03-11T17:33:36</CreationDate>
<ViewedOn>2010-03-12T18:51:45</ViewedOn>
<InProcessOn>2010-03-12T18:53:02</InProcessOn>
<DispatchedOn>2010-03-16T12:11:51</DispatchedOn>
<PaidOn>2010-03-12T18:53:02</PaidOn>
</Order>
</Orders>
EL DataFrame sería algo así:
OrderNumber CustomerNumber FirstName Country Id_Producto Quantity CreationDate
0 1089 1028 Pedro ES POT-16MMLIN-B1K 1 2010-03-15
1 None None None None K-FLUTED-0002 2 None
2 None None None None ENC-1590WB 1 None
3 1088 1136 Luis ES S-PINSTRIP-SIL10 1 2010-03-11
4 None None None None ACC-BATT9V-TW-T 1 None
5 None None None None SW-3PDT 1 None