Created
January 31, 2022 08:25
-
-
Save arunelias/6479336f9d9b0cd7da49c2950067c2df to your computer and use it in GitHub Desktop.
An efficient Link Validator written in Python using the Python modules Pandas and Requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas | |
import requests | |
import time | |
# Custom headers in key value pair | |
headers = {'user-agent': 'Link-Validator/0.0.1'} | |
# Create a session | |
session = requests.Session() | |
# Throttle requests/ min | |
throttle = 30 | |
# DataFrame.apply subroutine | |
def get_url_for_status(row): | |
# access global varaiables | |
global session, headers, throttle | |
# get URL from URL column | |
url = row['URL'] | |
# Handle common Erros | |
if url == "": | |
return | |
if url.startswith('#'): | |
return row.append(pandas.Series(dict([('Request Error', '# Link')]))) | |
if not url.startswith('http'): | |
return row.append(pandas.Series(dict([('Request Error', 'Invalid Link')]))) | |
# Handle other errors using Try Catch | |
try: | |
start = time.time() | |
# Streaming request: using a 'with' statement to ensure it's always closed | |
with session.get(url, headers=headers, stream=True, timeout=30, verify=False) as r: | |
# Access data if required, the data is transfered when r.content is accessed | |
#output_value = r.content | |
final_url = r.url | |
code = r.status_code | |
stop = time.time() | |
print("Status: {0} Time: {1} \nFinal URL: {2}".format(code, (stop-start), final_url)) | |
# Throttle the requests | |
if ((stop-start) < (60/throttle)): | |
time.sleep((60/throttle)-(stop-start)) | |
# Return Status | |
return row.append(pandas.Series(dict([('Response Code', code), ('Response Time', (stop-start)), ('Final URL', final_url)]))) | |
except Exception as e: | |
#raise e # Raise exception for debugging | |
return row.append(pandas.Series(dict([('Request Error',str(e))]))) | |
# Read URLs from Excel file | |
df = pandas.read_excel("D:\\Selenium\\Link_Validator.xlsx",sheet_name="Links") | |
# Call the DataFrame.apply subroutine | |
df = df.apply(get_url_for_status, axis=1) | |
# Write status as Excel file | |
with pandas.ExcelWriter("D:\\Selenium\\Link_Status.xlsx") as writer: | |
df.to_excel(writer, sheet_name="Link Status", index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment