I am a few days into python and pandas and I am running into a situation that I can not seem to resolve on my own. I have a for loop to fetch status codes and print out the results if they meet certain criteria. The for loop I have is as followed:
For loop:
import requests
from requests.exceptions import HTTPError
response_full_result = []
for url in url_list:
try:
response = requests.get(url)
response_full_result.append(response)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
failed_result.append(http_err)
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
else:
print('Success!')
What this for loop does is, it iterates through a column on a .csv and executes a get call to fetch status codes. Now, what this also does is print out exceptions as specified in the order they are executed. For example, if first three rows of column are: 200,400,NaN -, result will be: success, HTTP error, and Other Error (respectively)
Desired Result: I understand by design it is printing as expected - what I would like is for all the outputs to be stored in a variable / List that I can work with later. i.e. success, HTTP error, Other Error - Is there a way to do this? I tried append method, and pickle means I will have to convert to dictionary which is not ideal. Is there a way to do this in Python or Pandas?
Also, thanks to this doc for providing for loop - it is not mine. I am using PyCharm on Python 3.9. I am new to this and just started last week, I have found a lot of things but was unable to find an answer that helped me in my particular situation. Maybe I missed this - apologies.
Thank you to anyone who can help and give suggestions !
