Package CHEM :: Package DB :: Package rdb :: Module BeautifulSoup :: Class BeautifulSoup
[hide private]
[frames] | no frames]

Class BeautifulSoup



          PageElement --+        
                        |        
                      Tag --+    
                            |    
markupbase.ParserBase --+   |    
                        |   |    
       sgmllib.SGMLParser --+    
                            |    
           BeautifulStoneSoup --+
                                |
                               BeautifulSoup
Known Subclasses:
ICantBelieveItsBeautifulSoup, MinimalSoup, RobustHTMLParser

This parser knows the following facts about HTML:

* Some tags have no closing tag and should be interpreted as being
  closed as soon as they are encountered.

* The text inside some tags (ie. 'script') may contain tags which
  are not really part of the document and which should be parsed
  as text, not tags. If you want to parse the text as tags, you can
  always fetch it and parse it explicitly.

* Tag nesting rules:

  Most tags can't be nested at all. For instance, the occurance of
  a <p> tag should implicitly close the previous <p> tag.

   <p>Para1<p>Para2
    should be transformed into:
   <p>Para1</p><p>Para2

  Some tags can be nested arbitrarily. For instance, the occurance
  of a <blockquote> tag should _not_ implicitly close the previous
  <blockquote> tag.

   Alice said: <blockquote>Bob said: <blockquote>Blah
    should NOT be transformed into:
   Alice said: <blockquote>Bob said: </blockquote><blockquote>Blah

  Some tags can be nested, but the nesting is reset by the
  interposition of other tags. For instance, a <tr> tag should
  implicitly close the previous <tr> tag within the same <table>,
  but not close a <tr> tag in another table.

   <table><tr>Blah<tr>Blah
    should be transformed into:
   <table><tr>Blah</tr><tr>Blah
    but,
   <tr>Blah<table><tr>Blah
    should NOT be transformed into
   <tr>Blah<table></tr><tr>Blah

Differing assumptions about tag nesting rules are a major source
of problems with the BeautifulSoup class. If BeautifulSoup is not
treating as nestable a tag your page author treats as nestable,
try ICantBelieveItsBeautifulSoup, MinimalSoup, or
BeautifulStoneSoup before writing your own subclass.



Instance Methods [hide private]
 
__init__(self, *args, **kwargs)
The Soup object is initialized as the 'root tag', and the provided markup (which can be a string or a file-like object) is fed into the underlying parser.
 
start_meta(self, attrs)
Beautiful Soup can detect a charset included in a META tag, try to convert the document to that charset, and re-parse the document from the beginning.

Inherited from BeautifulStoneSoup: __getattr__, endData, handle_charref, handle_comment, handle_data, handle_decl, handle_entityref, handle_pi, isSelfClosingTag, parse_declaration, popTag, pushTag, reset, unknown_endtag, unknown_starttag

Inherited from Tag: __call__, __contains__, __delitem__, __eq__, __getitem__, __iter__, __len__, __ne__, __nonzero__, __repr__, __setitem__, __str__, __unicode__, append, childGenerator, fetch, fetchText, find, findAll, findChild, findChildren, first, firstText, get, has_key, prettify, recursiveChildGenerator, renderContents

Inherited from Tag (private): _getAttrMap

Inherited from PageElement: extract, fetchNextSiblings, fetchParents, fetchPrevious, fetchPreviousSiblings, findAllNext, findAllPrevious, findNext, findNextSibling, findNextSiblings, findParent, findParents, findPrevious, findPreviousSibling, findPreviousSiblings, insert, nextGenerator, nextSiblingGenerator, parentGenerator, previousGenerator, previousSiblingGenerator, replaceWith, setup, substituteEncoding, toEncoding

Inherited from PageElement (private): _findAll, _findOne, _lastRecursiveChild

Inherited from sgmllib.SGMLParser: close, convert_charref, convert_codepoint, convert_entityref, error, feed, finish_endtag, finish_shorttag, finish_starttag, get_starttag_text, goahead, handle_endtag, handle_starttag, parse_endtag, parse_pi, parse_starttag, report_unbalanced, setliteral, setnomoretags, unknown_charref, unknown_entityref

Inherited from sgmllib.SGMLParser (private): _convert_ref

