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.
- 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.
To install the package, use the following command:
pip install youtube-comments-scrapperThis 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 lxmlHere'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)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.
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)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)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)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.")__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 isTrue.timeout(int): The maximum time to wait for elements to load. Default is10seconds.scroll_pause_time(float): The pause time between scroll actions. Default is1.5seconds.enable_logging(bool): Whether to enable logging to a file. Default isFalse.return_page_source(bool): Whether to return the page source along with comments. Default isFalse.
video_url(str): The URL of the YouTube video.scroll(bool): Whether to scroll the page to load all comments. Default isTrue.
Returns:
- A tuple
(comments, page_source)ifreturn_page_sourceisTrue, otherwise just the list ofcomments.
- Logs an informational message.
- Logs a warning message.
- Logs an error message.
This project is licensed under the MIT License.
