Reputation: 109
I am trying to iterate over a list of URI's that I'm pulling from a CSV. I would appear that requests
cannot use a variable in the URL string, but I wanted to check and see if anyone had any thoughts on how to make something like this work.
with open(fwinfo) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
try:
rkey = requests.get('https://'row['ip_address'])
if rkey.status_code == 200:
Upvotes: 0
Views: 1123
Reputation: 4445
I think what you want here is:
rkey = requests.get('https://{}'.format(row['ip_address']))
Upvotes: 0
Reputation: 1124988
You are getting a SyntaxError
, which means you are getting your Python syntax wrong; this is not the fault of the requests
library.
You need to use string concatenation or string formatting here; you cannot just place a variable after a string.
+
concatenates strings:
rkey = requests.get('https://' + row['ip_address'])
or you can use str.format()
to insert your CSV value into a string:
rkey = requests.get('https://{}'.format(row['ip_address']))
Take into account that HTTP servers often serve more than one website from a given IP address; different sites are served based on the Host
header. Take that into account when using just IP addresses, you may have to manually add the host.
Upvotes: 1