2017-11-15 14 views
0

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 
+0

これは物語であり、質問ではありません。あなたは何を試しましたか?どのように失敗しましたか? – timgeb

+0

ヘルプhttps://docs.python.org/3.0/library/stdtypes.html#str.replace –

答えて

0

が交互にストリップ()メソッドここ

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.replace('$','')) 
    threading.Timer(5.0, bitcoin_scheduler).start() 

bitcoin_scheduler() 
1

を使用し、replace()メソッドを使用しますが、簡単な例です6264.29を出力する必要があります。番号に2を加えました。あなたの価格はフォーマット「$ 100.00」である場合

0

、その後のことができますドル記号を削除するだけで実行します。

price = price[1:] 

これは「100.00」に「$ 100.00」になるだろう - それは、最初の文字を取り除き文字列float型に変換するに

price = float(price) 

要するに、それは単に次のようになります。

price = float(price[1:]) 

それはその上にチェックいくつかのエラーを実行する価値があるかもしれません。

1

あなたが定義することができるようにあなたがいないフロートのためにint型のためisdigit()が、デフォルトisdigit()方法のみ 作品でチェックすることができ、独自の両方のために動作しますisdigit()

import requests 
from bs4 import BeautifulSoup 
import time 
import threading 

new=[] 

def isdigit(d): 
    try: 
     float(d) 
     return True 
    except ValueError: 
     return False 

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('$') 
    if isdigit(price)==True: 
     price=float(price) 
     #do your stuff with price 
     print(time.ctime(), name,price) 
     print(type(price)) 


    threading.Timer(5.0, bitcoin_scheduler).start() 

bitcoin_scheduler() 

出力:

Wed Nov 15 17:07:22 2017 (BTC) 7003.54 
<class 'float'> 
Wed Nov 15 17:07:54 2017 (BTC) 7003.54 
<class 'float'> 
関連する問題