Giter Club home page Giter Club logo

kalvium-data-analyst's Introduction

Kalvium-Data-Analyst

Kalvium Data Analyst task

Lok Sabha Election Results Analysis

This repository contains a data analysis of the recently concluded Lok Sabha election results. The analysis includes scraping the election results data from the Election Commission of India's website, processing the data, and generating insights.

Table of Contents

Introduction

This project aims to provide a comprehensive analysis of the 2024 Lok Sabha election results. The data is scraped from the Election Commission of India's official website and various insights are derived from the processed data.

Data Scraping

The data is scraped from the Election Commission of India's results page using BeautifulSoup. If the data is loaded dynamically, Selenium is used to ensure all data is captured correctly.

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/lok-sabha-election-analysis.git
    cd lok-sabha-election-analysis
  2. Install the required packages:

    pip install -r requirements.txt
  3. If using Selenium, ensure you have the appropriate WebDriver for your browser:

Usage

To scrape the election results data and save it to a CSV file, run:

python scrape_election_results.py


### scrape_election_results.py

Here's the complete Python script for scraping the election results and saving them to a CSV file:

```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def scrape_with_bs4():
    url = "https://results.eci.gov.in/pcresults.htm"  # Update with the correct URL
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    table = soup.find("table", {"id": "datatable"})

    if table is None:
        print("Table not found. Here is the HTML:")
        print(soup.prettify())
    else:
        data = []
        headers = [th.text.strip() for th in table.find_all("th")]
        data.append(headers)

        for row in table.find_all("tr")[1:]:
            cols = row.find_all("td")
            cols = [ele.text.strip() for ele in cols]
            data.append(cols)

        df = pd.DataFrame(data[1:], columns=data[0])
        df.to_csv("election_results.csv", index=False)
        print("Data saved to election_results.csv")

def scrape_with_selenium():
    driver_path = 'path/to/chromedriver'  # Change to the path where your chromedriver is located
    driver = webdriver.Chrome(driver_path)
    url = "https://results.eci.gov.in/pcresults.htm"  # Update with the correct URL
    driver.get(url)

    time.sleep(10)  # Adjust the sleep time as needed

    table = driver.find_element(By.ID, "datatable")

    data = []
    headers = [th.text.strip() for th in table.find_elements(By.TAG_NAME, "th")]
    data.append(headers)

    rows = table.find_elements(By.TAG_NAME, "tr")[1:]  # Skip header row
    for row in rows:
        cols = row.find_elements(By.TAG_NAME, "td")
        cols = [ele.text.strip() for ele in cols]
        data.append(cols)

    df = pd.DataFrame(data[1:], columns=data[0])
    df.to_csv("election_results.csv", index=False)
    driver.quit()
    print("Data saved to election_results.csv")

# Uncomment one of the following lines to use the respective scraping method
# scrape_with_bs4()
# scrape_with_selenium()

kalvium-data-analyst's People

Contributors

shivambhavsar-cmd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.