from datetime import datetime, timedelta, date
import webbrowser

# Get the start and end dates from the user
start_date_str = input("Enter the start date (YYYY-MM-DD): ")
end_date_str = input("Enter the end date (YYYY-MM-DD): ")

# Convert user input to datetime objects
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
end_date = datetime.strptime(end_date_str, "%Y-%m-%d")

# Loop through the date range and open URLs in the web browser
current_date = start_date
while current_date <= end_date:
    # Check if the day is Saturday (5) or Sunday (6)
    if current_date.weekday() == 5 or current_date.weekday() == 6:
        # Check if the month is October or November
        if current_date.month not in [10, 11]:
            # Skip weekends in other months
            current_date += timedelta(days=1)
            continue

    date_str = current_date.strftime("%d%b%Y").upper()
    year = current_date.strftime("%Y")
    month = current_date.strftime("%b").upper()

    # Generate the dynamic URL
    url = f"https://nsearchives.nseindia.com/content/historical/EQUITIES/{year}/{month}/cm{date_str}bhav.csv.zip"

    # Open the URL in the web browser
    webbrowser.open(url)

    # Move to the next date
    current_date += timedelta(days=1)

CSV_DB/bulk_download.py
gmmkmtgk/GM_Stocks
import pandas as pd
import os
from datetime import datetime

# Directory containing the CSV files
directory = 'csv_files'

# Initialize a flag to check if we have processed any files
processed_first_file = False

# Iterate through files in the directory
for filename in os.listdir(directory):
    if filename.endswith(".csv"):
        file_path = os.path.join(directory, filename)
        
        # Extract the date part from the filename
        date_str = filename[2:-8]  # Extracts the '01SEP2023' part
        date_obj = datetime.strptime(date_str, "%d%b%Y")
        
        # Read the CSV file into a DataFrame
        df = pd.read_csv(file_path)
        
        # Filter rows where 'series' is 'EQ'
        df = df[df['SERIES'] == 'EQ']
        
        # Extract the 'SYMBOL' and 'CLOSE' columns and rename 'CLOSE' to the date
        df = df[['SYMBOL', 'CLOSE']]
        df.rename(columns={'CLOSE': date_obj.strftime("%Y-%m-%d")}, inplace=True)
        
        if not processed_first_file:
            merged_df = df
            processed_first_file = True
        else:
            # Merge the current DataFrame with the merged DataFrame on the 'SYMBOL' column
            merged_df = pd.merge(merged_df, df, on='SYMBOL', how='outer')

# Replace NaN values with an empty string
merged_df.fillna('', inplace=True)

# Save the merged DataFrame to a CSV file
merged_df.to_csv('merged_output.csv', index=False)

CSV_DB/data_manage.py
gmmkmtgk/GM_Stocks
import pandas as pd

# Load the merged CSV file
df = pd.read_csv('merged_output.csv')

# Sort the columns (dates) in ascending order
date_columns = sorted(df.columns[1:], key=lambda x: pd.to_datetime(x))
sorted_columns = ['SYMBOL'] + date_columns
df = df[sorted_columns]

# Save the sorted DataFrame to a new CSV file
df.to_csv('sorted_merged_output.csv', index=False)

CSV_DB/file_sort.py
gmmkmtgk/GM_Stocks
import wget

# URL of the ZIP file
url = "https://nsearchives.nseindia.com/content/historical/EQUITIES/2022/SEP/cm15SEP2022bhav.csv.zip"

try:
    # Download the file to the current working directory
    downloaded_file = wget.download(url)
    print(f"File '{downloaded_file}' downloaded successfully.")
except Exception as e:
    print(f"Failed to download the file: {str(e)}")

CSV_DB/test.py
gmmkmtgk/GM_Stocks
import os
import zipfile

# Define the paths to the input and output folders
input_folder = 'zip_files'
output_folder = 'csv_files'

# Ensure the output folder exists, create it if necessary
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# Loop through all files in the input folder
for filename in os.listdir(input_folder):
    if filename.endswith('.zip'):
        # Create a full path to the input ZIP file
        input_file_path = os.path.join(input_folder, filename)

        # Extract the filename without the extension
        base_filename = os.path.splitext(filename)[0]

        # Create a full path to the output CSV file
        output_file_path = os.path.join(output_folder, base_filename + '.csv')

        try:
            # Extract the ZIP file
            with zipfile.ZipFile(input_file_path, 'r') as zip_ref:
                zip_ref.extractall(output_folder)
            print(f"Extracted '{filename}' to '{output_file_path}'")
        except Exception as e:
            print(f"Error extracting '{filename}': {str(e)}")

