How to add variable to URL?

I’m trying to collect data from a website. I have an Excel file containing all the different extensions for the website. F.i. www.example.com/example2. I have a script that succesfully extracts the HTML from the site, but now I want to do it automatically for all the extensions. However, when I say:

 siteExtension = "example2" 
 url = ("https://www.example.com/siteExtension")
 r = requests.get(url)

instead of:

url = ("https://www.example.com/example2")
r = requests.get(url)

I get an error code. Do you guys have any suggestions how to do this? Thanks in advance!

Answer

You need to concatenate the value of siteExtension to your url string. You can do this in a number of ways, but here are the two most common:

url = "https://www.example.com/" + siteExtension

or, the more ubiquitous form:

url = "https://www.example.com/{}".format(siteExtension)

Leave a Reply

Your email address will not be published. Required fields are marked *