Skip to main content

A Python package to scrape YouTube comments using Selenium and BeautifulSoup

Project description

YouTube Comment Scraper

YouTubeCommentScraper is a Python package designed to scrape comments from YouTube videos using Selenium. The scraper is customizable, allowing you to run the browser in headless mode, control the timeout, pause time for scrolling, and more. You can also choose whether to log actions and return the page source along with the comments.

Features

  • Headless Mode: Run the browser in headless mode (optional).
  • Customizable Timeouts: Set the timeout for waiting for elements to load.
  • Automatic Scrolling: Automatically scrolls the page until all comments are loaded.
  • Logging Support: Enable logging to a file for tracking activities.
  • Return Page Source: Optionally return the page source along with the comments.
  • BeautifulSoup Integration: Extract comments using BeautifulSoup for robust parsing.

Installation

To install the package, use the following command:

pip install youtube-comment-scraper

Dependencies

This package requires the following dependencies:

  • selenium
  • webdriver-manager
  • beautifulsoup4
  • lxml (optional but recommended for faster HTML parsing)

You can install these dependencies using the following command:

pip install selenium webdriver-manager beautifulsoup4 lxml

Usage

1. Basic Usage: Scraping Comments

Here's a simple example to scrape comments from a YouTube video:

from youtube_comment_scraper import YouTubeCommentScraper

scraper = YouTubeCommentScraper(headless=True, timeout=10, scroll_pause_time=1.5, enable_logging=False, return_page_source=False)
video_url = "https://www.youtube.com/watch?v=Ycg48pVp3SU"
comments = scraper.scrape_comments(video_url)

print("Comments:", comments)

2. Scraping Comments with Logging Enabled

Enable logging to track the actions performed during scraping:

from youtube_comment_scraper import YouTubeCommentScraper

scraper = YouTubeCommentScraper(headless=True, timeout=10, scroll_pause_time=1.5, enable_logging=True, return_page_source=False)
video_url = "https://www.youtube.com/watch?v=Ycg48pVp3SU"
comments = scraper.scrape_comments(video_url)

print("Comments:", comments)

This will generate a log file (youtube_scraper.log) in the current directory.

3. Returning Page Source Along with Comments

If you want to extract comments and return the page's HTML source:

from youtube_comment_scraper import YouTubeCommentScraper

scraper = YouTubeCommentScraper(headless=True, timeout=10, scroll_pause_time=1.5, enable_logging=False, return_page_source=True)
video_url = "https://www.youtube.com/watch?v=Ycg48pVp3SU"
comments, page_source = scraper.scrape_comments(video_url)

print("Comments:", comments)
print("Page Source:", page_source)

4. Custom Scroll Pause Time

You can control how long the scraper pauses between scroll actions using the scroll_pause_time parameter:

from youtube_comment_scraper import YouTubeCommentScraper

scraper = YouTubeCommentScraper(headless=True, timeout=10, scroll_pause_time=2.0, enable_logging=False, return_page_source=False)
video_url = "https://www.youtube.com/watch?v=Ycg48pVp3SU"
comments = scraper.scrape_comments(video_url)

print("Comments:", comments)

5. Scraping Comments Without Scrolling

If you only want to scrape the comments that load without scrolling:

from youtube_comment_scraper import YouTubeCommentScraper

scraper = YouTubeCommentScraper(headless=True, timeout=10, scroll_pause_time=1.5, enable_logging=False, return_page_source=False)
video_url = "https://www.youtube.com/watch?v=Ycg48pVp3SU"
comments = scraper.scrape_comments(video_url, scroll=False)

print("Comments:", comments)

6. Logging Custom Messages

You can log custom messages using the built-in log_info, log_warning, and log_error methods:

scraper.log_info("This is an info log message.")
scraper.log_warning("This is a warning message.")
scraper.log_error("This is an error message.")

Class Reference

YouTubeCommentScraper

__init__(self, headless=True, timeout=10, scroll_pause_time=1.5, enable_logging=False, return_page_source=False)

  • headless (bool): Run the browser in headless mode. Default is True.
  • timeout (int): The maximum time to wait for elements to load. Default is 10 seconds.
  • scroll_pause_time (float): The pause time between scroll actions. Default is 1.5 seconds.
  • enable_logging (bool): Whether to enable logging to a file. Default is False.
  • return_page_source (bool): Whether to return the page source along with comments. Default is False.

scrape_comments(self, video_url, scroll=True)

  • video_url (str): The URL of the YouTube video.
  • scroll (bool): Whether to scroll the page to load all comments. Default is True.

Returns:

  • A tuple (comments, page_source) if return_page_source is True, otherwise just the list of comments.

log_info(self, message)

  • Logs an informational message.

log_warning(self, message)

  • Logs a warning message.

log_error(self, message)

  • Logs an error message.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

youtube-comments-scrapper-0.3.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

youtube_comments_scrapper-0.3.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file youtube-comments-scrapper-0.3.0.tar.gz.

File metadata

File hashes

Hashes for youtube-comments-scrapper-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e70e26900d883038fe90a1097a304285786395647b8ce4035b83e3212d2f4989
MD5 99ae6dc3412c78dab54e6a7fb0c60dce
BLAKE2b-256 a8fe30cbb15e5d0f3dc9b28ef74c3f26b8ec36fe3c033286049e086e16792b46

See more details on using hashes here.

File details

Details for the file youtube_comments_scrapper-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for youtube_comments_scrapper-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d56345fc6b7959706d2e6b9e70d91f8e734d3f61809fea7d2921baffe780895
MD5 da1b9540924c5a782d96456a173c31aa
BLAKE2b-256 8138e9d4a5b866ef7bbb527ea65537729b6d15e54c7a748f0c66c814a43dd3ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page