Scrappy Lyrics Search is a Flask-based web application that allows you to search for song lyrics by artist name. It scrapes lyrics from AzLyrics.com and displays the results in a user-friendly interface.
- Python 3.x
- Flask
- Requests
- BeautifulSoup4
Lyric-scrapper/
│
├── scrappy.py # Flask application script
├── requirements.txt # List of dependencies
└── templates/
└── index.html # HTML template for the web application
First, ensure you have Python 3.x installed. Open your terminal or command prompt and navigate to the directory where you want to set up the project
-
Clone the Repository
mkdir Lyric-scrapper cd Lyric-scrapper git clone https://github.com/metrik420/Lyric-scrapper.git
-
Install Dependencies
pip install -r requirements.txt
-
Run the Application
python3 scrappy.py
Open your web browser and go to
http://localhost:1990
to access the application.
- Flask Application Setup
- The app is built using Flask and runs on 0.0.0.0:1990 with debugging enabled. It can be accessed by devices on the same network.
- User-Agent Randomization
- To avoid detection as a bot, the application uses a list of randomized User-Agent strings, ensuring each request simulates traffic from different browsers.
- Human-like Request Delays
- The fetch_lyrics function introduces a random delay (2 to 5 seconds) between requests, mimicking human browsing behavior and reducing the chances of being blocked by the website.
- Dynamic Search URL Generation
- Upon form submission, the artist's name is encoded and incorporated into the AzLyrics search URL, dynamically generating the search query for lyrics retrieval.
- Lyrics Fetching
- The app sends a GET request to AzLyrics, using BeautifulSoup to parse the HTML and extract song titles and corresponding URLs for lyrics.
- Robust Error Handling
- Handles potential HTTP errors or request exceptions. If an error occurs, it logs the issue and ensures the application continues running smoothly without crashing.
- HTML Parsing for Song Data
- The app extracts song titles and their corresponding URLs from tables with the class "table table-condensed", specifically targeting links containing /lyrics/.
- Form Submission Handling
- The application supports both GET and POST requests:
- POST: When a user submits an artist name, it retrieves and displays the lyrics.
- GET: Renders the default search form for user input.
- Dynamic Template Rendering
- The index.html template is dynamically updated based on user input. It displays search results (song titles and lyrics) or appropriate error messages if the search fails.
- Clean URL Construction
- For each song title, the app ensures the URL is fully constructed, appending the domain (https://www.azlyrics.com) when necessary, ensuring all links work correctly.
- Search Error Messaging
- If no songs are found or an error occurs during the fetch process, a user-friendly error message is displayed, helping guide the user on potential issues.
This Flask application allows users to search for song lyrics by artist, dynamically generates the search results, and employs multiple techniques to avoid detection by anti-scraping mechanisms.