2020-01-30 01:07:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Tests of Beautiful Soup as a whole."""
|
|
|
|
|
|
|
|
from pdb import set_trace
|
|
|
|
import logging
|
2022-01-24 04:07:52 +00:00
|
|
|
import os
|
2022-11-07 18:06:49 +00:00
|
|
|
import pickle
|
|
|
|
import pytest
|
2020-01-30 01:07:26 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from bs4 import (
|
|
|
|
BeautifulSoup,
|
|
|
|
BeautifulStoneSoup,
|
2022-01-24 04:07:52 +00:00
|
|
|
GuessedAtParserWarning,
|
|
|
|
MarkupResemblesLocatorWarning,
|
2022-11-07 18:06:49 +00:00
|
|
|
dammit,
|
2022-01-24 04:07:52 +00:00
|
|
|
)
|
|
|
|
from bs4.builder import (
|
2022-11-07 18:06:49 +00:00
|
|
|
builder_registry,
|
2022-01-24 04:07:52 +00:00
|
|
|
TreeBuilder,
|
|
|
|
ParserRejectedMarkup,
|
2020-01-30 01:07:26 +00:00
|
|
|
)
|
|
|
|
from bs4.element import (
|
2022-01-24 04:07:52 +00:00
|
|
|
Comment,
|
2020-01-30 01:07:26 +00:00
|
|
|
SoupStrainer,
|
2022-01-24 04:07:52 +00:00
|
|
|
Tag,
|
|
|
|
NavigableString,
|
2020-01-30 01:07:26 +00:00
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
|
|
|
|
from . import (
|
2020-01-30 01:07:26 +00:00
|
|
|
default_builder,
|
|
|
|
SoupTest,
|
|
|
|
skipIf,
|
|
|
|
)
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
try:
|
|
|
|
from bs4.builder import LXMLTreeBuilder, LXMLTreeBuilderForXML
|
|
|
|
LXML_PRESENT = True
|
|
|
|
except ImportError as e:
|
|
|
|
LXML_PRESENT = False
|
2022-11-07 18:06:49 +00:00
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
PYTHON_3_PRE_3_2 = (sys.version_info[0] == 3 and sys.version_info < (3,2))
|
|
|
|
|
|
|
|
class TestConstructor(SoupTest):
|
|
|
|
|
|
|
|
def test_short_unicode_input(self):
|
|
|
|
data = "<h1>éé</h1>"
|
|
|
|
soup = self.soup(data)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "éé" == soup.h1.string
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_embedded_null(self):
|
|
|
|
data = "<h1>foo\0bar</h1>"
|
|
|
|
soup = self.soup(data)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "foo\0bar" == soup.h1.string
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_exclude_encodings(self):
|
|
|
|
utf8_data = "Räksmörgås".encode("utf-8")
|
|
|
|
soup = self.soup(utf8_data, exclude_encodings=["utf-8"])
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "windows-1252" == soup.original_encoding
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_custom_builder_class(self):
|
|
|
|
# Verify that you can pass in a custom Builder class and
|
|
|
|
# it'll be instantiated with the appropriate keyword arguments.
|
|
|
|
class Mock(object):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.called_with = kwargs
|
|
|
|
self.is_xml = True
|
2022-01-24 04:07:52 +00:00
|
|
|
self.store_line_numbers = False
|
|
|
|
self.cdata_list_attributes = []
|
|
|
|
self.preserve_whitespace_tags = []
|
|
|
|
self.string_containers = {}
|
2020-01-30 01:07:26 +00:00
|
|
|
def initialize_soup(self, soup):
|
|
|
|
pass
|
2022-01-24 04:07:52 +00:00
|
|
|
def feed(self, markup):
|
|
|
|
self.fed = markup
|
|
|
|
def reset(self):
|
|
|
|
pass
|
|
|
|
def ignore(self, ignore):
|
|
|
|
pass
|
|
|
|
set_up_substitutions = can_be_empty_element = ignore
|
2020-01-30 01:07:26 +00:00
|
|
|
def prepare_markup(self, *args, **kwargs):
|
2022-01-24 04:07:52 +00:00
|
|
|
yield "prepared markup", "original encoding", "declared encoding", "contains replacement characters"
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
kwargs = dict(
|
|
|
|
var="value",
|
|
|
|
# This is a deprecated BS3-era keyword argument, which
|
|
|
|
# will be stripped out.
|
|
|
|
convertEntities=True,
|
|
|
|
)
|
|
|
|
with warnings.catch_warnings(record=True):
|
|
|
|
soup = BeautifulSoup('', builder=Mock, **kwargs)
|
|
|
|
assert isinstance(soup.builder, Mock)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert dict(var="value") == soup.builder.called_with
|
|
|
|
assert "prepared markup" == soup.builder.fed
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
# You can also instantiate the TreeBuilder yourself. In this
|
|
|
|
# case, that specific object is used and any keyword arguments
|
|
|
|
# to the BeautifulSoup constructor are ignored.
|
|
|
|
builder = Mock(**kwargs)
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
soup = BeautifulSoup(
|
|
|
|
'', builder=builder, ignored_value=True,
|
|
|
|
)
|
|
|
|
msg = str(w[0].message)
|
|
|
|
assert msg.startswith("Keyword arguments to the BeautifulSoup constructor will be ignored.")
|
2022-11-07 18:06:49 +00:00
|
|
|
assert builder == soup.builder
|
|
|
|
assert kwargs == builder.called_with
|
2020-01-30 01:07:26 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def test_parser_markup_rejection(self):
|
|
|
|
# If markup is completely rejected by the parser, an
|
|
|
|
# explanatory ParserRejectedMarkup exception is raised.
|
|
|
|
class Mock(TreeBuilder):
|
|
|
|
def feed(self, *args, **kwargs):
|
|
|
|
raise ParserRejectedMarkup("Nope.")
|
|
|
|
|
|
|
|
def prepare_markup(self, *args, **kwargs):
|
|
|
|
# We're going to try two different ways of preparing this markup,
|
|
|
|
# but feed() will reject both of them.
|
|
|
|
yield markup, None, None, False
|
|
|
|
yield markup, None, None, False
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
import re
|
2022-11-07 18:06:49 +00:00
|
|
|
with pytest.raises(ParserRejectedMarkup) as exc_info:
|
|
|
|
BeautifulSoup('', builder=Mock)
|
|
|
|
assert "The markup you provided was rejected by the parser. Trying a different parser or a different encoding may help." in str(exc_info.value)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def test_cdata_list_attributes(self):
|
|
|
|
# Most attribute values are represented as scalars, but the
|
|
|
|
# HTML standard says that some attributes, like 'class' have
|
|
|
|
# space-separated lists as values.
|
|
|
|
markup = '<a id=" an id " class=" a class "></a>'
|
|
|
|
soup = self.soup(markup)
|
|
|
|
|
|
|
|
# Note that the spaces are stripped for 'class' but not for 'id'.
|
|
|
|
a = soup.a
|
2022-11-07 18:06:49 +00:00
|
|
|
assert " an id " == a['id']
|
|
|
|
assert ["a", "class"] == a['class']
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
# TreeBuilder takes an argument called 'mutli_valued_attributes' which lets
|
|
|
|
# you customize or disable this. As always, you can customize the TreeBuilder
|
|
|
|
# by passing in a keyword argument to the BeautifulSoup constructor.
|
|
|
|
soup = self.soup(markup, builder=default_builder, multi_valued_attributes=None)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert " a class " == soup.a['class']
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
# Here are two ways of saying that `id` is a multi-valued
|
|
|
|
# attribute in this context, but 'class' is not.
|
|
|
|
for switcheroo in ({'*': 'id'}, {'a': 'id'}):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
# This will create a warning about not explicitly
|
|
|
|
# specifying a parser, but we'll ignore it.
|
|
|
|
soup = self.soup(markup, builder=None, multi_valued_attributes=switcheroo)
|
|
|
|
a = soup.a
|
2022-11-07 18:06:49 +00:00
|
|
|
assert ["an", "id"] == a['id']
|
|
|
|
assert " a class " == a['class']
|
2020-01-30 01:07:26 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def test_replacement_classes(self):
|
|
|
|
# Test the ability to pass in replacements for element classes
|
|
|
|
# which will be used when building the tree.
|
|
|
|
class TagPlus(Tag):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class StringPlus(NavigableString):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class CommentPlus(Comment):
|
|
|
|
pass
|
|
|
|
|
|
|
|
soup = self.soup(
|
|
|
|
"<a><b>foo</b>bar</a><!--whee-->",
|
|
|
|
element_classes = {
|
|
|
|
Tag: TagPlus,
|
|
|
|
NavigableString: StringPlus,
|
|
|
|
Comment: CommentPlus,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# The tree was built with TagPlus, StringPlus, and CommentPlus objects,
|
|
|
|
# rather than Tag, String, and Comment objects.
|
|
|
|
assert all(
|
|
|
|
isinstance(x, (TagPlus, StringPlus, CommentPlus))
|
|
|
|
for x in soup.recursiveChildGenerator()
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alternate_string_containers(self):
|
|
|
|
# Test the ability to customize the string containers for
|
|
|
|
# different types of tags.
|
|
|
|
class PString(NavigableString):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BString(NavigableString):
|
|
|
|
pass
|
|
|
|
|
|
|
|
soup = self.soup(
|
|
|
|
"<div>Hello.<p>Here is <b>some <i>bolded</i></b> text",
|
|
|
|
string_containers = {
|
|
|
|
'b': BString,
|
|
|
|
'p': PString,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# The string before the <p> tag is a regular NavigableString.
|
|
|
|
assert isinstance(soup.div.contents[0], NavigableString)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
# The string inside the <p> tag, but not inside the <i> tag,
|
|
|
|
# is a PString.
|
|
|
|
assert isinstance(soup.p.contents[0], PString)
|
|
|
|
|
|
|
|
# Every string inside the <b> tag is a BString, even the one that
|
|
|
|
# was also inside an <i> tag.
|
|
|
|
for s in soup.b.strings:
|
|
|
|
assert isinstance(s, BString)
|
|
|
|
|
|
|
|
# Now that parsing was complete, the string_container_stack
|
|
|
|
# (where this information was kept) has been cleared out.
|
2022-11-07 18:06:49 +00:00
|
|
|
assert [] == soup.string_container_stack
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
class TestWarnings(SoupTest):
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _assert_warning(self, warnings, cls):
|
|
|
|
for w in warnings:
|
|
|
|
if isinstance(w.message, cls):
|
|
|
|
return w
|
2022-11-07 18:06:49 +00:00
|
|
|
raise Exception("%s warning not found in %r" % (cls, warnings))
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
def _assert_no_parser_specified(self, w):
|
|
|
|
warning = self._assert_warning(w, GuessedAtParserWarning)
|
|
|
|
message = str(warning.message)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert message.startswith(BeautifulSoup.NO_PARSER_SPECIFIED_WARNING[:60])
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_warning_if_no_parser_specified(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2022-01-24 04:07:52 +00:00
|
|
|
soup = BeautifulSoup("<a><b></b></a>")
|
|
|
|
self._assert_no_parser_specified(w)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_warning_if_parser_specified_too_vague(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2022-01-24 04:07:52 +00:00
|
|
|
soup = BeautifulSoup("<a><b></b></a>", "html")
|
|
|
|
self._assert_no_parser_specified(w)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_no_warning_if_explicit_parser_specified(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2022-01-24 04:07:52 +00:00
|
|
|
soup = BeautifulSoup("<a><b></b></a>", "html.parser")
|
2022-11-07 18:06:49 +00:00
|
|
|
assert [] == w
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_parseOnlyThese_renamed_to_parse_only(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
soup = self.soup("<a><b></b></a>", parseOnlyThese=SoupStrainer("b"))
|
|
|
|
msg = str(w[0].message)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "parseOnlyThese" in msg
|
|
|
|
assert "parse_only" in msg
|
|
|
|
assert b"<b></b>" == soup.encode()
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_fromEncoding_renamed_to_from_encoding(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
utf8 = b"\xc3\xa9"
|
|
|
|
soup = self.soup(utf8, fromEncoding="utf8")
|
|
|
|
msg = str(w[0].message)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "fromEncoding" in msg
|
|
|
|
assert "from_encoding" in msg
|
|
|
|
assert "utf8" == soup.original_encoding
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_unrecognized_keyword_argument(self):
|
2022-11-07 18:06:49 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
self.soup("<a>", no_such_argument=True)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"extension",
|
|
|
|
['markup.html', 'markup.htm', 'markup.HTML', 'markup.txt',
|
|
|
|
'markup.xhtml', 'markup.xml', "/home/user/file", "c:\\user\file"]
|
|
|
|
)
|
|
|
|
def test_resembles_filename_warning(self, extension):
|
|
|
|
# A warning is issued if the "markup" looks like the name of
|
|
|
|
# an HTML or text file, or a full path to a file on disk.
|
2020-01-30 01:07:26 +00:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2022-11-07 18:06:49 +00:00
|
|
|
soup = self.soup("markup" + extension)
|
2022-01-24 04:07:52 +00:00
|
|
|
warning = self._assert_warning(w, MarkupResemblesLocatorWarning)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "looks more like a filename" in str(warning.message)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"extension",
|
|
|
|
['markuphtml', 'markup.com', '', 'markup.js']
|
|
|
|
)
|
|
|
|
def test_resembles_filename_no_warning(self, extension):
|
|
|
|
# The 'looks more like a filename' warning is not issued if
|
|
|
|
# the markup looks like a bare string, a domain name, or a
|
|
|
|
# file that's not an HTML file.
|
2022-01-24 04:07:52 +00:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2022-11-07 18:06:49 +00:00
|
|
|
soup = self.soup("markup" + extension)
|
|
|
|
assert [] == w
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def test_url_warning_with_bytes_url(self):
|
2022-11-07 18:06:49 +00:00
|
|
|
url = b"http://www.crummybytes.com/"
|
2020-01-30 01:07:26 +00:00
|
|
|
with warnings.catch_warnings(record=True) as warning_list:
|
2022-11-07 18:06:49 +00:00
|
|
|
soup = self.soup(url)
|
2022-01-24 04:07:52 +00:00
|
|
|
warning = self._assert_warning(
|
|
|
|
warning_list, MarkupResemblesLocatorWarning
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "looks more like a URL" in str(warning.message)
|
|
|
|
assert url not in str(warning.message).encode("utf8")
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def test_url_warning_with_unicode_url(self):
|
2022-11-07 18:06:49 +00:00
|
|
|
url = "http://www.crummyunicode.com/"
|
2020-01-30 01:07:26 +00:00
|
|
|
with warnings.catch_warnings(record=True) as warning_list:
|
|
|
|
# note - this url must differ from the bytes one otherwise
|
|
|
|
# python's warnings system swallows the second warning
|
2022-11-07 18:06:49 +00:00
|
|
|
soup = self.soup(url)
|
2022-01-24 04:07:52 +00:00
|
|
|
warning = self._assert_warning(
|
|
|
|
warning_list, MarkupResemblesLocatorWarning
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert "looks more like a URL" in str(warning.message)
|
|
|
|
assert url not in str(warning.message)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_url_warning_with_bytes_and_space(self):
|
2022-01-24 04:07:52 +00:00
|
|
|
# Here the markup contains something besides a URL, so no warning
|
|
|
|
# is issued.
|
2020-01-30 01:07:26 +00:00
|
|
|
with warnings.catch_warnings(record=True) as warning_list:
|
|
|
|
soup = self.soup(b"http://www.crummybytes.com/ is great")
|
2022-11-07 18:06:49 +00:00
|
|
|
assert not any("looks more like a URL" in str(w.message)
|
|
|
|
for w in warning_list)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_url_warning_with_unicode_and_space(self):
|
|
|
|
with warnings.catch_warnings(record=True) as warning_list:
|
2022-11-07 18:06:49 +00:00
|
|
|
soup = self.soup("http://www.crummyunicode.com/ is great")
|
|
|
|
assert not any("looks more like a URL" in str(w.message)
|
|
|
|
for w in warning_list)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSelectiveParsing(SoupTest):
|
|
|
|
|
|
|
|
def test_parse_with_soupstrainer(self):
|
|
|
|
markup = "No<b>Yes</b><a>No<b>Yes <c>Yes</c></b>"
|
|
|
|
strainer = SoupStrainer("b")
|
|
|
|
soup = self.soup(markup, parse_only=strainer)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert soup.encode() == b"<b>Yes</b><b>Yes <c>Yes</c></b>"
|
2020-01-30 01:07:26 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
|
|
|
|
class TestNewTag(SoupTest):
|
|
|
|
"""Test the BeautifulSoup.new_tag() method."""
|
|
|
|
def test_new_tag(self):
|
|
|
|
soup = self.soup("")
|
|
|
|
new_tag = soup.new_tag("foo", bar="baz", attrs={"name": "a name"})
|
|
|
|
assert isinstance(new_tag, Tag)
|
|
|
|
assert "foo" == new_tag.name
|
|
|
|
assert dict(bar="baz", name="a name") == new_tag.attrs
|
|
|
|
assert None == new_tag.parent
|
|
|
|
|
|
|
|
def test_tag_inherits_self_closing_rules_from_builder(self):
|
|
|
|
if LXML_PRESENT:
|
|
|
|
xml_soup = BeautifulSoup("", "lxml-xml")
|
|
|
|
xml_br = xml_soup.new_tag("br")
|
|
|
|
xml_p = xml_soup.new_tag("p")
|
|
|
|
|
|
|
|
# Both the <br> and <p> tag are empty-element, just because
|
|
|
|
# they have no contents.
|
|
|
|
assert b"<br/>" == xml_br.encode()
|
|
|
|
assert b"<p/>" == xml_p.encode()
|
|
|
|
|
|
|
|
html_soup = BeautifulSoup("", "html.parser")
|
|
|
|
html_br = html_soup.new_tag("br")
|
|
|
|
html_p = html_soup.new_tag("p")
|
|
|
|
|
|
|
|
# The HTML builder users HTML's rules about which tags are
|
|
|
|
# empty-element tags, and the new tags reflect these rules.
|
|
|
|
assert b"<br/>" == html_br.encode()
|
|
|
|
assert b"<p></p>" == html_p.encode()
|
|
|
|
|
|
|
|
class TestNewString(SoupTest):
|
|
|
|
"""Test the BeautifulSoup.new_string() method."""
|
|
|
|
def test_new_string_creates_navigablestring(self):
|
|
|
|
soup = self.soup("")
|
|
|
|
s = soup.new_string("foo")
|
|
|
|
assert "foo" == s
|
|
|
|
assert isinstance(s, NavigableString)
|
|
|
|
|
|
|
|
def test_new_string_can_create_navigablestring_subclass(self):
|
|
|
|
soup = self.soup("")
|
|
|
|
s = soup.new_string("foo", Comment)
|
|
|
|
assert "foo" == s
|
|
|
|
assert isinstance(s, Comment)
|
|
|
|
|
|
|
|
|
|
|
|
class TestPickle(SoupTest):
|
|
|
|
# Test our ability to pickle the BeautifulSoup object itself.
|
|
|
|
|
|
|
|
def test_normal_pickle(self):
|
|
|
|
soup = self.soup("<a>some markup</a>")
|
|
|
|
pickled = pickle.dumps(soup)
|
|
|
|
unpickled = pickle.loads(pickled)
|
|
|
|
assert "some markup" == unpickled.a.string
|
|
|
|
|
|
|
|
def test_pickle_with_no_builder(self):
|
|
|
|
# We had a bug that prevented pickling from working if
|
|
|
|
# the builder wasn't set.
|
|
|
|
soup = self.soup("some markup")
|
|
|
|
soup.builder = None
|
|
|
|
pickled = pickle.dumps(soup)
|
|
|
|
unpickled = pickle.loads(pickled)
|
|
|
|
assert "some markup" == unpickled.string
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
class TestEncodingConversion(SoupTest):
|
|
|
|
# Test Beautiful Soup's ability to decode and encode from various
|
|
|
|
# encodings.
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
def setup_method(self):
|
2020-01-30 01:07:26 +00:00
|
|
|
self.unicode_data = '<html><head><meta charset="utf-8"/></head><body><foo>Sacr\N{LATIN SMALL LETTER E WITH ACUTE} bleu!</foo></body></html>'
|
|
|
|
self.utf8_data = self.unicode_data.encode("utf-8")
|
|
|
|
# Just so you know what it looks like.
|
2022-11-07 18:06:49 +00:00
|
|
|
assert self.utf8_data == b'<html><head><meta charset="utf-8"/></head><body><foo>Sacr\xc3\xa9 bleu!</foo></body></html>'
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_ascii_in_unicode_out(self):
|
|
|
|
# ASCII input is converted to Unicode. The original_encoding
|
|
|
|
# attribute is set to 'utf-8', a superset of ASCII.
|
2022-11-07 18:06:49 +00:00
|
|
|
chardet = dammit.chardet_dammit
|
2020-01-30 01:07:26 +00:00
|
|
|
logging.disable(logging.WARNING)
|
|
|
|
try:
|
|
|
|
def noop(str):
|
|
|
|
return None
|
|
|
|
# Disable chardet, which will realize that the ASCII is ASCII.
|
2022-11-07 18:06:49 +00:00
|
|
|
dammit.chardet_dammit = noop
|
2020-01-30 01:07:26 +00:00
|
|
|
ascii = b"<foo>a</foo>"
|
|
|
|
soup_from_ascii = self.soup(ascii)
|
|
|
|
unicode_output = soup_from_ascii.decode()
|
2022-11-07 18:06:49 +00:00
|
|
|
assert isinstance(unicode_output, str)
|
|
|
|
assert unicode_output == self.document_for(ascii.decode())
|
|
|
|
assert soup_from_ascii.original_encoding.lower() == "utf-8"
|
2020-01-30 01:07:26 +00:00
|
|
|
finally:
|
|
|
|
logging.disable(logging.NOTSET)
|
2022-11-07 18:06:49 +00:00
|
|
|
dammit.chardet_dammit = chardet
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_unicode_in_unicode_out(self):
|
|
|
|
# Unicode input is left alone. The original_encoding attribute
|
|
|
|
# is not set.
|
|
|
|
soup_from_unicode = self.soup(self.unicode_data)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert soup_from_unicode.decode() == self.unicode_data
|
|
|
|
assert soup_from_unicode.foo.string == 'Sacr\xe9 bleu!'
|
|
|
|
assert soup_from_unicode.original_encoding == None
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_utf8_in_unicode_out(self):
|
|
|
|
# UTF-8 input is converted to Unicode. The original_encoding
|
|
|
|
# attribute is set.
|
|
|
|
soup_from_utf8 = self.soup(self.utf8_data)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert soup_from_utf8.decode() == self.unicode_data
|
|
|
|
assert soup_from_utf8.foo.string == 'Sacr\xe9 bleu!'
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def test_utf8_out(self):
|
|
|
|
# The internal data structures can be encoded as UTF-8.
|
|
|
|
soup_from_unicode = self.soup(self.unicode_data)
|
2022-11-07 18:06:49 +00:00
|
|
|
assert soup_from_unicode.encode('utf-8') == self.utf8_data
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
@skipIf(
|
|
|
|
PYTHON_3_PRE_3_2,
|
|
|
|
"Bad HTMLParser detected; skipping test of non-ASCII characters in attribute name.")
|
|
|
|
def test_attribute_name_containing_unicode_characters(self):
|
|
|
|
markup = '<div><a \N{SNOWMAN}="snowman"></a></div>'
|
2022-11-07 18:06:49 +00:00
|
|
|
assert self.soup(markup).div.encode("utf8") == markup.encode("utf8")
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|