URL日期迭代变更的逻辑构建技术问题咨询
Hey there! Let's work through fixing your date iteration and URL generation logic. I see you're aiming to loop through every single day from 2016 to 2020 and plug each date into that Uber Movement URL—solid goal, but your current code has a few kinks we can iron out.
First, let's spot the issues in your current code:
- Index errors: When you do
YYYY[i],iis the year value (like 2016), not a list index. Same goes forMM[j]andDD[k]—you're trying to use the month/day values as indices, which will throw anIndexErrorright away. - Invalid dates: Your code loops days 1-31 for every month, but not all months have 31 days (looking at you, February, April, June, etc.). This would generate broken dates like
2016-02-31. - Loop structure: Your
datevariable andprint(date)line are outside the nested loops, so they'll only run once (and crash before that due to the index issue).
The cleanest solution: Use Python's datetime module
The datetime library handles all the messy date logic (like leap years, varying month lengths) automatically, so you don't have to reinvent the wheel. Here's a revised version of your code:
from datetime import datetime, timedelta # Set your date range start_date = datetime(2016, 1, 1) end_date = datetime(2020, 12, 31) current_date = start_date while current_date <= end_date: # Format the date as YYYY-MM-DD (matches the URL's required format) date_str = current_date.strftime("%Y-%m-%d") # Build the URL with f-strings for readability url = f'https://movement.uber.com/explore/atlanta/travel-times/query?si=1074&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]={date_str}&dt[dr][ed]={date_str}&cd=&sa;=&sdn=&lang=en-US' print(url) # Jump to the next day current_date += timedelta(days=1)
Why this works better:
- No more invalid dates:
datetimeautomatically skips impossible dates (like February 31) and handles leap years (e.g., 2016's February has 29 days). - Simpler logic: No nested loops to manage—just a single while loop that increments the day each time.
- Clean formatting:
strftime("%Y-%m-%d")ensures months and days are two digits (e.g.,01instead of1), which matches the URL's expected date format.
If you prefer to stick with nested loops (for learning purposes):
If you want to build the date logic manually, you'll need to account for varying month lengths and leap years. Here's how to adjust your code:
# Define the range of years years = range(2016, 2021) # Base days per month (index 0 unused, indexes 1-12 = months 1-12) days_per_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for year in years: # Adjust February days for leap years if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): days_per_month[2] = 29 else: days_per_month[2] = 28 for month in range(1, 13): # Get the correct number of days for the current month max_days = days_per_month[month] for day in range(1, max_days + 1): # Format date with leading zeros for months/days date_str = f"{year}-{month:02d}-{day:02d}" # Build the URL url = f'https://movement.uber.com/explore/atlanta/travel-times/query?si=1074&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]={date_str}&dt[dr][ed]={date_str}&cd=&sa;=&sdn=&lang=en-US' print(url)
Key fixes here:
- We use the
year,month, anddayvariables directly (no need to store them in lists first—your original list creation steps were redundant). - We adjust February's day count based on whether the year is a leap year.
- We format months and days to two digits with
:02dto match the URL's date format.
Either approach will get you the daily URLs you need, but the datetime method is far less error-prone. Give it a try!
内容的提问来源于stack exchange,提问作者Luiz Scheuer

