BeautifulSoup n levels of nested xml elements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: BeautifulSoup n levels of nested xml elements (/thread-13953.html) |
BeautifulSoup n levels of nested xml elements - fatwalletguy - Nov-07-2018 I am trying to read a xml file and create a dictionary object this is my xml 4 levels of elements This is my code from bs4 import BeautifulSoup def DesignSpace(): infile = open("data.xml","r") contents = infile.read() soup = BeautifulSoup(contents,'xml') space = soup.find('Element') items = {} children = space.findChildren("Element" , recursive=False) for child in children: key = child["name"] for a in child.findChildren("Element"): value=a["name"] subkey=a.findParent("Element") subkey1=subkey["name"] items.setdefault(subkey1, []).append(value) print(items) return items DesignSpace()I can only go 1 level down I want to go all the way to the 4th level or nth level Transmission Tx1 Tx2 Engine V4 V6 RE: BeautifulSoup n levels of nested xml elements - Larz60+ - Nov-08-2018 I have an lxml package that I am releasing soon. the code is here: https://python-forum.io/Thread-selneium-JS-bypass?pid=62480#pid62480 it uses lxml to create a dictionary for an html file. with a slight modification (or maybe none at all), it will work with xml. |