首页 > 解决方案 > For 循环和 If 语句未按预期执行

问题描述

这是代码:

# 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)

该代码使用正则表达式来查找四个连续的数字。由于有多个 4 位数字,我想将其缩小到 1800 到 2021 之间,因为这是一个合理的时间范围。然而,simple_year_list 会打印出不符合条件的数字。

标签: regexdatetimefor-loopif-statement

解决方案


您可以在正则表达式中完成所有操作。

添加开始^和结束$锚点,并通过模式限制范围:

dateformat1 = re.compile(r"^(1[89]\d\d|20([01]\d|2[01]))$")

推荐阅读