Claim your Biolink Click Here
1 like 0 dislike
43 views
I want to extract all stocks names in CSV with python script, any one can provide me that?
in Computers & Internet by (820 points) | 43 views

1 Answer

1 like 0 dislike
Best answer
import csv
import requests
from bs4 import BeautifulSoup

def scrape_page(url):
    # Send a GET request to the URL
    response = requests.get(url)

    # Check if the page exists
    if response.status_code != 200:
        return []

    # Parse the HTML content
    soup = BeautifulSoup(response.content, "html.parser")

    # Find the table containing the stock names
    table = soup.find("table", class_="data-table")

    if table:
        # Find all rows in the table
        rows = table.find_all("tr")

        # Extract stock names from each row
        stock_names = []
        for row in rows[1:]:  # Exclude header row
            cells = row.find_all("td")
            if cells:  # Check if cells exist in the row
                stock_name = cells[1].text.strip()  # Assuming the stock name is in the first column
                stock_names.append(stock_name)

        return stock_names
    else:
        print("Table not found on page:", url)
        return []

def scrape_all_pages(base_url):
    all_stock_names = []

    # Iterate over each page
    page_number = 1
    while True:
        page_url = base_url.format(page_number)
        print("Scraping page:", page_url)
        stock_names = scrape_page(page_url)
        if not stock_names:
            break
        all_stock_names.extend(stock_names)
        page_number += 1

    return all_stock_names

# URL of the webpage with pagination
base_url = "https://www.screener.in/screens/198573/all-stock-list-bse/?page={}"

# Scrape data from all pages
all_stock_names = scrape_all_pages(base_url)

# Write the stock names to a CSV file
csv_filename = "stock_names.csv"
with open(csv_filename, "w", newline="", encoding="utf-8") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(["Stock Name"])  # Write header
    for name in all_stock_names:
        csv_writer.writerow([name])

print("All stock names have been scraped and saved in", csv_filename)
by (1.6k points)
selected by

Related questions

2 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
2 answers
0 like 0 dislike
1 answer
3 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,313 questions
1,475 answers
569 comments
4,809 users