1

I'm trying to extract text out of some html elements using xpath but can't figure out how to form one. The key thing here is to make use of this ga:type="Comment" within the xpath.

Html elements:

<div ga:type="Comment" class="store" dir="auto">This text should be parsed.</div>

My try so far:

from lxml.html import fromstring

elements = """
<div ga:type="Comment" class="store" dir="auto">This text should be parsed.</div>
"""

root = fromstring(elements)
item = root.xpath("//*[@ga:type='Comment']")[0].text
print(item)

Output I wanna get:

This text should be parsed.

Upon execution I get thie following error:

lxml.etree.XPathEvalError: Undefined namespace prefix

How can I create an xpath containing this portion ga:type="Comment" within it and still has the capability to parse the text?

robots.txt
  • 96
  • 2
  • 10
  • 36

0 Answers0