mardi 13 juillet 2021

For Loop and If Statement not performing as expected

Here's the code:

# Scrape table data
alltable = driver.find_elements_by_id("song-table")

date = date.today()
simple_year_list = []
complex_year_list = []
dateformat1 = re.compile(r"\d\d\d\d")
dateformat2 = re.compile(r"\d\d\d\d-\d\d-\d\d")
for term in alltable:
    simple_year = dateformat1.findall(term.text)
    for year in simple_year:
        if 1800 < int(year) < date.year: # Year can't be above what the current year is or below 1800,
            simple_year_list.append(simple_year)     # Might have to be changed if you have a song from before 1800
        else:
            continue
    complex_year = dateformat2.findall(term.text)
    complex_year_list.append(complex_year)

The code uses regular expressions to find four consecutive digits. Since there are multiple 4 digit numbers, I want to narrow it down to between 1800 and 2021 since that's a reasonable time frame. simple_year_list, however, prints out numbers that don't follow the conditions.

Aucun commentaire:

Enregistrer un commentaire