BeautifulSoup4とPython 3.0を使用したWebスクレイピングスクリプト 価格の値から$記号を削除し、浮動小数点型にして数値演算を実行したいとします。しかしそれはテキストにあります。WebスクレイピングとPythonデータ型
import requests
from bs4 import BeautifulSoup
def bitcoin_scheduler():
url = "https://coinmarketcap.com/currencies/bitcoin/"
r = requests.get(url)
offline_data = r.content
soup = BeautifulSoup(offline_data, 'html.parser')
name_box = soup.find('small', attrs={'class': 'bold hidden-xs'})
name = name_box.text.strip()
price_box = soup.find('span', attrs={'class': 'text-large'})
price = price_box.text.strip()
print(time.ctime(), name, price)
threading.Timer(5.0, bitcoin_scheduler).start()
bitcoin_scheduler()
結果:
temp = "$6962.29"
temp = temp.strip("$") # Removes $ from both sides
temp = float(temp) # Converts to float
temp += 2 # Adding 2
print(temp)
それを:
Wed Nov 15 16:37:20 2017 (BTC) $6962.29
Wed Nov 15 16:37:25 2017 (BTC) $6962.29
Wed Nov 15 16:37:31 2017 (BTC) $6962.29
Wed Nov 15 16:37:36 2017 (BTC) $6962.29
これは物語であり、質問ではありません。あなたは何を試しましたか?どのように失敗しましたか? – timgeb
ヘルプhttps://docs.python.org/3.0/library/stdtypes.html#str.replace –