This is my code to access a webpage but I need to add parameters to it: 1. First parameter is added by reading a line from file 2. Second parameter is a counter to continuously access pages
import urllib2
import json,os
f = open('codes','r')
for line in f.readlines():
id = line.strip('\n')
url = 'http://api.opencorporates.com/v0.2/companies/search?q=&jurisdiction_code=%s&per_page=26¤t_status=Active&page=%d'
i = 0
directory = id
os.makedirs(directory)
while True:
i += 5
req = urllib2.Request('%s%s%d' % (url,id, i))
print req
try:
response = urllib2.urlopen('%s%s%d' % (url, id, i))
except urllib2.HTTPError, e:
break
content = response.read()
fo = str(i) + '.json'
OUTFILE = os.path.join(directory, fo)
with open(OUTFILE, 'w') as f:
f.write(content)
This keeps creating empty directories. I know something is wrong with the url parameters. How to rectify this?
Request. Off the top of my head, the string format looks wrong. Put the url you're requesting into a variable and print that and see what it looks like.