I am trying to use a index or list: vItems = ["Yew","Magic","Maple"] To populate dictionaries by those names (Yew,Magic,Maple). I have 8 variables that update every loop, and I would like to fill in 8 dictionary items to those 3 dictionaries (24 total). To avoid having 24 lines of code, I'd rather try to integrate a for loop to reduce it to 8 lines. Here is what I have so far:
import requests
import bs4
print("Saplings script running...")
#Pulls all GE prices
vUrl = "https://prices.runescape.wiki/api/v1/osrs/1h"
headers = {
'User-Agent': 'Calculating profit/hr methods',
'From': ''
}
res = requests.get(vUrl , headers = headers)
soup = bs4.BeautifulSoup(res.text, 'lxml')
vText = str(res.text)
#vItems = [["Yew",5315,5373],["Magic",5316,5374],["Mahogany",21488,21480],["Maple",5314,5372],["Dragonfruit",22877,22866],["Palm",5289,5502],["Papaya",5288,5501],["Celastrus",22869,22856]]
Yew = {"SeedID":5315,"SaplingID":5373}
Magic = {"SeedID":5316,"SaplingID":5374}
Maple = {"SeedID":5314,"SaplingID":5372}
vItems = ["Yew","Magic","Maple"]
#Searches large string of text for dictionary related to item ID
vStart = vText.index("\"" + str(Yew['SeedID']) + "\"")
vStart = vText.index("{",vStart)
vEnd = vText.index("}",vStart) + 1
vSeed = eval(vText[vStart:vEnd])
#Searches large string of text for dictionary related to item ID
vStart = vText.index("\"" + str(Yew['SaplingID']) + "\"")
vStart = vText.index("{",vStart)
vEnd = vText.index("}",vStart) + 1
vSapling = eval(vText[vStart:vEnd])
# Populates Yew dictionary from values pulled online
vTest = "Yew"
Yew['Seed_avgHighPrice'] = vSeed['avgHighPrice']
Yew['Seed_highPriceVolume'] = vSeed['highPriceVolume']
Yew['Seed_avgLowPrice'] = vSeed['avgLowPrice']
Yew['Seed_lowPriceVolume'] = vSeed['lowPriceVolume']
Yew['Sapling_avgHighPrice'] = vSapling['avgHighPrice']
Yew['Sapling_highPriceVolume'] = vSapling['highPriceVolume']
Yew['Sapling_avgLowPrice'] = vSapling['avgLowPrice']
Yew['Sapling_lowPriceVolume'] = vSapling['lowPriceVolume']
# Prints results
print(Yew)
print(Yew['Seed_avgHighPrice'], Yew['Seed_highPriceVolume'], Yew['Seed_avgLowPrice'], Yew['Seed_lowPriceVolume'])
It only populates the data for Yew currently, but I would like to replace the Yew['asdf'] with a for loop. Something like:
for k in vItems:
'k'['asdf'] = vSeed['asdf']