1

I have been looking for a graceful way to dynamically format strings (URLs) in Python while looping... This is for when I want to request using different query parameters for each.

For example, this is what I've been resorting to at the moment (assuming I have 3 URLs):

for i in range(3):
    for num in range(0, 496, 5):
        if i == 1:
            requests.get('https://my-website.com?pricefrom={}&priceto={}'.format(num, num + 5))

        if i == 2:
            requests.get('https://my-website.com?qtyfrom={}&qtyto={}'.format(num, num + 5))

        # ......... :(

This is just ugly and I don't want to imagine what I'd do in the scenario where I have more links to request to.

Isn't there a solution simpler/more graceful like this for example:

urls = [<url1>, <url2>,....<url50>] # maybe each url has placeholders

for url in urls:
    # do some magic

Any help would be highly appreciated

4
  • you said different query params for each, can you post at least another variation of the url? thanks. Commented Jan 19, 2018 at 9:46
  • Why do you want to format strings when requests.get() let you pass the arguments as a dictionnary ? docs.python-requests.org/en/master/user/quickstart/… Commented Jan 19, 2018 at 9:57
  • Nice catch @brunodesthuilliers, but the thing is: a) I'm not always working with requests(I used it here for simplicity) b) Some of the urls have a lot more query params and I may need to change different parts of those which is why I prefer the string-formatting approach Commented Jan 19, 2018 at 13:11
  • @LexBryan Done .. Note: Slightly changed the first url as well Commented Jan 19, 2018 at 13:19

2 Answers 2

2

You can store strings with format in list as you said:

import requests

urls = ['https://example.com?from={fr}&to={to}',
        'https://example1.com?from={fr}&to={to}',
        'https://example2.com?from={fr}&to={to}'
]
for url in urls:
    for num in range(0, 496, 5):
        requests.get(url.format(fr=num, to=num + 5))

Also in python 3.6 you can use f-strings

Sign up to request clarification or add additional context in comments.

2 Comments

Hey @luminousmen I tried this (even after adding the nested loop you forgot) but it wasn't working. Syntax error in the assignment of from and to in the format... Luckily, with a little tinkering, i found the solution... Thanks a million though..
Sorry @EbJ, wrote on a phone) Fix implementation
0

Thanks to help from @luminousmen and the rest of you guys... I found a solution. Apparently you CAN just add placeholders and not do any operations on them :'D :

urls = ['https://example.com?from={}&to={}', 
        'https://example1.com?from={}&to={}',
        'https://example2.com?from={}&to={}']

for url in urls:
    for num in range(0, 496, 5):
        requests.get(url.format(num, num + 5))

Comments

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.