0

I would need to run the code iterating the scraping in a date range. Currently I am doing it manually as follows:

start_date=input("Enter start date: mm/dd/yyyy ") 
end_date=input("Enter end date: mm/dd/yyyy ") 
query=input("Enter query: ")
print("\n")

driver = webdriver.Chrome('/path')
driver.get('path’+‘cd_min=start_date&cd_max=end_date’)

The link above might be slightly different. I would like to insert the start date manuanlly, then iterate with a while or for loop the functions through time, at step of 1 (start_date+1, start_date+2, and so on) from a start_date to an end_date chosen at the beginning using input as well).

Could you please tell me how I can do that?

still_learning
  • 776
  • 9
  • 32
  • Does this answer your question? [Iterating through a range of dates in Python](https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python) – pasbi Apr 02 '20 at 10:55

1 Answers1

1

As far as I understand you need to create a range of dates that you can iterate over. That could be accomplished by this using pandas. Assuming your input is correct you could do the following.

import pandas as pd
from datetime import datetime, timedelta

start_date=input("Enter start date: mm/dd/yyyy ") 
end_date=input("Enter end date: mm/dd/yyyy ") 

datelist = pd.date_range(start=start_date, end=end_date).to_pydatetime().tolist()
for i, _ in enumerate(datelist):
    if i <len(datelist):
        start_date = datelist[i].strftime('%m/%d/%Y')
        end_date = datelist[i+1].strftime('%m/%d/%Y')

        driver = webdriver.Chrome('/path')
        driver.get('path'+f'cd_min={start_date}&cd_max={end_date}')
Philip
  • 944
  • 11
  • 26