mardi 30 mars 2021

IF/Else statements with web scraping python

I am currently trying to scrape the amount of snow from a (saved) weather forecast page, the page is from weather underground, which is okay to scrape. Unfortunately the amount of precipitation and the type of precipitation are in two separate lines so I am trying to write and if/else statement that takes the list of types of precipitation I scrapped and if the type of precipitation is "Snow" than it will scrape and append the amount of precipitation to another list. My issue isn't so much the scrapping part as I have found the appropriate nodes/code to create full individual lists for both, but I don't know the correct syntax for how to append a value to a list, given an index position in a different list.

My code is as followed:

# To obtain the list of precipitation types:

[IN]:
precip = []
i = 0

for n in range(0,10):
    precip.append(forcastfun.find_all('div', {'class':'obs-precip'})[n].find('title').get_text())
    i += 1

print(precip)

[OUT]:
['Precip', 'Precip', 'Precip', 'Precip', 'Precip', 'Precip', 'Precip', 'Snow', 'Snow', 'Precip']

# Now I want to iterate through this list and any position it says snow, I then want it to scrape the amount of precipitation for that position and append it to a new list so far I have

[IN]:
amt = [ ]
i = 0

for n in precip:
     if n == "Snow"
     amt.append(forcastfun.find_all('div', {'class':'obs-precip'})[n].find('span', {"_ngcontent-app-root-c236":""}).get_text())
     i  +=0

[OUT]:

TypeError: list indices must be integers or slices, not str

Can someone help me correct this so that the the statement will go ahead and get the amount of snow for index position 7 and 8? As this is meant to work for any time I scrape the forecast.

Thank you

Aucun commentaire:

Enregistrer un commentaire