Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/bs4/_warnings.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Define some custom warnings."""
4class GuessedAtParserWarning(UserWarning):
5 """The warning issued when BeautifulSoup has to guess what parser to
6 use -- probably because no parser was specified in the constructor.
7 """
9 MESSAGE: str = """No parser was explicitly specified, so I'm using the best available %(markup_type)s parser for this system ("%(parser)s"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
11The code that caused this warning is on line %(line_number)s of the file %(filename)s. To get rid of this warning, pass the additional argument 'features="%(parser)s"' to the BeautifulSoup constructor.
12"""
15class UnusualUsageWarning(UserWarning):
16 """A superclass for warnings issued when Beautiful Soup sees
17 something that is typically the result of a mistake in the calling
18 code, but might be intentional on the part of the user. If it is
19 in fact intentional, you can filter the individual warning class
20 to get rid of the warning. If you don't like Beautiful Soup
21 second-guessing what you are doing, you can filter the
22 UnusualUsageWarningclass itself and get rid of these entirely.
23 """
26class MarkupResemblesLocatorWarning(UnusualUsageWarning):
27 """The warning issued when BeautifulSoup is given 'markup' that
28 actually looks like a resource locator -- a URL or a path to a file
29 on disk.
30 """
32 #: :meta private:
33 GENERIC_MESSAGE: str = """
35However, if you want to parse some data that happens to look like a %(what)s, then nothing has gone wrong: you are using Beautiful Soup correctly, and this warning is spurious and can be filtered. To make this warning go away, run this code before calling the BeautifulSoup constructor:
37 from bs4 import MarkupResemblesLocatorWarning
38 import warnings
40 warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)
41 """
43 URL_MESSAGE: str = (
44 """The input passed in on this line looks more like a URL than HTML or XML.
46If you meant to use Beautiful Soup to parse the web page found at a certain URL, then something has gone wrong. You should use an Python package like 'requests' to fetch the content behind the URL. Once you have the content as a string, you can feed that string into Beautiful Soup."""
47 + GENERIC_MESSAGE
48 )
50 FILENAME_MESSAGE: str = (
51 """The input passed in on this line looks more like a filename than HTML or XML.
53If you meant to use Beautiful Soup to parse the contents of a file on disk, then something has gone wrong. You should open the file first, using code like this:
55 filehandle = open(your filename)
57You can then feed the open filehandle into Beautiful Soup instead of using the filename."""
58 + GENERIC_MESSAGE
59 )
62class AttributeResemblesVariableWarning(UnusualUsageWarning, SyntaxWarning):
63 """The warning issued when Beautiful Soup suspects a provided
64 attribute name may actually be the misspelled name of a Beautiful
65 Soup variable. Generally speaking, this is only used in cases like
66 "_class" where it's very unlikely the user would be referencing an
67 XML attribute with that name.
68 """
70 MESSAGE: str = """%(original)r is an unusual attribute name and is a common misspelling for %(autocorrect)r.
72If you meant %(autocorrect)r, change your code to use it, and this warning will go away.
74If you really did mean to check the %(original)r attribute, this warning is spurious and can be filtered. To make it go away, run this code before creating your BeautifulSoup object:
76 from bs4 import AttributeResemblesVariableWarning
77 import warnings
79 warnings.filterwarnings("ignore", category=AttributeResemblesVariableWarning)
80"""
83class XMLParsedAsHTMLWarning(UnusualUsageWarning):
84 """The warning issued when an HTML parser is used to parse
85 XML that is not (as far as we can tell) XHTML.
86 """
88 MESSAGE: str = """It looks like you're using an HTML parser to parse an XML document.
90Assuming this really is an XML document, what you're doing might work, but you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the Python package 'lxml' installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
92If you want or need to use an HTML parser on this document, you can make this warning go away by filtering it. To do that, run this code before calling the BeautifulSoup constructor:
94 from bs4 import XMLParsedAsHTMLWarning
95 import warnings
97 warnings.filterwarnings("ignore", category=XMLParsedAsHTMLWarning)
98"""