_warnings.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """Define some custom warnings."""
  2. class GuessedAtParserWarning(UserWarning):
  3. """The warning issued when BeautifulSoup has to guess what parser to
  4. use -- probably because no parser was specified in the constructor.
  5. """
  6. 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.
  7. The 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.
  8. """
  9. class UnusualUsageWarning(UserWarning):
  10. """A superclass for warnings issued when Beautiful Soup sees
  11. something that is typically the result of a mistake in the calling
  12. code, but might be intentional on the part of the user. If it is
  13. in fact intentional, you can filter the individual warning class
  14. to get rid of the warning. If you don't like Beautiful Soup
  15. second-guessing what you are doing, you can filter the
  16. UnusualUsageWarningclass itself and get rid of these entirely.
  17. """
  18. class MarkupResemblesLocatorWarning(UnusualUsageWarning):
  19. """The warning issued when BeautifulSoup is given 'markup' that
  20. actually looks like a resource locator -- a URL or a path to a file
  21. on disk.
  22. """
  23. #: :meta private:
  24. GENERIC_MESSAGE: str = """
  25. However, 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:
  26. from bs4 import MarkupResemblesLocatorWarning
  27. import warnings
  28. warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)
  29. """
  30. URL_MESSAGE: str = (
  31. """The input passed in on this line looks more like a URL than HTML or XML.
  32. If 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."""
  33. + GENERIC_MESSAGE
  34. )
  35. FILENAME_MESSAGE: str = (
  36. """The input passed in on this line looks more like a filename than HTML or XML.
  37. If 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:
  38. filehandle = open(your filename)
  39. You can then feed the open filehandle into Beautiful Soup instead of using the filename."""
  40. + GENERIC_MESSAGE
  41. )
  42. class AttributeResemblesVariableWarning(UnusualUsageWarning, SyntaxWarning):
  43. """The warning issued when Beautiful Soup suspects a provided
  44. attribute name may actually be the misspelled name of a Beautiful
  45. Soup variable. Generally speaking, this is only used in cases like
  46. "_class" where it's very unlikely the user would be referencing an
  47. XML attribute with that name.
  48. """
  49. MESSAGE: str = """%(original)r is an unusual attribute name and is a common misspelling for %(autocorrect)r.
  50. If you meant %(autocorrect)r, change your code to use it, and this warning will go away.
  51. If 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:
  52. from bs4 import AttributeResemblesVariableWarning
  53. import warnings
  54. warnings.filterwarnings("ignore", category=AttributeResemblesVariableWarning)
  55. """
  56. class XMLParsedAsHTMLWarning(UnusualUsageWarning):
  57. """The warning issued when an HTML parser is used to parse
  58. XML that is not (as far as we can tell) XHTML.
  59. """
  60. MESSAGE: str = """It looks like you're using an HTML parser to parse an XML document.
  61. Assuming 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.
  62. If 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:
  63. from bs4 import XMLParsedAsHTMLWarning
  64. import warnings
  65. warnings.filterwarnings("ignore", category=XMLParsedAsHTMLWarning)
  66. """