Python xpath tree returning pointer

Viewed 55

I'm trying to scrape the names of all translations (languages), but XPath tree always return some unusable pointer, I think I cannot use at all. This is the website: https://en.wikipedia.org/wiki/Bratislava.

import requests
import lxml.html

page = requests.get('https://en.wikipedia.org/wiki/Bratislava')
tree = lxml.html.fromstring(page.content)
lang = tree.xpath('//*[@id="p-lang"]/div/ul')
print(lang)

Thanks for help.

1 Answers

Instead of

print(lang)

that returns list of Elements use

print(lang[0].text_content())

to get text content of first Element

Or you can print all languages on by one:

import requests
import lxml.html

page = requests.get('https://en.wikipedia.org/wiki/Bratislava')
tree = lxml.html.fromstring(page.content)
lang = tree.xpath('//*[@id="p-lang"]/div/ul')


for item in lang[0].itertext():
    print(item)
Related