Zwei dict zusammenführen
Verfasst: Dienstag 12. Oktober 2021, 12:39
Hallo liebe Leute, ich möchte von eine Webseite mit beautifoulsoup die Daten ein (pro Table ein dict mit ca. 168 Datensätzen, Table1/dict1 = Länder, Table2/dict2 = Preise, 1xEur, 1xUSD) einlesen. Gelingt ja auch erstmal.
Nun möchte ich beide myDict (1 und 2) zusammenführen. Was ich auch mache, das gelingt mir nicht. Kann mir jemand bitte helfen. Bitte nicht meckern. Ich bin noch sehr neu und suche sehr viel selbst. Vielen Dank.
Nun möchte ich beide myDict (1 und 2) zusammenführen. Was ich auch mache, das gelingt mir nicht. Kann mir jemand bitte helfen. Bitte nicht meckern. Ich bin noch sehr neu und suche sehr viel selbst. Vielen Dank.
Code: Alles auswählen
from bs4 import BeautifulSoup
import requests
import csv
url = f'https://de.globalpetrolprices.com/gasoline_prices/'
url2 = f'https://www.finanzen.net/waehrungsrechner/us-dollar_euro'
output_filepath = '/home/Energiepreis.csv'
daten = requests.get(url)
soup = BeautifulSoup(daten.content, 'html.parser')
table = soup.find('div', attrs={'id': 'outsideLinks'})
table2 = soup.find('div', attrs={'id': 'graphic'})
daten2 = requests.get(url2)
soup2a = BeautifulSoup(daten2.content, 'html.parser')
soup2b = BeautifulSoup(daten2.text, 'html.parser')
kurs_name = soup2b.find_all('span', attrs={'id': 'currency-second-display'})
kurs_name2 = soup2a.find_all('span', attrs={'id': 'currency-second-display'})
kurs_zeile = soup2b.find('span', attrs={'id': 'currency-second-display'})
kurs = kurs_zeile.text.split(" ",4)
a = kurs[3].replace(',','.')
summe_1 = float(a)
with open(output_filepath, 'w', encoding='utf-8') as csv_schreib_datei:
writer = csv.writer(csv_schreib_datei)
writer.writerow(['Land','USD', 'EUR'])
myDict0 = {}
a = 1
for i in table.find_all('a'):
link_name = (i.get('href')).replace('gasoline_prices/','').replace('/','')
list0 = [link_name]
myDict1 = {a: link_name}
print(myDict1)
a = a + 1
a = 1
for x in table2.find_all('div', attrs={'style': 'position: absolute; top: 2px; left: 7px; height: 15px; color: #000000;'}):
myDict2 = {}
preis_name = (x.text)
preis_name = str(preis_name)
preis_eur = float(preis_name)/ float(a)
list1=[str(preis_name),str(preis_eur)]
#data1 = [str(link_name), str(preis_name), str(preis_eur)]
myDict2 = {a: [str(preis_name),str(preis_eur)]}
a = a + 1
def merge_two_dicts(x, y):
z = myDict2.copy() # start with keys and values of x
z.update(myDict1) # modifies z with keys and values of y
return z
print(merge_two_dicts(myDict1, myDict2))