gpt4 book ai didi

python - XPath 和 Python - 检索货币

转载 作者:行者123 更新时间:2023-11-28 18:33:48 24 4
gpt4 key购买 nike

我正在 Python 中试验 lmxl、requests 和 xpath。我搜索了很多,但似乎找不到解决问题的方法。

我有以下简单的代码:

from lxml import html
import requests

page = requests.get('http://url.tld/property-for-sale/')
tree = html.fromstring(page.content)

property_link = tree.xpath('//div[@class="search_result_title_box"]/h2/a/@href')
property_title = tree.xpath('//div[@class="search_result_title_box"]/h2/a/text()')
property_price = tree.xpath('//div[@class="info-box"]/strong[@class="price"]/text()')



print property_title
print property_price

我正在检索 property_title 和 property_price 并将它们打印出来。但是,当我打印特性价格时,我正在检索一个货币字段,我看到它返回的内容类似于以下内容:

[u'\u20ac225,000', u'\u20ac1,000,000', u'\u20ac245,000',.... etc.... ]

我该如何解决这个问题,以便提取数字或格式化货币?

谢谢

最佳答案

这就是列表中 unicode 的表示方式。如果您打印每个项目,它将按您的需要打印:

>>> prices = [u'\u20ac225,000', u'\u20ac1,000,000', u'\u20ac245,000']
>>> print prices
[u'\u20ac225,000', u'\u20ac1,000,000', u'\u20ac245,000']
>>> for price in prices:
... print price
...
€225,000
€1,000,000
€245,000

关于python - XPath 和 Python - 检索货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com