I have a list of IDs that I need to pass into an API.
Succesfully, I made the IDs into a url string, and I have a list of ~300k urls(~300K IDs)
I want to get the text part of each api call back and in alist.
I can do this by taking every ID and passing it into the URL using a for loop like so without iterating through a list:
L = [1,2,3]
for i in L:
#print (row)
url = 'url&Id={}'.format(i)
xml_data1 = requests.get(url).text
lst.append(xml_data1)
time.sleep(1)
print(xml_data1)
I have been trying to use concurrent.futures
and urllib.request
and library to send multiple requests at once but I keep getting error:
username=xxxx&password=xxxx&Id=1' generated an exception: 'HTTPResponse' object has no attribute 'readall'
using this code:
lst = [url.com,url2.com]
URLS = lst
# Retrieve a single page and report the url and contents
def load_url(url, timeout):
conn = urllib.request.urlopen(url, timeout=timeout)
return conn.readall()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
# do json processing here
except Exception as exc:
print('%r generated an exception: %s' % (url, exc))
else:
print('%r page is %d bytes' % (url, len(data)))
How can I adapt the for loop I have or the code above to make multiple API calls at once?
I am asking because my connection keeps getting reset with the for loop, and I dont know how to continue where I left off in terms of either ID or url.
Using python3.6
Edit:
I applied the code from here Python requests with multithreading
where lst is the list of urls.
class Test:
def __init__(self):
self.urls = lst
def exception(self, request, exception):
print ("Problem: {}: {}".format(request.url, exception))
def async(self):
results = grequests.map((grequests.get(u) for u in self.urls), exception_handler=self.exception, size=5)
print (results)
test = Test()
test.async()
The code seems to be working no error message given, but how do I append from within the code the response.text into a list?