Reputation: 107
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
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
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
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