Create a Python web scraper that extracts information about movies from IMDb. The scraper should fetch movie titles, release years, ratings, and genres for a given range of pages. Store the extracted data in a SQLite database. PLAINFORMAT
import requests
from bs4 import BeautifulSoup
import sqlite3

def create_table():
    conn = sqlite3.connect('movies.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS movies
                 (title TEXT, release_year INTEGER, rating REAL, genres TEXT)''')
    conn.commit()
    conn.close()

def insert_movie(title, release_year, rating, genres):
    conn = sqlite3.connect('movies.db')
    c = conn.cursor()
    c.execute("INSERT INTO movies VALUES (?, ?, ?, ?)", (title, release_year, rating, genres))
    conn.commit()
    conn.close()

def scrape_movies(start_page, end_page):
    base_url = "https://www.imdb.com/search/title/?start={}&ref_=adv_nxt"
    
    for page in range(start_page, end_page + 1):
        url = base_url.format((page - 1) * 50 + 1)
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')

        movie_containers = soup.find_all('div', class_='lister-item mode-advanced')

        for container in movie_containers:
            title = container.h3.a.text
            release_year = int(container.h3.find('span', class_='lister-item-year').text.strip('()'))
            rating = float(container.strong.text)
            genres = container.find('span', class_='genre').text.strip()
            
            insert_movie(title, release_year, rating, genres)

if __name__ == "__main__":
    create_table()
    scrape_movies(1, 5)