Das ist ja mal praktisch, vielen Dank!
Das hilft mir weiter, ich interessiere mich ebenfalls für das Auslesen von Website-Daten.
Wie viel Erfahrung hast du schon in Python? Bin noch recht am Anfang, was glaubst du, wie lange es dauert, bis dass ich das auch hinkriegen kann?
Circa 12 Jahre müssten es inzwischen sein. Aber das heißt nicht viel: Beruflich mache ich etwas kaufmännisches. Das Programmieren in Python ist also ein Hobby, welches ich mal mehr, mal weniger intensiv betreibe. Wie gut du in welcher Zeit werden kannst, lässt sich unmöglich abschätzen. Für Projekte benötigt man ja auch Zusatzwissen, wie hier zB ein Grundverständnis von HTML und von der BeautifulSoup-API. Und wenn der Code am Ende nicht wie Kraut und Rüben aussehen soll, ist natürlich eine gewisses Maß an (Python-)Programmiererfahrung von Vorteil. Aber wie gesagt: Es ist von Person zu Person total unterschiedlich, in welche Richtung man sich beim Programmieren entwickelt. Am meisten helfen Vorhaben, an denen man wirklich Spaß hat und wo somit die Motivation zum Ausprobieren und Dazulernen vorhanden ist.
Verbesserte Version:
- Mehrere Spritsorten zur Auswahl
- Uhrzeit/Datum der Abfrage wird gezeigt
- Anzeige auch von Tankstellen ohne aktuellen Preis (zB wenn nachts geschlossen)
Ich habe es mir um die GPS-Funktionalität von iOS (Pythonista) ergänzt und einen Kurzbefehl angelegt. Jetzt bekomme ich auf Knopfdruck alle Tankstellen/ Preise in meiner aktuellen Umgebung.
Statt die Konstante für den Ort komplett raus zu werfen, hier mit Konstante auf die zurückgefallen wird, wenn das `location`-Modul nicht importiert werden kann.
#!/usr/bin/env python3
import time
from datetime import datetime
import bs4
import requests
try:
import location # iOS Pythonista Modul
except ModuleNotFoundError:
location = None
LOCATION = 45886 # Ort oder PLZ
RADIUS = 2 # Umkreis in km
FUEL_TYPE = "E5" # Spritsorte
URL = "https://www.clever-tanken.de/tankstelle_liste"
FUEL_TYPES = {"diesel": 3, "super e5": 7, "super e10": 5, "super plus": 6}
def get_zip_code():
if location is None:
return LOCATION
location.start_updates()
time.sleep(1)
coord = location.get_location()
location.stop_updates()
return location.reverse_geocode(coord)[0]["ZIP"]
def get_header(fuel_type):
now = datetime.now()
text = f"Spritpreise ({fuel_type}) am {now:%d.%m.%Y} um {now:%H:%M} Uhr"
divider = "-" * len(text)
return f"{text}\n{divider}\n"
def convert_type(fuel_type):
if isinstance(fuel_type, int):
return fuel_type
for name, value in FUEL_TYPES.items():
if fuel_type.casefold() in (name, *name.split()):
return value
raise ValueError(f"Unknown type: {fuel_type!r}")
def iter_results(markup):
soup = bs4.BeautifulSoup(markup, "html.parser")
for result in soup.find_all(class_="list-card-container"):
price = result.find(class_="price").text.strip()
fuel_location = ", ".join(
result.find(class_=f"fuel-station-location-{typ}").text.strip()
for typ in ["name", "street", "city"]
)
yield price, fuel_location
def get_prices(location, radius, fuel_type):
query = dict(ort=location, r=radius, spritsorte=convert_type(fuel_type))
response = requests.get(URL, query)
response.raise_for_status()
return iter_results(response.text)
def main():
zip_location = get_zip_code()
print(get_header(FUEL_TYPE))
results = get_prices(zip_location, RADIUS, FUEL_TYPE)
for price, fuel_location in results:
print(f"{fuel_location} ({price} €)")
if __name__ == "__main__":
main()
Please call it what it is: copyright infringement, not piracy. Piracy takes place in international waters, and involves one or more of theft, murder, rape and kidnapping. Making an unauthorized copy of a piece of software is not piracy, it is an infringement of a government-granted monopoly.