print("Extraction completed.")

CSV_DB/zip_to_csv.py
gmmkmtgk/GM_Stocks
import csv
import os


input_file = os.path.join('..', 'CSV_DB', 'sorted_merged_output.csv')
output_file = os.path.join('..', 'stock_data.csv')   # Use raw string literal for the output file path

with open(input_file, mode='r', newline='') as infile, open(output_file, mode='w', newline='') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)

    for row in reader:
        global_checking_flag = False
        global_checking_flag2 = False

        if all(row):
            writer.writerow(row)
        else:
            for value in row[1:]:
                if value == "" and global_checking_flag == False:
                    continue
                elif value == "" and global_checking_flag == True:
                    global_checking_flag2 = True
                else:
                    global_checking_flag = True

            if global_checking_flag == True and global_checking_flag2 == False:
                writer.writerow(row)

Data_Cleaner/clean_data.py
gmmkmtgk/GM_Stocks
from tkinter import *

def convert_number_to_words(n, country="US"):
    if country == "US":
        return convert_number_to_words_us(n)
    elif country == "India":
        return convert_number_to_words_in(n)
    
    
def convert_number_to_words_us(n):
    units = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    teens = ["", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    thousands = ["", "thousand", "million", "billion"]
    
    def rec_convert(num, idx):
        if num == 0:
            return ""
        
        words = []
        
        if num > 99:
            words.append(units[num // 100])
            words.append("hundred")
            num %= 100
        
        if num > 10 and num < 20:
            words.append(teens[num-10])
        else:

            if num >= 20 or num == 10:
                words.append(tens[num // 10])
                num %= 10
            
            if num > 0:
                words.append(units[num])
        
        if idx > 0 and any(words):
            words.append(thousands[idx])
        
        return ' '.join(words)
    
    words = []
    idx = 0
    while n > 0:
        segment = n % 1000
        words.append(rec_convert(segment, idx))
        n //= 1000
        idx += 1

    words = [w for w in reversed(words) if w]  
    

    if len(words) > 1:
        return ', '.join(words[:-1]) + ' and ' + words[-1]
    else:
        return words[0]


def convert_number_to_words_in(n):
    units = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    teens = ["", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    thousands = ["", "thousand", "lakh", "crore"]


    def rec_convert(num, idx):
        if num == 0:
            return ""
        
        words = []
        
        if num > 99 and (idx == 0 or idx == 1):
            words.append(units[num // 100])
            words.append("hundred")
            num %= 100
        
        if num > 10 and num < 20:
            words.append(teens[num-10])
        else:
            if num >= 20 or num == 10:
                words.append(tens[num // 10])
                num %= 10
            
            if num > 0:
                words.append(units[num])
            
        if idx > 0 and any(words):
            words.append(thousands[idx])
        
        return ' '.join(words)
    
    words = []
    idx = 0
    while n > 0:
        segment_size = 3 if idx == 0 or idx == 1 else 2
        segment = n % (10**segment_size)
        
        words.append(rec_convert(segment, idx))
        n //= (10**segment_size)
        idx += 1

    words = [w for w in reversed(words) if w] 
    
    if len(words) > 1:
        return ', '.join(words[:-1]) + ' and ' + words[-1]
    else:
        return words[0]




def cheque_generator():
    name = name_entry.get()
    date = date_entry.get()
    amount = int(amount_entry.get())
    country = country_var.get()
    amount_in_words = convert_number_to_words(amount, country)
    result.set(f"Pay: {name}\nDate: {date}\nAmount: {amount_in_words}")


root = Tk()


Label(root, text="Name").grid(row=0)
Label(root, text="Date").grid(row=1)
Label(root, text="Amount").grid(row=2)
Label(root, text="Country").grid(row=3)

name_entry = Entry(root)
date_entry = Entry(root)
amount_entry = Entry(root)

name_entry.grid(row=0, column=1)
date_entry.grid(row=1, column=1)
amount_entry.grid(row=2, column=1)


countries = ["US", "India"]
country_var = StringVar(root)
country_var.set(countries[0])  
country_menu = OptionMenu(root, country_var, *countries)
country_menu.grid(row=3, column=1)

Button(root, text='Generate', command=cheque_generator).grid(row=4, column=1, sticky=W, pady=4)

result = StringVar()
Label(root, text="", textvariable=result).grid(row=5)

mainloop()

Financial_Utilities/cheque_genrator.py
gmmkmtgk/GM_Stocks
import pandas as pd
from prophet import Prophet
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np

# Read stock data from a CSV file
df = pd.read_csv('../stock_data.csv')

# Enter a loop to input a valid stock symbol
while True:
    stock = input("Enter the Stock Symbol: ")
    if df['SYMBOL'].isin([stock]).any(): # Check if the entered stock symbol exists in the dataframe
        break
    else:
        print("Invalid Stock Symbol")

# Transpose the DataFrame, set the first row as column names, and reset the index
df = df.transpose()
df.columns = df.iloc[0]
df = df[1:]
df = df.reset_index()

# Rename the columns to 'ds' for dates and 'y' for the stock symbol
df = df.rename(columns={'index': 'ds', stock: 'y'})

# Convert the 'ds' column to datetime format
df['ds'] = pd.to_datetime(df['ds'])

# Input the number of days for future prediction
days = int(input("Enter the value for days: "))

# Ask the user if they want to perform a backtest
backtest_option = input("Do you want to perform a backtest? (yes/no): ")

if backtest_option.lower() == "yes":
    # Input a start date for backtesting
    start_date = input("Enter the start date for backtesting (YYYY-MM-DD): ")

    # Filter the DataFrame for the backtest period
    backtest_df = backtest_df = df[df['ds'] >= start_date].head(days)

    training_df = df[(df['ds'] < start_date)]
    # Create a Prophet model
    model = Prophet()

    # Fit the model with the DataFrame containing historical stock data for backtesting
    model.fit(training_df)

    # Create a DataFrame for future dates for prediction
    future = model.make_future_dataframe(periods=days)

    # Make predictions using the Prophet model
    prediction = model.predict(future)

    # Extract actual values for the backtest period
    actual_values = backtest_df['y'].values

    # Extract predicted values for the backtest period
    predicted_values = prediction[prediction['ds'] >= start_date]['yhat'].values

    # Calculate Mean Absolute Error (MAE) as a performance metric
    mae = mean_absolute_error(actual_values, predicted_values)

    # Calculate Mean Squared Error (MSE) as a performance metric
    mse = mean_squared_error(actual_values, predicted_values)

    print(predicted_values)
    print(actual_values)

    # Print the performance metrics
    print(f"Mean Absolute Error (MAE): {mae}")
    print(f"Mean Squared Error (MSE): {mse}")

else:
    # Create a Prophet model
    model = Prophet()

    # Fit the model with the DataFrame containing historical stock data
    model.fit(df)

    # Create a DataFrame for future dates for prediction
    future = model.make_future_dataframe(periods=days)

    # Make predictions using the Prophet model
    prediction = model.predict(future)

    # Save the prediction results to a CSV file without including the index column
    prediction.to_csv("fbprophet_prediction.csv", index=False)

Machine_Learning_Prediction/facebook_prophet.py
gmmkmtgk/GM_Stocks
import pandas as pd

def query_stock_data(csv_file, conditions):
    """
    Query stock data from a CSV file based on multiple conditions.

    Args:
    - csv_file (str): The path to the CSV file containing stock data.
    - conditions (list): A list of conditions in the format (parameter, operator, value).
                         Supported operators: '==', '>', '<', '>=', '<=', '!='.
                         Example conditions: [('Market Cap', '>', 1000), ('ROCE', '>', 15)]

    Returns:
    - DataFrame: A Pandas DataFrame containing rows that match the query criteria.
    """
    try:
        # Read the CSV file into a Pandas DataFrame
        df = pd.read_csv(csv_file)

        # Initialize a filter mask
        mask = pd.Series([True] * len(df))

        # Apply each condition to the filter mask
        for param, operator, value in conditions:
            if operator == '==':
                mask = mask & (df[param] == value)
            elif operator == '>':
                mask = mask & (df[param] > value)
            elif operator == '<':
                mask = mask & (df[param] < value)
            elif operator == '>=':
                mask = mask & (df[param] >= value)
            elif operator == '<=':
                mask = mask & (df[param] <= value)
            elif operator == '!=':
                mask = mask & (df[param] != value)

        # Apply the filter mask to the DataFrame
        queried_data = df[mask]

        return queried_data
    except FileNotFoundError:
        print(f"Error: CSV file '{csv_file}' not found.")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

# Example usage:
# csv_file = 'stock_data.csv'  # Replace with the path to your CSV file
# conditions = [('Market Cap', '>', 1000), ('ROCE', '>', 15)]
# result = query_stock_data(csv_file, conditions)
# print(result)

Stocks_Queries/queries.py
gmmkmtgk/GM_Stocks
import pandas as pd

def query_stock_data(csv_file, conditions):
    """
    Query stock data from a CSV file based on multiple conditions.

    Args:
    - csv_file (str): The path to the CSV file containing stock data.
    - conditions (list): A list of conditions in the format (parameter, operator, value).
                         Supported operators: '==', '>', '<', '>=', '<=', '!='.
                         Example conditions: [('Market Cap', '>', 1000), ('ROCE', '>', 15)]

    Returns:
    - DataFrame: A Pandas DataFrame containing rows that match the query criteria.
    """
    try:
        # Read the CSV file into a Pandas DataFrame
        df = pd.read_csv(csv_file)

        # Initialize a filter mask
        mask = pd.Series([True] * len(df))

        # Apply each condition to the filter mask
        for param, operator, value in conditions:
            if operator == '==':
                mask = mask & (df[param] == value)
            elif operator == '>':
                mask = mask & (df[param] > value)
            elif operator == '<':
                mask = mask & (df[param] < value)
            elif operator == '>=':
                mask = mask & (df[param] >= value)
            elif operator == '<=':
                mask = mask & (df[param] <= value)
            elif operator == '!=':
                mask = mask & (df[param] != value)

        # Apply the filter mask to the DataFrame
        queried_data = df[mask]

        return queried_data
    except FileNotFoundError:
        print(f"Error: CSV file '{csv_file}' not found.")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

# Example usage:
# csv_file = 'stock_data.csv'  # Replace with the path to your CSV file
# conditions = [('Market Cap', '>', 1000), ('ROCE', '>', 15)]
# result = query_stock_data(csv_file, conditions)
# print(result)

Stocks_Queries/useage.py
gmmkmtgk/GM_Stocks
# returns the stock symbols crocing there all time high by a percentage
Strategies/ATH_breakout.py
gmmkmtgk/GM_Stocks
import csv
import os
import pandas as pd

# Define the input and output file paths
input_file1 = os.path.join('..','stock_data.csv')
input_file2 = os.path.join('result.csv')
output_file = os.path.join('full_result.csv')

# Read the CSV files into pandas DataFrames
df1 = pd.read_csv(input_file1)
df2 = pd.read_csv(input_file2)

# Merge the DataFrames based on the "SYMBOL" column
merged_df = pd.merge(df1, df2, on='SYMBOL', how='inner')

# Save the merged DataFrame to a new CSV file
merged_df.to_csv(output_file, index=False)

print(f"Merged data saved to {output_file}")

Strategies/get_full_result.py
gmmkmtgk/GM_Stocks
# symbol, days in argument
# socks breakout out in that period
Strategies/horizontal_breakout.py
gmmkmtgk/GM_Stocks
import csv
import os
import pandas as pd

# Define the input and output file paths
input_file = os.path.join('..', 'stock_data.csv')
output_file = os.path.join('result.csv')

def find_breakout_stocks(data_file, percentage_threshold):
    # Load the CSV file into a DataFrame
    df = pd.read_csv(data_file)

    # Initialize an empty list to store breakout stocks
    breakout_stocks = []

    # Loop through each row in the DataFrame
    for index, row in df.iterrows():
        symbol = row['SYMBOL']
        prices = row[1:]  # Exclude the 'SYMBOL' column
        all_time_high = max(prices[-60:])
        current_price = prices[-1]

        # Calculate the percentage increase from the previous all-time high
        percentage_increase = ((current_price - all_time_high) / all_time_high) * 100

        # Check if the percentage increase is greater than the threshold
        if percentage_increase <= 1 and percentage_increase >= 0 :
            breakout_stocks.append(symbol)

    return breakout_stocks

# Example usage:
data_file = input_file
percentage_threshold = 5  # You can change this threshold as needed
breakout_stocks = find_breakout_stocks(data_file, percentage_threshold)

# Save the results to an output CSV file
with open(output_file, 'w', newline='') as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerow(['Breakout Stocks'])
    csv_writer.writerows(breakout_stocks)

print("Breakout Stocks ({}% above all-time high):".format(percentage_threshold))
print(breakout_stocks)

Strategies/horizontal_high_timespan.py
gmmkmtgk/GM_Stocks
import pandas as pd
import os

def momentum_generic_simple(input_csv_file, output_csv_file, num_days):
    try:
        # Read the input CSV file into a DataFrame
        df = pd.read_csv(input_csv_file)
        
        # Calculate the new column values for all rows by dividing the last column by num_days columns back
        df['New_Column'] = df.iloc[:, -1] / df.iloc[:, -num_days]
        
        # Round the new column values to three decimal places
        df['New_Column'] = df['New_Column'].round(3)
        
        # Sort the DataFrame in descending order based on the new column
        df_result_sorted = df.sort_values(by='New_Column', ascending=False)
        
        # Take the top row of the sorted DataFrame
        top_row = df_result_sorted.iloc[0]
        
        # Create a new DataFrame with the top row's symbol and new column value
        df_result = pd.DataFrame({'SYMBOL': [top_row['SYMBOL']], 'New_Column': [top_row['New_Column']]})
        
        # Save the resulting DataFrame to the specified output CSV file
        df_result.to_csv(output_csv_file, index=False)
        
        return True
    except Exception as e:
        return False

# Define the fixed input CSV file path
input_csv_file = os.path.join('..', 'stock_data.csv')

# Ask the user to input the number of days
num_days = int(input("Enter the number of days: "))

# Specify the desired output CSV file name
output_csv_file = "result.csv"

if momentum_generic_simple(input_csv_file, output_csv_file, num_days):
    print(f"Top row saved to {output_csv_file}.")
else:
    print("Error processing the CSV file.")

Strategies/momentum_genric_simple.py
gmmkmtgk/GM_Stocks
import pandas as pd
import os

def momentum_genric_total(filename):
    # Read the CSV file into a DataFrame
    df = pd.read_csv(filename)

    # Get the last column name (assuming it's the last column in the DataFrame)
    last_column_name = df.columns[-1]

    # Get user input for days and weights
    user_input = []
    while True:
        try:
            days, weight = map(int, input("Enter days and weight (e.g., 10, 30): ").split(","))
            user_input.append((days, weight))
        except ValueError:
            break

    # Calculate the new combined column based on user input
    momentum_total = 0
    for days, weight in user_input:
        last_Days_name=df.columns[-days]
     
        momentum_total += (df[last_column_name] / (df[last_Days_name])) * weight
        print(momentum_total)
    # print(momentum_total)

   # Format the values in the new_combined_column to three decimal places
    df['momentum_total'] = momentum_total


    # Create a new DataFrame with only 'SYMBOL' and 'momentum_total ' columns
    result_df = df[['SYMBOL', 'momentum_total']]

    # Sort the DataFrame in descending order based on 'momentum_total'
    result_df = result_df.sort_values(by='momentum_total', ascending=False)

    # Save the sorted DataFrame to the CSV file
    result_df.to_csv('result.csv', index=False)

# Usage example:
momentum_genric_total(os.path.join('..', 'stock_data.csv'))

Strategies/momentum_genric_totally.py
gmmkmtgk/GM_Stocks
import csv
import os
import pandas as pd

# Define the input and output file paths
input_file = os.path.join('..', 'stock_data.csv')
output_file = os.path.join('result.csv')

# User-defined parameters
days1 = int(input("Enter the value for days1: "))
days2 = int(input("Enter the value for days2: "))
divergence = float(input("Enter the value for divergence: "))
threshold = float(input("Enter the value for threshold: "))
above = input("Enter 'y' for above or 'n' for below: ").lower() == 'y'

# Read the CSV data into a pandas DataFrame
df = pd.read_csv(input_file)

# Create an empty list to store symbols that meet the criteria
selected_symbols = []

# Iterate through the DataFrame to check for crossover and percentage condition
for index, row in df.iterrows():
    symbol = row['SYMBOL']
    latest_prices = row.iloc[:].values[1:].astype(float)  # Extract the latest prices
    # Calculate the moving averages
    dma1 = sum(latest_prices[-days1:]) / days1
    dma2 = sum(latest_prices[-days2:]) / days2

    # Check if the crossover and percentage conditions are met
    if (dma1 / dma2 > divergence - threshold) and (dma1 / dma2 < divergence + threshold):
        if above and dma1 > dma2:
            selected_symbols.append([symbol])  # Store each symbol in a list
        elif not above and dma1 < dma2:
            selected_symbols.append([symbol])  # Store each symbol in a list

# Write the selected symbols to the result CSV file with each symbol in a separate row
with open(output_file, 'w', newline='') as result_csv:
    writer = csv.writer(result_csv)
    writer.writerow(['SYMBOL'])
    writer.writerows(selected_symbols)

print("Results saved in result.csv")

Strategies/moving_average_crossover.py
gmmkmtgk/GM_Stocks
# for one stock
# symbol, date_of_invest, date_of_selling

# for multiple stocks by equal weight
# symbol, date_of_invest, date_of_selling

# for multiple stocks by unequal weight
# symbol, date_of_invest, date_of_selling


backtesting/get_return.py
gmmkmtgk/GM_Stocks
# detele all the saved charts in charts folder
draw_charts/detete_chart.py
gmmkmtgk/GM_Stocks
# open a ui dailog box to draw charts and lines
draw_charts/draw_chart_ui.py
gmmkmtgk/GM_Stocks
# save chart in charts folder
draw_charts/save_chart.py
gmmkmtgk/GM_Stocks
import subprocess

# Specify the path to the Python file you want to run
python_file_path = "path_to_your_script.py"

try:
    # Run the Python file using subprocess
    subprocess.run(["python", python_file_path], check=True)
except subprocess.CalledProcessError as e:
    print(f"Error running {python_file_path}: {e}")
except FileNotFoundError:
    print(f"Python executable not found. Make sure Python is installed and in your system's PATH.")

main.py
gmmkmtgk/GM_Stocks


# get f&o stocks

# get ban stocks


utilities/get_f&o_stocks.py
gmmkmtgk/GM_Stocks
import pandas as pd

def get_prices_from_user_input( symbol, num_days):
    try:
        df = pd.read_csv('stock_data.csv')
        symbol = symbol.upper()
        symbol_data = df[df['SYMBOL'] == symbol]
        if symbol_data.empty:
            return None
        prices = symbol_data.iloc[:, -num_days:].values.tolist()[0]
        prices = [price for price in prices if not pd.isna(price)]
        return prices
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

utilities/get_stock_price.py
gmmkmtgk/GM_Stocks
# Get all stocks present in csv file stock_data.csv
utilities/get_stocks.py
gmmkmtgk/GM_Stocks
#Add real time price couln in betwwen running market for better analysis
utilities/real_time.py
gmmkmtgk/GM_Stocks
import os
import requests
from urllib.parse import urlparse, urljoin
from bs4 import BeautifulSoup

# Function to save content to a file
def save_content_to_file(content, file_path):
    with open(file_path, 'wb') as file:
        file.write(content)

# Function to scrape a webpage and its linked pages recursively
def scrape_webpage(url):
    # Make a request to the URL
    response = requests.get(url)

    # Check if the request was successful (status code 200)
    if response.status_code == 200:
        # Parse the HTML content of the page using BeautifulSoup
        soup = BeautifulSoup(response.text, 'html.parser')

        # Save the page content to a file in the current directory
        file_name = urlparse(url).path.strip('/').replace('/', '_') + ".html"
        file_path = os.path.join(os.getcwd(), file_name)
        save_content_to_file(response.content, file_path)

        # Find and follow links on the page (you may need to customize this)
        for link in soup.find_all('a'):
            href = link.get('href')
            if href and not href.startswith('#'):
                absolute_url = urljoin(url, href)
                scrape_webpage(absolute_url)

# User input: URL to scrape
url_to_scrape = input("Enter the URL to scrape: ")

# Start scraping
scrape_webpage(url_to_scrape)

print("Scraping completed. Data saved in the current directory.")

web_scrapper/scrape_any_link.py
gmmkmtgk/GM_Stocks