Download data from xml using xpath - returns empty list

Viewed 166

I am fairly new to using python to collect data from the web. I am interested in writing a script that collects data from an xml webpage. Here is the address:

https://www.w3schools.com/xml/guestbook.asp

import requests
from lxml import html

url = "https://www.w3schools.com/xml/guestbook.asp"
page = requests.get(url)

extractedHtml = html.fromstring(page.content)
guest = extractedHtml.xpath("/guestbook/guest/fname")
print(guest)

I am not certain why this is returning an empty list. I've tried numerous syntax in the xpath statement, so I'm losing confidence my overall structure is correct.

For context, I want to write something that will parse the entire xml webpage and return a csv that can be used within other programs. I'm starting with the basics to make sure I understand how the various packages work. Thank you for any help.

1 Answers

This should do it

import requests
from lxml import html

url = "https://www.w3schools.com/xml/guestbook.asp"
page = requests.get(url)

extractedHtml = html.fromstring(page.content)
guest = extractedHtml.xpath("//guestbook/guest/fname")

for i in guest:
    print(i.text)

In the xpath, you need a double-dash in the beginning. Also, this returns a list with elements. The text of each element can be extracted using .text

Related