Ich schrieb einige Code mit einer for-Schleife. Jetzt möchte ich die for-Schleife stoppen, nachdem sie die Schleife einmal ausgeführt hat. Hier ist die for-Schleife:stoppe eine for-Schleife, Python
for table in tables: #reason for it to do it 12x
for tr in table.find_all("tr"):
firstTd = tr.find("td")
if firstTd and firstTd.has_attr("class") and "indent" in firstTd['class']:
values = {}
tds = tr.find_all("td")
maxVal = tds[1].find("span", class_="wx-value")
avgVal = tds[2].find("span", class_="wx-value")
minVal = tds[3].find("span", class_="wx-value")
if maxVal:
values['max'] = maxVal.text
if avgVal:
values['avg'] = avgVal.text
if minVal:
values['min'] = minVal.text
if len(tds) > 4:
sumVal = tds[4].find("span", class_="wx-value")
if sumVal:
values['sum'] = sumVal.text
scrapedData = {}
scrapedData[firstTd.text] = values
weatherdata.append(scrapedData)
with open ("january_2016.json", 'w') as outFile:
json.dump(weatherdata, outFile, indent=2)
Wie mache ich das?
Wenn Sie es nicht wollen, überhaupt zu wiederholen, warum gibt es eine Schleife? –