本文整理汇总了Python中bs4.__version__方法的典型用法代码示例。如果您正苦于以下问题:Python bs4.__version__方法的具体用法?Python bs4.__version__怎么用?Python bs4.__version__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bs4
的用法示例。
在下文中一共展示了bs4.__version__方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _skip_if_none_of
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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")
示例2: _skip_if_none_of
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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'):
pytest.skip("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):
pytest.skip("{0!r} not found".format(not_found))
if 'bs4' in module_names:
import bs4
if bs4.__version__ == LooseVersion('4.2.0'):
pytest.skip("Bad version of bs4: 4.2.0")
示例3: _parser_dispatch
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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('{invalid!r} is not a valid flavor, valid flavors '
'are {valid}'
.format(invalid=flavor, valid=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 LooseVersion(bs4.__version__) <= LooseVersion('4.2.0'):
raise ValueError("A minimum version of BeautifulSoup 4.2.1 "
"is required")
else:
if not _HAS_LXML:
raise ImportError("lxml not found, please install it")
return _valid_parsers[flavor]
示例4: test_bs4_version_fails
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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')
示例5: benchmark_parsers
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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))
示例6: mock_post
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
def mock_post(mocked_adapter, url, expected, reply='Success!'):
def text_callback(request, context):
# Python 2's parse_qsl doesn't like None argument
query = parse_qsl(request.text) if request.text else []
# In bs4 4.7.0+, CSS selectors return elements in page order,
# but did not in earlier versions.
if StrictVersion(bs4.__version__) >= StrictVersion('4.7.0'):
assert query == expected
else:
assert sorted(query) == sorted(expected)
return reply
mocked_adapter.register_uri('POST', url, text=text_callback)
示例7: _parser_dispatch
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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("bs4 not found please install it")
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]
示例8: diagnose
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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)
示例9: _parser_dispatch
# 需要导入模块: import bs4 [as 别名]
# 或者: from bs4 import __version__ [as 别名]
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('{invalid!r} is not a valid flavor, valid flavors '
'are {valid}'
.format(invalid=flavor, valid=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]