Skip to main content

YouTube Background

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-comments-scrapper

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 (optional):

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_comments_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_comments_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_comments_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_comments_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_comments_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.

Metadata

Release files for youtube-comments-scrapper 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for youtube-comments-scrapper 1.0.1
File Size Uploaded Provenance
youtube-comments-scrapper-1.0.1.tar.gz 5.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for youtube-comments-scrapper 1.0.1
File Interpreter ABI Platform
youtube_comments_scrapper-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / youtube-comments-scrapper-1.0.1.tar.gz

Download URL youtube-comments-scrapper-1.0.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2e671a5382e19fa59c33173f9bf5c6f3e5a10cabbeb7a3abd26a2431f5ec670b
BLAKE2b-256 checksum
How to use checksums
3e3a958b2d520cbed507ffd9a93dbe2487fb20c04ab0712759ce1265fdd1f2e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / youtube_comments_scrapper-1.0.1-py3-none-any.whl

Download URL youtube_comments_scrapper-1.0.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb89ba003fd8ad9a7726d5b000c47ec428bead3c85295adc13ea268f4d5e802c
BLAKE2b-256 checksum
How to use checksums
437fdbf5ae465a57b4c6afa8859871d9f4e08bf7de6c9a163e40d68bfc0c90f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Security for youtube-comments-scrapper 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page