我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用bs4.__version__()。
def _skip_if_none_of(module_names): if isinstance(module_names, string_types): _skip_if_no(module_names) if module_names == 'bs4': import bs4 if bs4.__version__ == LooseVersion('4.2.0'): raise nose.SkipTest("Bad version of bs4: 4.2.0") else: not_found = [module_name for module_name in module_names if not _have_module(module_name)] if set(not_found) & set(module_names): raise nose.SkipTest("{0!r} not found".format(not_found)) if 'bs4' in module_names: import bs4 if bs4.__version__ == LooseVersion('4.2.0'): raise nose.SkipTest("Bad version of bs4: 4.2.0")
def benchmark_parsers(num_elements=100000): """Very basic head-to-head performance benchmark.""" print("Comparative parser benchmark on Beautiful Soup %s" % __version__) data = rdoc(num_elements) print("Generated a large invalid HTML document (%d bytes)." % len(data)) for parser in ["lxml", ["lxml", "html"], "html5lib", "html.parser"]: success = False try: a = time.time() soup = BeautifulSoup(data, parser) b = time.time() success = True except Exception as e: print("%s could not parse the markup." % parser) traceback.print_exc() if success: print("BS4+%s parsed the markup in %.2fs." % (parser, b-a)) from lxml import etree a = time.time() etree.HTML(data) b = time.time() print("Raw lxml parsed the markup in %.2fs." % (b-a)) import html5lib parser = html5lib.HTMLParser() a = time.time() parser.parse(data) b = time.time() print("Raw html5lib parsed the markup in %.2fs." % (b-a))
def test_bs4_version_fails(): _skip_if_none_of(('bs4', 'html5lib')) import bs4 if bs4.__version__ == LooseVersion('4.2.0'): tm.assert_raises(AssertionError, read_html, os.path.join(DATA_PATH, "spam.html"), flavor='bs4')
def diagnose(data): """Diagnostic suite for isolating common problems.""" print("Diagnostic running on Beautiful Soup %s" % __version__) print("Python version %s" % sys.version) basic_parsers = ["html.parser", "html5lib", "lxml"] for name in basic_parsers: for builder in builder_registry.builders: if name in builder.features: break else: basic_parsers.remove(name) print(( "I noticed that %s is not installed. Installing it may help." % name)) if 'lxml' in basic_parsers: basic_parsers.append(["lxml", "xml"]) try: from lxml import etree print("Found lxml version %s" % ".".join(map(str,etree.LXML_VERSION))) except ImportError as e: print ( "lxml is not installed or couldn't be imported.") if 'html5lib' in basic_parsers: try: import html5lib print("Found html5lib version %s" % html5lib.__version__) except ImportError as e: print ( "html5lib is not installed or couldn't be imported.") if hasattr(data, 'read'): data = data.read() elif os.path.exists(data): print('"%s" looks like a filename. Reading data from the file.' % data) with open(data) as fp: data = fp.read() elif data.startswith("http:") or data.startswith("https:"): print('"%s" looks like a URL. Beautiful Soup is not an HTTP client.' % data) print("You need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup.") return print() for parser in basic_parsers: print("Trying to parse your markup with %s" % parser) success = False try: soup = BeautifulSoup(data, parser) success = True except Exception as e: print("%s could not parse the markup." % parser) traceback.print_exc() if success: print("Here's what %s did with the markup:" % parser) print(soup.prettify()) print("-" * 80)
def diagnose(data): """Diagnostic suite for isolating common problems.""" print("Diagnostic running on Beautiful Soup %s" % __version__) print("Python version %s" % sys.version) basic_parsers = ["html.parser", "html5lib", "lxml"] for name in basic_parsers: for builder in builder_registry.builders: if name in builder.features: break else: basic_parsers.remove(name) print(( "I noticed that %s is not installed. Installing it may help." % name)) if 'lxml' in basic_parsers: basic_parsers.append(["lxml", "xml"]) try: from lxml import etree print("Found lxml version %s" % ".".join(map(str,etree.LXML_VERSION))) except ImportError as e: print ( "lxml is not installed or couldn't be imported.") if 'html5lib' in basic_parsers: try: import html5lib print("Found html5lib version %s" % html5lib.__version__) except ImportError as e: print ( "html5lib is not installed or couldn't be imported.") if hasattr(data, 'read'): data = data.read() elif os.path.exists(data): print('"%s" looks like a filename. Reading data from the file.' % data) data = open(data).read() elif data.startswith("http:") or data.startswith("https:"): print('"%s" looks like a URL. Beautiful Soup is not an HTTP client.' % data) print("You need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup.") return print() for parser in basic_parsers: print("Trying to parse your markup with %s" % parser) success = False try: soup = BeautifulSoup(data, parser) success = True except Exception as e: print("%s could not parse the markup." % parser) traceback.print_exc() if success: print("Here's what %s did with the markup:" % parser) print(soup.prettify()) print("-" * 80)
def _parser_dispatch(flavor): """Choose the parser based on the input flavor. Parameters ---------- flavor : str The type of parser to use. This must be a valid backend. Returns ------- cls : _HtmlFrameParser subclass The parser class based on the requested input flavor. Raises ------ ValueError * If `flavor` is not a valid backend. ImportError * If you do not have the requested `flavor` """ valid_parsers = list(_valid_parsers.keys()) if flavor not in valid_parsers: raise ValueError('%r is not a valid flavor, valid flavors are %s' % (flavor, valid_parsers)) if flavor in ('bs4', 'html5lib'): if not _HAS_HTML5LIB: raise ImportError("html5lib not found, please install it") if not _HAS_BS4: raise ImportError( "BeautifulSoup4 (bs4) not found, please install it") import bs4 if bs4.__version__ == LooseVersion('4.2.0'): raise ValueError("You're using a version" " of BeautifulSoup4 (4.2.0) that has been" " known to cause problems on certain" " operating systems such as Debian. " "Please install a version of" " BeautifulSoup4 != 4.2.0, both earlier" " and later releases will work.") else: if not _HAS_LXML: raise ImportError("lxml not found, please install it") return _valid_parsers[flavor]