# download GPX points from https://geocaching.su/?pn=103 select region and download gpx file_from_site = "import_points.gpx" result_for_osmand = "osmand_result.gpx" import xml.etree.ElementTree as ET from html.parser import HTMLParser class HTMLFilter(HTMLParser): text = "" def handle_data(self, data): self.text += data tree = ET.parse(file_from_site) root = tree.getroot() bad_types = ["Waypoint|Trailhead", "Waypoint|Question to Answer", "Waypoint|Reference Point", "Waypoint|Final location", "Waypoint|Virtual Stage", "Waypoint|Parking Area"] out_gpx = ET.Element('gpx') for wpt in root.findall('{http://www.topografix.com/GPX/1/0}wpt'): cache_type = wpt.find('{http://www.topografix.com/GPX/1/0}type').text if cache_type in bad_types: continue lat = wpt.get('lat') lon = wpt.get('lon') desc = wpt.find('{http://www.topografix.com/GPX/1/0}desc') if desc is not None: name = desc.text else: name = "" url = wpt.find('{http://www.topografix.com/GPX/1/0}url') if url is None: url = "" else: url = url.text full_description = wpt.find('{http://www.groundspeak.com/cache/1/0}cache').\ find('{http://www.groundspeak.com/cache/1/0}long_description').text f = HTMLFilter() f.feed(full_description) full_description = f.text comments = [] comments_cache = wpt.find("{http://www.groundspeak.com/cache/1/0}cache") if comments_cache is not None: comments_tags = comments_cache.find("{http://www.groundspeak.com/cache/1/0}logs") if comments_tags is not None: for i in comments_tags: html_conv2 = HTMLFilter() comment_html = i.find("{http://www.groundspeak.com/cache/1/0}text") if comment_html.text is None: continue html_conv2.feed(comment_html.text) comment_clean = html_conv2.text comments.append(comment_clean) # now add to osmand GPX geocache = ET.SubElement(out_gpx, 'wpt') geocache.set('lat', lat) geocache.set('lon', lon) name_tag = ET.SubElement(geocache, 'name') name_tag.text = name # adding comments to description for comment in comments: full_description = full_description + "\n------------------\n" + comment desc_tag = ET.SubElement(geocache, 'desc') desc_tag.text = url + "\n\n" + full_description tree_out = ET.ElementTree(out_gpx) tree_out.write(result_for_osmand, encoding="utf-8")