Open URL using variable or data file

Hi, i’m getting how to get this work.

I’m trying to open URL from a source file or a variable. but i don’t know how it works.

here’s a screenshot of the result i’m getting:

image


Thank you

nevermind, i found a solution from this post

image

thanks

Old style string concatenation can be used

id = “3333333”
url = “https://example.com/%s” % id
print url
https://example.com/3333333

The new style string formatting:

url = “https://example.com/{0}”.format(id)
print url
https://example.com/3333333

The reading for file as mentioned by avasal with a small change:

f = open(‘file.txt’, ‘r’)
for line in f.readlines():
id = line.strip(’\n’)
url = “https://example.com/{0}”.format(id)
urlobj = urllib.urlopen(url)
try:
json_data = json.loads(urlobj)
print json_data
except:
print urlobj.readlines()

1 Like