2

I've done some searches with this and haven't quite found something that would be applicable for what I need to do. Had this been PHP, I think I might have figured it out. But I am completely new to Python.

I have a crawling script where I want the user to be able to input a year and a month. The script would then loop through each month (if providing more than one) and then each year (if providing more than one). The two variables FiledStartDate and FiledEndDate MUST exist, as they are sent via payload in the post request.

I could get this to work using single years and single months. However, I need to go beyond just single year and month.

Note: I didn't forget the Leap Year. If someone could include that, it would be great. Otherwise, I think with a working code, I can figure out how to deal with that.

Obviously, the below example code doesn't work. But hopefully it demonstrates what I want to do.

Thanks in advance

years = '2020'
months = ['01', '02', '03']

for year in years:
    for month in months:
        if month == '01':
            FiledStartDate = year + '-01-01'
            FiledEndDate = year + '-01-31'

        if month == '02':
            FiledStartDate = year + '-02-01'
            FiledEndDate = year + '-02-28'

        if month == '03':
            FiledStartDate = year + '-03-01'
            FiledEndDate = year + '-03-31'

        if month == '04':
            FiledStartDate = year + '-04-01'
            FiledEndDate = year + '-04-30'

    do(something)
redo(something)
4
  • What is something? Could you give an example of what you are trying to do with month and year (whether one or many) ? Commented Jun 7, 2021 at 13:44
  • Do you want to get all dates between 2 specific days Commented Jun 7, 2021 at 13:45
  • @Sujay No. I want it to include an entire month. One, I don't want to make too many requests via the scraper. So FiledStartDate has to be the first of the month, and FiledEndDate would be the last of the month. %Y-%m-%d format. So, no. anything dealing with time delta would be irrelevant...I think. lol Commented Jun 7, 2021 at 13:51
  • @shahkalpesh Sorry. I thought I was clear on that. It's a scraper. The post request payload has to include those two variables. And for each month and year the user wants, it has to update that post payload with those two date variables. FWIW, the link to the scraper is here. Commented Jun 7, 2021 at 13:56

1 Answer 1

2

Of course you will need to account for incorrect input, if such occurs (exceptions), otherwise simple solution

from datetime import datetime, timedelta
years = ['2020']
months = ['01', '02', '03']

for year in years:
    for month in months:
        FiledStartDate = datetime(int(year), int(month), 1)
        FiledEndDate   = datetime(int(year), int(month) + 1, 1) - timedelta(1)
        print(FiledStartDate, FiledEndDate)
...
2020-01-01 00:00:00 2020-01-31 00:00:00
2020-02-01 00:00:00 2020-02-29 00:00:00
2020-03-01 00:00:00 2020-03-31 00:00:00
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. I'll try that. Looks like maybe the original code I provided was closer than I thought.
Also, on a funny note about incorrect input. If do this right, the accounting would be that the crawler will break since it would spit out the wrong payload in the post request. The payload is already coded with those two variables and the values imported from a cfg file. And then the website will let the user know they done didn't follow simple directions. lol
One thing that I'll have to modify from your response. I don't need the hours.
Sure, just do print(FiledStartDate.strftime("%Y-%m-%d"), FiledEndDate.strftime("%Y-%m-%d"))
StartDate = datetime(int(year), int(month), 1) , FiledStartDate = StartDate.strftime("%Y-%m-%d") and then print(FiledStartDate). IMO cleaner for the end code in the scraper (which is already a jumbled mess lol).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.