From 5b5540581396b07404018664f0954f2d13377753 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 24 Dec 2018 10:17:54 -0500 Subject: Issue a warning and raise a more useful exception if someone tries to call Tag.select() without SoupSieve installed. --- NEWS.txt | 14 ++++++++++++++ bs4/element.py | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 6e44504..4f5278a 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,5 +1,19 @@ = 4.7.0 (Unreleased) +* Beautiful Soup's CSS Selector implementation has been replaced by a + dependency on Isaac Muse's SoupSieve project (the soupsieve package + on PyPI). The good news is that SoupSieve has a much more robust and + complete implementation of CSS selectors, resolving a large number + of longstanding issues. The bad news is that from this point onward, + SoupSieve must be installed if you want to use the select() method. + + You don't have to change anything lf you installed Beautiful Soup + through pip (SoupSieve will be automatically installed when you + upgrade Beautiful Soup) or if you don't use CSS selectors from + within Beautiful Soup. + + SoupSieve documentation: https://facelessuser.github.io/soupsieve/ + * Fix a number of problems with the tree builder that caused trees that were superficially okay, but which fell apart when bits were extracted. Patch by Isaac Muse. [bug=1782928] diff --git a/bs4/element.py b/bs4/element.py index 7734f80..97403c9 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -9,7 +9,14 @@ except ImportError , e: import re import sys import warnings -import soupsieve +try: + import soupsieve +except ImportError, e: + soupsieve = None + warnings.warn( + 'The soupsieve package is not installed. CSS selectors cannot be used.' + ) + from bs4.dammit import EntitySubstitution DEFAULT_OUTPUT_ENCODING = "utf-8" @@ -1347,7 +1354,11 @@ class Tag(PageElement): if limit is None: limit = 0 - + if soupsieve is None: + raise NotImplementedError( + "Cannot execute CSS selectors because the soupsieve package is not installed." + ) + return soupsieve.select(selector, self, namespaces, limit, **kwargs) # Old names for backwards compatibility -- cgit v1.2.1