I have the below script to go through list of URLs, and return the response.status_code of the server.
The output is working fine, however I am trying to save the output into pandas dataframe.. which turns out to be empty for some reason.
Below is the code:
import requests
import webbrowser
import pandas as pd
import csv
import string
from time import sleep as time
df = pd.read_csv("status_url.csv")
#df.info()
#df.head()
df2 = pd.DataFrame() # creating new df to save the output of the loop below
df2['url_2'] = ''
df2['result'] = ''
def check(filename):
with open(filename, 'r') as f_in:
reader = csv.DictReader(f_in)
for row in df['Url']:
Url = df['Url']
try:
response = requests.get(row,timeout=3)
if(response.status_code) == 200:
print(row, response.status_code)
row = df2['url_2']
response.status_code = df2['result']
except requests.exceptions.HTTPError as errh:
row = df2['url_2']
df2['result'] = 'HTTP Error'
except requests.exceptions.ConnectionError as errc:
print (row,"Error Connecting: Failed to establish a new connection") # this print line works correctly
row = df2['url_2']
df2['result'] = "Error Connecting: Failed to establish a new connection"
except requests.exceptions.Timeout as errt:
row = df2['url_2']
df2['result'] = "Timeout Error"
except requests.exceptions.RequestException as err:
row = df2['url_2']
df2['result'] = 'OOps: Something Else'
df2.to_csv('URLs_Output.csv', sep=',', index=False) # this file is empty while it should not.
filename = 'status_url.csv'
check(filename)
Any ideas how to save the output to pandas as csv file? where it has two columns(row, which is the url) and the result(200, or error, etc)
Thank you