我试图使用python的Beautiful Soup库从html文件中获取一组div,然后从中获取div内部的字符串(money值),然后移除美元符号并将其转换为十进制,这样我就可以使用大于和小于条件语句来比较值。我已经在google上搜索了一下,似乎没有办法将这个unicode字符串转换成十进制值。我真的需要一些帮助。如何将unicode转换为十进制值?在
这是我最后一次尝试:
import unicodedata
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("/Users/sm/Documents/python/htmldemo.html"))
for tag in soup.findAll("div",attrs={"itemprop":"price"}) :
val = tag.string
new_val = val[8:]
workable = int(new_val)
if workable > 250:
print(type(workable))
else:
print(type(workable))
编辑:
当我打印新值的类型时,我得到:
^{pr2}$
使用}取决于您希望它是整数还是可以有小数点的数字。在
int()
或{据我所知,您正在使用
int()
。当您似乎认为这不起作用时,您可能希望float()
。在您确定值是unicode,而不是某个beauthulsoup内部表示形式吗?在python中将unicode转换为integer似乎没有问题。下面是Python解释器的输出。在
可能您在将某些BeautifulGroup内部类型转换为integer时遇到问题。在
相关问题 更多 >
编程相关推荐