Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing KML/HTML file using BeautifulSoup

I am looking at a kml file which I am trying to parse using BeautifulSoup

I am trying the following code but have failed to achieve the desired output:

from bs4 import BeautifulSoup
fn = r'sampleKMLFile.kml'
f = open(fn, 'r')
s = BeautifulSoup(f, 'xml')
pnodes = s.find_all('name')
z2 = s.find_all("Folder", {"name": 'MyNodes'})

Basically I want to achieve the following in a pandas dataframe:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MyNodes longitude latitude
Houston -95       33 
Austin  -97       33

The KML file is large and has the following content that I am interested in.

<Folder>
        <name>MyNodes</name>
    <Placemark>
      <name>Houston</name>
      <Camera>
        <longitude>-95</longitude>
        <latitude>33</latitude>
        <roll>-1.6</roll>
        <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
      </Camera>
      <styleUrl>#msn_placemark_circle</styleUrl>
      <Point>
        <coordinates>-95,33,0</coordinates>
        <gx:drawOrder>1</gx:drawOrder>
      </Point>
    </Placemark>
    <Placemark>
      <name>Austin</name>
      <Camera>
        <longitude>-97</longitude>
        <latitude>33</latitude>
        <roll>-1.6</roll>
        <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
      </Camera>
      <styleUrl>#msn_placemark_circle</styleUrl>
      <Point>
        <coordinates>-97,33,0</coordinates>
        <gx:drawOrder>1</gx:drawOrder>
      </Point>
    </Placemark>
      </Folder>

Edit: since the above html format string is in a file, how do I read the file to generate sample or cnt from the two answers below

>Solution :

# cnt = """KML file content str"""
soup = BeautifulSoup(cnt, "lxml")
placemark = soup.find_all("placemark")
print("MyNodes longitude latitude")
for obj in placemark:
    # here you can save the value inside i.e. a dictionary
    print(obj.find("name").text, end=" ")
    print(obj.find("longitude").text, end=" ")
    print(obj.find("latitude").text)

# MyNodes longitude latitude
# Houston -95 33
# Austin -97 33

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading