Check out Kite (free AI Coding Assistant) → Link

Buy Me a Coffee? Your support is much appreciated!

PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn

Source Code:

from pprint import pprint
import datetime
import requests # pip install requests
from bs4 import BeautifulSoup # pip install beautifulsoup4
import pandas as pd # pip install pandas

url = "https://www.marketwatch.com/tools/screener/after-hours?mod=investing"
headers= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0'}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')

dfs = {}

screener_tables = soup.find_all('div', {'class': 'element element--table table--fixed screener-table'})

xlsxwriter = pd.ExcelWriter('After Hour Screener Tables.xlsx')

for screener_table in screener_tables:
    screener_name = screener_table.h2.text
    screener_table = screener_table.find('table')
    df = pd.read_html(str(screener_table))[0]
    df['Symbol Symbol'] = df['Symbol Symbol'].str.replace(
    r'\b(.+)(\s+\1)+\b',
    r'\1')

    df.rename({'Symbol Symbol': 'Symbol'}, axis=1)
    dfs[screener_name] = df
    df.to_excel(xlsxwriter, sheet_name=screener_name, index=False)

xlsxwriter.save()