Get numeric output with parsel

142 Views Asked by At

I'm trying to parse a numeric field using parsel. By default, the documentation shows how to extract text. And this:

from parsel import Selector
html = "<title>2</title>\n"
selector = Selector(text=html)
get_text = selector.css("title::text").get()
print(type(get_text))

Returns str. However,

print(selector.css("title::number").get())

gives the error

cssselect.xpath.ExpressionError: The pseudo-element ::number is unknown

Is there an easy way to extract numbers using parsel?

1

There are 1 best solutions below

0
Willian Vieira On BEST ANSWER

You can use lxml, because parcel conversion return str result.

from lxml import etree
xml = etree.XML('<title>2</title>\n')
find = etree.XPath('number(//title/text())')
result = find(xml)
print(result)
print(type(result))

output:

2.0

<class 'float'>