Inherited from markupbase.ParserBase: getpos, parse_comment, parse_marked_section, unknown_decl, updatepos

Inherited from markupbase.ParserBase (private): _parse_doctype_attlist, _parse_doctype_element, _parse_doctype_entity, _parse_doctype_notation, _parse_doctype_subset, _scan_name

Class Variables [hide private]
  SELF_CLOSING_TAGS = {'base': None, 'br': None, 'frame': None, ...
  QUOTE_TAGS = {'script': None}
  NESTABLE_INLINE_TAGS = ['span', 'font', 'q', 'object', 'bdo', ...
  NESTABLE_BLOCK_TAGS = ['blockquote', 'div', 'fieldset', 'ins',...
  NESTABLE_LIST_TAGS = {'dd': ['dl'], 'dl': [], 'dt': ['dl'], 'l...
  NESTABLE_TABLE_TAGS = {'table': [], 'tbody': ['table'], 'td': ...
  NON_NESTABLE_BLOCK_TAGS = ['address', 'form', 'p', 'pre']
  RESET_NESTING_TAGS = {'address': None, 'blockquote': None, 'dd...
  NESTABLE_TAGS = {'bdo': [], 'blockquote': [], 'center': [], 'd...
  CHARSET_RE = re.compile(r'((^|;)\s*charset=)([^;]*)')

Inherited from BeautifulStoneSoup: HTML_ENTITIES, MARKUP_MASSAGE, ROOT_TAG_NAME, XML_ENTITIES, XML_ENTITY_LIST, i

Inherited from Tag: XML_SPECIAL_CHARS_TO_ENTITIES

Inherited from sgmllib.SGMLParser: entity_or_charref, entitydefs

Inherited from sgmllib.SGMLParser (private): _decl_otherchars

Method Details [hide private]

__init__(self, *args, **kwargs)
(Constructor)

 
The Soup object is initialized as the 'root tag', and the
provided markup (which can be a string or a file-like object)
is fed into the underlying parser. 

sgmllib will process most bad HTML, and the BeautifulSoup
class has some tricks for dealing with some HTML that kills
sgmllib, but Beautiful Soup can nonetheless choke or lose data
if your data uses self-closing tags or declarations
incorrectly.

By default, Beautiful Soup uses regexes to sanitize input,
avoiding the vast majority of these problems. If the problems
don't apply to you, pass in False for markupMassage, and
you'll get better performance.

The default parser massage techniques fix the two most common
instances of invalid HTML that choke sgmllib:

 <br/> (No space between name of closing tag and tag close)
 <! --Comment--> (Extraneous whitespace in declaration)

You can pass in a custom list of (RE object, replace method)
tuples to get Beautiful Soup to scrub your input the way you
want.

Overrides: BeautifulStoneSoup.__init__
(inherited documentation)

Class Variable Details [hide private]

SELF_CLOSING_TAGS

Value:
{'base': None,
 'br': None,
 'frame': None,
 'hr': None,
 'img': None,
 'input': None,
 'link': None,
 'meta': None,
...

NESTABLE_INLINE_TAGS

Value:
['span', 'font', 'q', 'object', 'bdo', 'sub', 'sup', 'center']

NESTABLE_BLOCK_TAGS

Value:
['blockquote', 'div', 'fieldset', 'ins', 'del']

NESTABLE_LIST_TAGS

Value:
{'dd': ['dl'],
 'dl': [],
 'dt': ['dl'],
 'li': ['ul', 'ol'],
 'ol': [],
 'ul': []}

NESTABLE_TABLE_TAGS

Value:
{'table': [],
 'tbody': ['table'],
 'td': ['tr'],
 'tfoot': ['table'],
 'th': ['tr'],
 'thead': ['table'],
 'tr': ['table', 'tbody', 'tfoot', 'thead']}

RESET_NESTING_TAGS

Value:
{'address': None,
 'blockquote': None,
 'dd': ['dl'],
 'del': None,
 'div': None,
 'dl': [],
 'dt': ['dl'],
 'fieldset': None,
...

NESTABLE_TAGS

Value:
{'bdo': [],
 'blockquote': [],
 'center': [],
 'dd': ['dl'],
 'del': [],
 'div': [],
 'dl': [],
 'dt': ['dl'],
...