titusflex
titusflex

Reputation: 107

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!

Upvotes: 3

Views: 21008

Answers (3)

Carlos Parra
Carlos Parra

Reputation: 1067

You're having this error because you're not passing the value of siteExtension variable to the url, instead you are passing the siteExtension string.

You can fix it as follows:

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

Or using this way:

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

Upvotes: 1

Jacob See
Jacob See

Reputation: 775

You need to concatenate the strings together instead of making siteExtension a part of your string literal, like this:

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

Upvotes: 0

user2209008
user2209008

Reputation:

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)

Upvotes: 8

Related Questions