奔跑的赵云 发表于 2020-12-5 19:06:53

网页信息转化城浮点数 出了问题

[float(i.text.split('|').strip()[:-2]) for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
不转换浮点数的时候是很正常的,但是加上float() 之后就会报错:'list' object is not callable 该怎么改??

奔跑的赵云 发表于 2020-12-5 19:48:07

原本是这样的 .strip()[:-2] for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
所得是:['58.81',
'89.79',                         ##想把这些数字转换成浮点数就成了上面那样
'30.27',
'79.12',
'125.35',
'51.3',
'61.06',
'97.22',
'65.67',
'127.17',
'79.46',
'132.51',
'57.78',
'54.64',
'57.18']

永恒的蓝色梦想 发表于 2020-12-5 20:01:50

奔跑的赵云 发表于 2020-12-5 19:48
原本是这样的 .strip()[:-2] for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
所得是 ...

不可能报错。

奔跑的赵云 发表于 2020-12-5 20:08:55

永恒的蓝色梦想 发表于 2020-12-5 20:01
不可能报错。

情况如下:
soup = bs4.BeautifulSoup(respone.text)
name = ##刚才那些乱七八糟的码,不是text信息
type = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
size = .strip()[:-2] for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
dircetion = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
ZX = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
floor = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
year = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
) for i in soup.findAll(name ='div',attrs = {'class':'totalPrice'})]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-5bac3378361a> in <module>
      7 floor = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
      8 year = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
----> 9 ) for i in soup.findAll(name ='div',attrs = {'class':'totalPrice'})]

<ipython-input-39-5bac3378361a> in <listcomp>(.0)
      7 floor = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
      8 year = .strip() for i in soup.findAll(name ='div',attrs = {'class':'houseInfo'})]
----> 9 ) for i in soup.findAll(name ='div',attrs = {'class':'totalPrice'})]

TypeError: 'list' object is not callable
页: [1]
查看完整版本: 网页信息转化城浮点数 出了问题