Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I automatically change a part of a url to query a website a set number of times in python?

I have very basic knowledge of python, so sorry if my question sounds dumb.

I need to query a website for a personal project I am doing, but I need to query it 500 times, and each time I need to change 1 specific part of the url, then take the data and upload it to gsheets.

(The () signifies what part of the url I need to change)
https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=(symbol)&apikey=apikey’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I thought about using while and format {} to do it, but I was unsure how to change the string each time, bar writing out the names for variables by hand (defeating the whole purpose of this).

I already have a list of the symbols I need to use, but I don’t know how to input them

Example of how I get 1 piece of data

import requests
url = 'https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=MMM&apikey=demo'
r = requests.get(url)
data = r.json()

Example of what I’d like to change it to

import requests
url = 'https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=AOS&apikey=demo'
r = requests.get(url)
data = r.json()

#then change it to

import requests
url = 'https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=ABT&apikey=demo'
r = requests.get(url)
data = r.json()

so on and so forth, 500 times.

>Solution :

You might combine .format with for loop, consider following simple example

symbols = ["abc","xyz","123"]
for s in symbols:
    url = 'https://www.example.com?symbol={}'.format(s)
    print(url)

output

https://www.example.com?symbol=abc
https://www.example.com?symbol=xyz
https://www.example.com?symbol=123

You might also elect to use any other way of formatting, e.g. f-string (requires python3.6 or newer) in which case code would be

symbols = ["abc","xyz","123"]
for s in symbols:
    url = f'https://www.example.com?symbol={s}'
    print(url)

Alternatively you might params optional argument of requests.get function as follows

import requests
symbols = ["abc","xyz","123"]
for s in symbols:
    r = requests.get('https://www.example.com', params={'symbol':s})
    print(r.url)

output

https://www.example.com/?symbol=abc
https://www.example.com/?symbol=xyz
https://www.example.com/?symbol=123
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading