Skip to content

Instantly share code, notes, and snippets.

@marcogoldin
Last active December 15, 2023 18:47
Show Gist options
  • Save marcogoldin/f42b74ec777dd685230babca0473c6c0 to your computer and use it in GitHub Desktop.
Save marcogoldin/f42b74ec777dd685230babca0473c6c0 to your computer and use it in GitHub Desktop.
import logging
# Create a custom logger
logger = logging.getLogger(__name__)
# Create a handler
f_handler = logging.FileHandler('file.log')
# Create a formatter and add it to the handler
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
f_handler.setFormatter(f_format)
# Add the handler to the logger
logger.addHandler(f_handler)
for el in my_list:
try:
url = "https://your-website.com?param="+url_param
response = requests.get(url)
response.raise_for_status()
html_content = response.content
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table', {'class': 'css-class'})
except requests.exceptions.HTTPError as http_err:
logger.error(f'HTTP error occurred: {http_err}')
except Exception as err:
logger.error(f'Other error occurred: {err}')
else:
if table is not None:
try:
# html table wrangling, i.e. create a pandas dataframe
pass
except Exception as e:
logger.error(f'Error occurred while creating dataframe from table: {e}')
else:
logger.info(f'No table found for el: {el}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment