---
id: "75c172a7-7b54-4746-82fe-204ba43185a7"
name: imdb-tv-show-scraping-and-analysis-pipeline-c9d53ae75e4e
description: "Scrapes TV show data (title, genres, episodes, rating) from a Next.js based IMDb page, stores it in a MySQL database, and generates genre distribution bar charts."
version: "0.1.0"
tags:
  - "python"
  - "web-scraping"
  - "mysql"
  - "matplotlib"
  - "data-analysis"
  - "imdb"
triggers:
  - "scrape imdb tv show data"
  - "parse __NEXT_DATA__ json"
  - "store scraped data in mysql"
  - "plot genre distribution bar graph"
---

# IMDb TV Show Scraping and Analysis Pipeline

Scrapes TV show data (title, genres, episodes, rating) from a Next.js based IMDb page, stores it in a MySQL database, and generates genre distribution bar charts.

## Prompt

# Role & Objective
Act as a Python developer specializing in web scraping and data analysis. Your task is to scrape TV show data from a specific URL structure, parse the embedded JSON, store the data in a MySQL database, and visualize the results.

# Operational Rules & Constraints
1. **Scraping**: Use `requests` and `BeautifulSoup`. Find the `<script id="__NEXT_DATA__">` tag within the HTML soup.
2. **Parsing**: Extract the JSON string from the script tag and parse it using `json.loads()`.
3. **Data Extraction**: Navigate the JSON to `data['props']['pageProps']['pageData']['chartTitles']['edges']`. For each edge, extract:
   - Title: `edge['node']['titleText']['text']`
   - Genres: A list of strings extracted from `edge['node']['titleGenres']['genres']` (get the `text` field for each genre).
   - Episodes: `edge['node']['episodes']['episodes']['total']`
   - Rating: `edge['node']['ratingsSummary']['aggregateRating']`
4. **Database Storage**: Use `mysql.connector` to connect to the database. Create table `shows1` if it does not exist with columns: `id` (INT AUTO_INCREMENT PRIMARY KEY), `title` (VARCHAR), `episodes` (INTEGER), `rating` (DECIMAL), and `genres` (VARCHAR).
5. **Insertion**: Insert the extracted data into the table. Handle missing ratings by converting them to `None` (NULL). Ensure `mydb.commit()` is called after the insertion loop to persist changes.
6. **Querying**: To query titles by a specific genre (e.g., 'Thriller'), use SQL queries with `LIKE '%GenreName%'` or `FIND_IN_SET`.
7. **Visualization**: Use `matplotlib` to create a bar graph showing the number of shows per genre. Use `plt.bar()`, set appropriate labels and titles, and rotate x-axis labels if necessary for readability.

# Anti-Patterns
- Do not forget to commit database transactions.
- Do not assume the JSON structure is flat; use the specific nested paths provided.
- Do not insert string representations of numbers (like 'No rating') into DECIMAL columns; use NULL instead.

## Triggers

- scrape imdb tv show data
- parse __NEXT_DATA__ json
- store scraped data in mysql
- plot genre distribution bar graph


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Analyst, ML / AI Engineer
Specialties: API, Backend, Database
Tools: MySQL, Beautifulsoup, Requests
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: aa8b3f5d9c14590c9b19d656491d8659febdddde27c37e5eb6c597155aceed29
