2017-01-26 7 views
0

ウェブサイトから在庫の価格をインポートしようとしています。価格を文字列としてインポートしますが、浮動小数点数に変更しようとするとエラーが表示されます:PythonとBeautifulSoupでデータを削っているときにフロートが無効です。

ValueError: invalid literal for float()

これは私のコードです。

import requests 
from bs4 import BeautifulSoup 

#<span class="current-price" style="color:#D22B00">1,780</span> 

request = requests.get("http://www.sharesansar.com/company/EBL") 

content = request.content 

soup = BeautifulSoup(content, "html.parser") 

span_line = soup.find("span", {"class":"current-price", "style":"color:#D22B00"}) 

price = span_line.text.strip() 

stock_price = float(price) 
+0

を取り除くため、Pythonで無効であるあなたは、 'フロート()' 'の前に印刷price'に入れることはできますか?何が印刷されますか? –

+0

float(price)を実行する前に、可変価格は1780を文字列として保持します。ありがとう! – beebeckzzz

答えて

2
price = span_line.text.strip().replace(',', '') 

1,780,

class float([x])

If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced.

+0

@beebeckzzz ????? –

+1

[locale.atof()](http://stackoverflow.com/a/13362164/515948)を使用できます。 –

関連する問題