How would I increment the numerical portion of a specific parameter in a URL string?

Tags: , , , ,



Here’s my specific example:

param1 = 'XXXXXXXXXXX'
param2 = 'XXXX'
param3 = 'XXXX'
param4 = 'XXXX'
param5 = 'XXXX'
param6 = 'XXXX'
#param7 below holds the (always numerical) value that needs to be incremented so that param7 = '2' in the next pass through
param7 = '1' 

#url of fantasy league with statistics
url = ('https://www.fantrax.com/fantasy/league/'+param1+'/players'
   +';positionOrGroup='+param2
   +';miscDisplayType='+param3
   +';seasonOrProjection=SEASON_9'+param4
   #so on and so forth
   +';parameterThatNeedsIncremented'+param7)

browser.get(url)

I need the numerical value of param7 in this example to increase by a count of 1 before each pass up to param7 = ’30’.

Is there any way to create a list or dict containing values ‘1’ through ’30’ and tell param7 to use use move through the dict at index + 1?

Answer

You can use a for loop with a range() function which returns a sequence of numbers, starting from first number specified, incrementing by 1 (by default), and stoping before second specified number.

param1 = 'XXXXXXXXXXX'
param2 = 'XXXX'
param3 = 'XXXX'
param4 = 'XXXX'
param5 = 'XXXX'
param6 = 'XXXX'

for param7 in range(1, 31):
    param7 = str(param7) # If it needs to be string
    #url of fantasy league with statistics
    url = ('https://www.fantrax.com/fantasy/league/'+param1+'/players'
        +';positionOrGroup='+param2
        +';miscDisplayType='+param3
        +';seasonOrProjection=SEASON_9'+param4
        #so on and so forth
        +';parameterThatNeedsIncremented'+param7)

    browser.get(url)


Source: stackoverflow