David Masip
David Masip

Reputation: 2491

Get numeric output with parsel

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?

Upvotes: 1

Views: 157

Answers (1)

Willian Vieira
Willian Vieira

Reputation: 736

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'>

Upvotes: 1

Related Questions