2017-11-15 2 views
0

Web Scraping-Skript mit BeautifulSoup4 und Python 3.0 Ich möchte das $ -Zeichen (im Ergebnis) aus Preis Preis, machen Sie es vom Typ float und führen Sie einige numerische Operation auf es. Aber es ist im Text.Web Scraping und Python Datentypen

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() 

Ergebnis:

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

Dies ist eine Geschichte, keine Frage. Was hast du versucht und wie ist es gescheitert? – timgeb

+0

Könnte helfen https://docs.python.org/3.0/library/stdtypes.html#str.replace –

Antwort

0

die replace() -Methode, alternativ die Leiste() -Methode

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

Hier ist ein einfaches Beispiel:

temp = "$6962.29" 
temp = temp.strip("$") # Removes $ from both sides 
temp = float(temp)  # Converts to float 
temp += 2    # Adding 2 
print(temp) 

Es sollte 6264,29 als Ausgabe geben, weil wir haben 2 zur Zahl hinzugefügt.

0

Wenn Ihr Preis im Format „$ 100.00“ ist, dann ist das Dollar-Symbol zu entfernen, können Sie einfach tun:

price = price[1:] 

Dies würde „$ 100.00“ macht in „100.00“ - Es streift das erste Zeichen aus die Saite.

mit einem Schwimmer zu konvertieren:

price = float(price) 

Insgesamt wäre es einfach sein:

price = float(price[1:]) 

Es kann sich lohnen, auf einige Fehler Ausführen von der Überprüfung.

1

können Sie prüfen, mit isdigit() aber Standard isdigit() Methode nur Werke für int nicht für Schwimmer, so dass Sie Ihre eigenen isdigit() definieren können, die für beide funktioniert:

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() 

Ausgabe:

Wed Nov 15 17:07:22 2017 (BTC) 7003.54 
<class 'float'> 
Wed Nov 15 17:07:54 2017 (BTC) 7003.54 
<class 'float'>