Parse XML file to fetch required data and store it in mongodb database in Python -
i have xml file looks this:
i want fetch following information file events:
under category event:
- start_date
- end_date
- title
under category venue:
- address
- address_2/
- city
- latitude
- longitude
- name
- postal_code
and store information in mongodb database. don't have experience in parsing. can please me this! !
from pymongo import mongoclient import xml.etree.elementtree et urllib2 import urlopen cl = mongoclient() coll = cl["dbname"]["collectionname"] tree = et.parse("https://www.eventbrite.com/xml/event_search?app_key=uso53e2zht6lm4d5ra&country=de&max=100&date=future&page=1") root = tree.getroot() event in root.findall("./event"): doc = {} c in event.getchildren(): if c.tag in ("start_date", "end_date", "title"): doc[c.tag] = c.text elif c.tag == "venue": doc[c.tag] = {} v in c.getchildren(): if v.tag in ("address", "address_2", "city", "latitude", "longitude", "name", "postal_code"): doc[c.tag][v.tag] = v.text coll.insert(doc)
Comments
Post a Comment