osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generator - Provide parameters to url - requests


Hi

I am struggling to figure out how I can create a generator to provide values to my url. My url needs to insert the year month and day in the url not as params to the url.


import json
import requests
import datetime

# using this I can create a list of dates for the first 210 days of this year.

base = datetime.datetime(2017,1,1)

def datesRange(max, min):
    date_list = (base - datetime.timedelta(days=x) for x in range(max,min))
    DAY = date_list.day
    MONTH = date_list.month
    YEAR = date_list.year
    yield DAY, MONTH, YEAR

dateValues = datesRange(-210,0)
    
def create_url(day, month, year):
   https://api.tatts.com/sales/vmax/web/data/racing/{0}/{1}/{2}/sr/full".format(YEAR,MONTH,DAY)
    return url

Then I want to insert them in this url one at a time from the generator

try:
   r = requests.get(INSERT_URL_HERE)
   if r.status_code == requests.codes.ok then:
      # open a file for writing using url paramters
      with open(SR + DAY + MONTH + YEAR + '.json', 'w') as f:
      # Do stuff from here not relevant to question.

I have just gotten lost.

Is there an easier way to go about this?

Cheers

Sayth