PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/python/pymel/util/external/BeautifulSoup.py

https://bitbucket.org/jspatrick/emacs
Python | 1151 lines | 1004 code | 11 blank | 136 comment | 39 complexity | f38a310e884b7c68e62dacda3bd0a5a6 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. import exceptions
  2. """
  3. Beautiful Soup
  4. Elixir and Tonic
  5. "The Screen-Scraper's Friend"
  6. http://www.crummy.com/software/BeautifulSoup/
  7. Beautiful Soup parses a (possibly invalid) XML or HTML document into a
  8. tree representation. It provides methods and Pythonic idioms that make
  9. it easy to navigate, search, and modify the tree.
  10. A well-formed XML/HTML document yields a well-formed data
  11. structure. An ill-formed XML/HTML document yields a correspondingly
  12. ill-formed data structure. If your document is only locally
  13. well-formed, you can use this library to find and process the
  14. well-formed part of it.
  15. Beautiful Soup works with Python 2.2 and up. It has no external
  16. dependencies, but you'll have more success at converting data to UTF-8
  17. if you also install these three packages:
  18. * chardet, for auto-detecting character encodings
  19. http://chardet.feedparser.org/
  20. * cjkcodecs and iconv_codec, which add more encodings to the ones supported
  21. by stock Python.
  22. http://cjkpython.i18n.org/
  23. Beautiful Soup defines classes for two main parsing strategies:
  24. * BeautifulStoneSoup, for parsing XML, SGML, or your domain-specific
  25. language that kind of looks like XML.
  26. * BeautifulSoup, for parsing run-of-the-mill HTML code, be it valid
  27. or invalid. This class has web browser-like heuristics for
  28. obtaining a sensible parse tree in the face of common HTML errors.
  29. Beautiful Soup also defines a class (UnicodeDammit) for autodetecting
  30. the encoding of an HTML or XML document, and converting it to
  31. Unicode. Much of this code is taken from Mark Pilgrim's Universal Feed Parser.
  32. For more than you ever wanted to know about Beautiful Soup, see the
  33. documentation:
  34. http://www.crummy.com/software/BeautifulSoup/documentation.html
  35. Here, have some legalese:
  36. Copyright (c) 2004-2008, Leonard Richardson
  37. All rights reserved.
  38. Redistribution and use in source and binary forms, with or without
  39. modification, are permitted provided that the following conditions are
  40. met:
  41. * Redistributions of source code must retain the above copyright
  42. notice, this list of conditions and the following disclaimer.
  43. * Redistributions in binary form must reproduce the above
  44. copyright notice, this list of conditions and the following
  45. disclaimer in the documentation and/or other materials provided
  46. with the distribution.
  47. * Neither the name of the the Beautiful Soup Consortium and All
  48. Night Kosher Bakery nor the names of its contributors may be
  49. used to endorse or promote products derived from this software
  50. without specific prior written permission.
  51. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  55. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  56. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  57. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  58. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  59. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  60. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  61. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE, DAMMIT.
  62. """
  63. import codecs
  64. import markupbase
  65. import re
  66. import sgmllib
  67. import types
  68. from sgmllib import *
  69. class SoupStrainer:
  70. """
  71. Encapsulates a number of ways of matching a markup element (tag or
  72. text).
  73. """
  74. def __init__(self, name=None, attrs={}, text=None, **kwargs):
  75. pass
  76. def __str__(self):
  77. pass
  78. def search(self, markup):
  79. pass
  80. def searchTag(self, markupName=None, markupAttrs={}):
  81. pass
  82. class UnicodeDammit:
  83. """
  84. A class for detecting the encoding of a *ML document and
  85. converting it to a Unicode string. If the source encoding is
  86. windows-1252, can replace MS smart quotes with their HTML or XML
  87. equivalents.
  88. """
  89. def __init__(self, markup, overrideEncodings=[], smartQuotesTo='xml', isHTML=False):
  90. pass
  91. def find_codec(self, charset):
  92. pass
  93. CHARSET_ALIASES = None
  94. EBCDIC_TO_ASCII_MAP = None
  95. MS_CHARS = None
  96. class StopParsing(Exception):
  97. __weakref__ = None
  98. class ResultSet(list):
  99. """
  100. A ResultSet is just a list that keeps track of the SoupStrainer
  101. that created it.
  102. """
  103. def __init__(self, source):
  104. pass
  105. __dict__ = None
  106. __weakref__ = None
  107. class PageElement:
  108. """
  109. Contains the navigational information for some part of the page
  110. (either a tag or a piece of text)
  111. """
  112. def append(self, tag):
  113. """
  114. Appends the given tag to the contents of this tag.
  115. """
  116. pass
  117. def extract(self):
  118. """
  119. Destructively rips this element out of the tree.
  120. """
  121. pass
  122. def fetchNextSiblings(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  123. """
  124. Returns the siblings of this Tag that match the given
  125. criteria and appear after this Tag in the document.
  126. """
  127. pass
  128. def fetchParents(self, name=None, attrs={}, limit=None, **kwargs):
  129. """
  130. Returns the parents of this Tag that match the given
  131. criteria.
  132. """
  133. pass
  134. def fetchPrevious(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  135. """
  136. Returns all items that match the given criteria and appear
  137. before this Tag in the document.
  138. """
  139. pass
  140. def fetchPreviousSiblings(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  141. """
  142. Returns the siblings of this Tag that match the given
  143. criteria and appear before this Tag in the document.
  144. """
  145. pass
  146. def findAllNext(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  147. """
  148. Returns all items that match the given criteria and appear
  149. after this Tag in the document.
  150. """
  151. pass
  152. def findAllPrevious(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  153. """
  154. Returns all items that match the given criteria and appear
  155. before this Tag in the document.
  156. """
  157. pass
  158. def findNext(self, name=None, attrs={}, text=None, **kwargs):
  159. """
  160. Returns the first item that matches the given criteria and
  161. appears after this Tag in the document.
  162. """
  163. pass
  164. def findNextSibling(self, name=None, attrs={}, text=None, **kwargs):
  165. """
  166. Returns the closest sibling to this Tag that matches the
  167. given criteria and appears after this Tag in the document.
  168. """
  169. pass
  170. def findNextSiblings(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  171. """
  172. Returns the siblings of this Tag that match the given
  173. criteria and appear after this Tag in the document.
  174. """
  175. pass
  176. def findParent(self, name=None, attrs={}, **kwargs):
  177. """
  178. Returns the closest parent of this Tag that matches the given
  179. criteria.
  180. """
  181. pass
  182. def findParents(self, name=None, attrs={}, limit=None, **kwargs):
  183. """
  184. Returns the parents of this Tag that match the given
  185. criteria.
  186. """
  187. pass
  188. def findPrevious(self, name=None, attrs={}, text=None, **kwargs):
  189. """
  190. Returns the first item that matches the given criteria and
  191. appears before this Tag in the document.
  192. """
  193. pass
  194. def findPreviousSibling(self, name=None, attrs={}, text=None, **kwargs):
  195. """
  196. Returns the closest sibling to this Tag that matches the
  197. given criteria and appears before this Tag in the document.
  198. """
  199. pass
  200. def findPreviousSiblings(self, name=None, attrs={}, text=None, limit=None, **kwargs):
  201. """
  202. Returns the siblings of this Tag that match the given
  203. criteria and appear before this Tag in the document.
  204. """
  205. pass
  206. def insert(self, position, newChild):
  207. pass
  208. def nextGenerator(self):
  209. """
  210. #These Generators can be used to navigate starting from both
  211. #NavigableStrings and Tags.
  212. """
  213. pass
  214. def nextSiblingGenerator(self):
  215. pass
  216. def parentGenerator(self):
  217. pass
  218. def previousGenerator(self):
  219. pass
  220. def previousSiblingGenerator(self):
  221. pass
  222. def replaceWith(self, replaceWith):
  223. pass
  224. def setup(self, parent=None, previous=None):
  225. """
  226. Sets up the initial relations between this element and
  227. other elements.
  228. """
  229. pass
  230. def substituteEncoding(self, str, encoding=None):
  231. """
  232. # Utility methods
  233. """
  234. pass
  235. def toEncoding(self, s, encoding=None):
  236. """
  237. Encodes an object to a string in some encoding, or to Unicode.
  238. .
  239. """
  240. pass
  241. class Tag(PageElement):
  242. """
  243. Represents a found HTML tag with its attributes and contents.
  244. """
  245. def __call__(self, *args, **kwargs):
  246. """
  247. Calling a tag like a function is the same as calling its
  248. findAll() method. Eg. tag('a') returns a list of all the A tags
  249. found within this tag.
  250. """
  251. pass
  252. def __contains__(self, x):
  253. pass
  254. def __delitem__(self, key):
  255. """
  256. Deleting tag[key] deletes all 'key' attributes for the tag.
  257. """
  258. pass
  259. def __eq__(self, other):
  260. """
  261. Returns true iff this tag has the same name, the same attributes,
  262. and the same contents (recursively) as the given tag.
  263. NOTE: right now this will return false if two tags have the
  264. same attributes in a different order. Should this be fixed?
  265. """
  266. pass
  267. def __getattr__(self, tag):
  268. pass
  269. def __getitem__(self, key):
  270. """
  271. tag[key] returns the value of the 'key' attribute for the tag,
  272. and throws an exception if it's not there.
  273. """
  274. pass
  275. def __init__(self, parser, name, attrs=None, parent=None, previous=None):
  276. """
  277. Basic constructor.
  278. """
  279. pass
  280. def __iter__(self):
  281. """
  282. Iterating over a tag iterates over its contents.
  283. """
  284. pass
  285. def __len__(self):
  286. """
  287. The length of a tag is the length of its list of contents.
  288. """
  289. pass
  290. def __ne__(self, other):
  291. """
  292. Returns true iff this tag is not identical to the other tag,
  293. as defined in __eq__.
  294. """
  295. pass
  296. def __nonzero__(self):
  297. """
  298. A tag is non-None even if it has no contents.
  299. """
  300. pass
  301. def __repr__(self, encoding='utf-8'):
  302. """
  303. Renders this tag as a string.
  304. """
  305. pass
  306. def __setitem__(self, key, value):
  307. """
  308. Setting tag[key] sets the value of the 'key' attribute for the
  309. tag.
  310. """
  311. pass
  312. def __str__(self, encoding='utf-8', prettyPrint=False, indentLevel=0):
  313. """
  314. Returns a string or Unicode representation of this tag and
  315. its contents. To get Unicode, pass None for encoding.
  316. NOTE: since Python's HTML parser consumes whitespace, this
  317. method is not certain to reproduce the whitespace present in
  318. the original string.
  319. """
  320. pass
  321. def __unicode__(self):
  322. pass
  323. def childGenerator(self):
  324. """
  325. #Generator methods
  326. """
  327. pass
  328. def decompose(self):
  329. """
  330. Recursively destroys the contents of this tree.
  331. """
  332. pass
  333. def fetch(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs):
  334. """
  335. Extracts a list of Tag objects that match the given
  336. criteria. You can specify the name of the Tag and any
  337. attributes you want the Tag to have.
  338. The value of a key-value pair in the 'attrs' map can be a
  339. string, a list of strings, a regular expression object, or a
  340. callable that takes a string and returns whether or not the
  341. string matches for some custom definition of 'matches'. The
  342. same is true of the tag name.
  343. """
  344. pass
  345. def fetchText(self, text=None, recursive=True, limit=None):
  346. pass
  347. def find(self, name=None, attrs={}, recursive=True, text=None, **kwargs):
  348. """
  349. Return only the first child of this Tag matching the given
  350. criteria.
  351. """
  352. pass
  353. def findAll(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs):
  354. """
  355. Extracts a list of Tag objects that match the given
  356. criteria. You can specify the name of the Tag and any
  357. attributes you want the Tag to have.
  358. The value of a key-value pair in the 'attrs' map can be a
  359. string, a list of strings, a regular expression object, or a
  360. callable that takes a string and returns whether or not the
  361. string matches for some custom definition of 'matches'. The
  362. same is true of the tag name.
  363. """
  364. pass
  365. def findChild(self, name=None, attrs={}, recursive=True, text=None, **kwargs):
  366. """
  367. Return only the first child of this Tag matching the given
  368. criteria.
  369. """
  370. pass
  371. def findChildren(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs):
  372. """
  373. Extracts a list of Tag objects that match the given
  374. criteria. You can specify the name of the Tag and any
  375. attributes you want the Tag to have.
  376. The value of a key-value pair in the 'attrs' map can be a
  377. string, a list of strings, a regular expression object, or a
  378. callable that takes a string and returns whether or not the
  379. string matches for some custom definition of 'matches'. The
  380. same is true of the tag name.
  381. """
  382. pass
  383. def first(self, name=None, attrs={}, recursive=True, text=None, **kwargs):
  384. """
  385. Return only the first child of this Tag matching the given
  386. criteria.
  387. """
  388. pass
  389. def firstText(self, text=None, recursive=True):
  390. pass
  391. def get(self, key, default=None):
  392. """
  393. Returns the value of the 'key' attribute for the tag, or
  394. the value given for 'default' if it doesn't have that
  395. attribute.
  396. """
  397. pass
  398. def has_key(self, key):
  399. pass
  400. def prettify(self, encoding='utf-8'):
  401. pass
  402. def recursiveChildGenerator(self):
  403. pass
  404. def renderContents(self, encoding='utf-8', prettyPrint=False, indentLevel=0):
  405. """
  406. Renders the contents of this tag as a string in the given
  407. encoding. If encoding is None, returns a Unicode string..
  408. """
  409. pass
  410. BARE_AMPERSAND_OR_BRACKET = None
  411. XML_ENTITIES_TO_SPECIAL_CHARS = None
  412. XML_SPECIAL_CHARS_TO_ENTITIES = None
  413. class NavigableString(unicode, PageElement):
  414. def __getattr__(self, attr):
  415. """
  416. text.string gives you text. This is for backwards
  417. compatibility for Navigable*String, but for CData* it lets you
  418. get the string without the CData wrapper.
  419. """
  420. pass
  421. def __getnewargs__(self):
  422. pass
  423. def __str__(self, encoding='utf-8'):
  424. pass
  425. def __unicode__(self):
  426. pass
  427. def __new__(cls, value):
  428. """
  429. Create a new NavigableString.
  430. When unpickling a NavigableString, this method is called with
  431. the string in DEFAULT_OUTPUT_ENCODING. That encoding needs to be
  432. passed in to the superclass's __new__ or the superclass won't know
  433. how to handle non-ASCII characters.
  434. """
  435. pass
  436. __dict__ = None
  437. __weakref__ = None
  438. class Comment(NavigableString):
  439. def __str__(self, encoding='utf-8'):
  440. pass
  441. class CData(NavigableString):
  442. def __str__(self, encoding='utf-8'):
  443. pass
  444. class BeautifulStoneSoup(Tag, SGMLParser):
  445. """
  446. This class contains the basic parser and search code. It defines
  447. a parser that knows nothing about tag behavior except for the
  448. following:
  449. You can't close a tag without closing all the tags it encloses.
  450. That is, "<foo><bar></foo>" actually means
  451. "<foo><bar></bar></foo>".
  452. [Another possible explanation is "<foo><bar /></foo>", but since
  453. this class defines no SELF_CLOSING_TAGS, it will never use that
  454. explanation.]
  455. This class is useful for parsing XML or made-up markup languages,
  456. or when BeautifulSoup makes an assumption counter to what you were
  457. expecting.
  458. """
  459. def __getattr__(self, methodName):
  460. """
  461. This method routes method call requests to either the SGMLParser
  462. superclass or the Tag superclass, depending on the method name.
  463. """
  464. pass
  465. def __init__(self, markup='', parseOnlyThese=None, fromEncoding=None, markupMassage=True, smartQuotesTo='xml', convertEntities=None, selfClosingTags=None, isHTML=False):
  466. """
  467. The Soup object is initialized as the 'root tag', and the
  468. provided markup (which can be a string or a file-like object)
  469. is fed into the underlying parser.
  470. sgmllib will process most bad HTML, and the BeautifulSoup
  471. class has some tricks for dealing with some HTML that kills
  472. sgmllib, but Beautiful Soup can nonetheless choke or lose data
  473. if your data uses self-closing tags or declarations
  474. incorrectly.
  475. By default, Beautiful Soup uses regexes to sanitize input,
  476. avoiding the vast majority of these problems. If the problems
  477. don't apply to you, pass in False for markupMassage, and
  478. you'll get better performance.
  479. The default parser massage techniques fix the two most common
  480. instances of invalid HTML that choke sgmllib:
  481. <br/> (No space between name of closing tag and tag close)
  482. <! --Comment--> (Extraneous whitespace in declaration)
  483. You can pass in a custom list of (RE object, replace method)
  484. tuples to get Beautiful Soup to scrub your input the way you
  485. want.
  486. """
  487. pass
  488. def convert_charref(self, name):
  489. """
  490. This method fixes a bug in Python's SGMLParser.
  491. """
  492. pass
  493. def endData(self, containerClass="<class 'pymel.util.external.BeautifulSoup.NavigableString'>"):
  494. pass
  495. def handle_charref(self, ref):
  496. """
  497. Handle character references as data.
  498. """
  499. pass
  500. def handle_comment(self, text):
  501. """
  502. Handle comments as Comment objects.
  503. """
  504. pass
  505. def handle_data(self, data):
  506. pass
  507. def handle_decl(self, data):
  508. """
  509. Handle DOCTYPEs and the like as Declaration objects.
  510. """
  511. pass
  512. def handle_entityref(self, ref):
  513. """
  514. Handle entity references as data, possibly converting known
  515. HTML and/or XML entity references to the corresponding Unicode
  516. characters.
  517. """
  518. pass
  519. def handle_pi(self, text):
  520. """
  521. Handle a processing instruction as a ProcessingInstruction
  522. object, possibly one with a %SOUP-ENCODING% slot into which an
  523. encoding will be plugged later.
  524. """
  525. pass
  526. def isSelfClosingTag(self, name):
  527. """
  528. Returns true iff the given string is the name of a
  529. self-closing tag according to this parser.
  530. """
  531. pass
  532. def parse_declaration(self, i):
  533. """
  534. Treat a bogus SGML declaration as raw data. Treat a CDATA
  535. declaration as a CData object.
  536. """
  537. pass
  538. def popTag(self):
  539. pass
  540. def pushTag(self, tag):
  541. pass
  542. def reset(self):
  543. pass
  544. def unknown_endtag(self, name):
  545. pass
  546. def unknown_starttag(self, name, attrs, selfClosing=0):
  547. pass
  548. ALL_ENTITIES = None
  549. HTML_ENTITIES = None
  550. MARKUP_MASSAGE = None
  551. NESTABLE_TAGS = None
  552. PRESERVE_WHITESPACE_TAGS = None
  553. QUOTE_TAGS = None
  554. RESET_NESTING_TAGS = None
  555. ROOT_TAG_NAME = None
  556. SELF_CLOSING_TAGS = None
  557. STRIP_ASCII_SPACES = None
  558. XHTML_ENTITIES = None
  559. XML_ENTITIES = None
  560. class ProcessingInstruction(NavigableString):
  561. def __str__(self, encoding='utf-8'):
  562. pass
  563. class Declaration(NavigableString):
  564. def __str__(self, encoding='utf-8'):
  565. pass
  566. class BeautifulSoup(BeautifulStoneSoup):
  567. """
  568. This parser knows the following facts about HTML:
  569. * Some tags have no closing tag and should be interpreted as being
  570. closed as soon as they are encountered.
  571. * The text inside some tags (ie. 'script') may contain tags which
  572. are not really part of the document and which should be parsed
  573. as text, not tags. If you want to parse the text as tags, you can
  574. always fetch it and parse it explicitly.
  575. * Tag nesting rules:
  576. Most tags can't be nested at all. For instance, the occurance of
  577. a <p> tag should implicitly close the previous <p> tag.
  578. <p>Para1<p>Para2
  579. should be transformed into:
  580. <p>Para1</p><p>Para2
  581. Some tags can be nested arbitrarily. For instance, the occurance
  582. of a <blockquote> tag should _not_ implicitly close the previous
  583. <blockquote> tag.
  584. Alice said: <blockquote>Bob said: <blockquote>Blah
  585. should NOT be transformed into:
  586. Alice said: <blockquote>Bob said: </blockquote><blockquote>Blah
  587. Some tags can be nested, but the nesting is reset by the
  588. interposition of other tags. For instance, a <tr> tag should
  589. implicitly close the previous <tr> tag within the same <table>,
  590. but not close a <tr> tag in another table.
  591. <table><tr>Blah<tr>Blah
  592. should be transformed into:
  593. <table><tr>Blah</tr><tr>Blah
  594. but,
  595. <tr>Blah<table><tr>Blah
  596. should NOT be transformed into
  597. <tr>Blah<table></tr><tr>Blah
  598. Differing assumptions about tag nesting rules are a major source
  599. of problems with the BeautifulSoup class. If BeautifulSoup is not
  600. treating as nestable a tag your page author treats as nestable,
  601. try ICantBelieveItsBeautifulSoup, MinimalSoup, or
  602. BeautifulStoneSoup before writing your own subclass.
  603. """
  604. def __init__(self, *args, **kwargs):
  605. pass
  606. def start_meta(self, attrs):
  607. """
  608. Beautiful Soup can detect a charset included in a META tag,
  609. try to convert the document to that charset, and re-parse the
  610. document from the beginning.
  611. """
  612. pass
  613. CHARSET_RE = None
  614. NESTABLE_BLOCK_TAGS = None
  615. NESTABLE_INLINE_TAGS = None
  616. NESTABLE_LIST_TAGS = None
  617. NESTABLE_TABLE_TAGS = None
  618. NESTABLE_TAGS = None
  619. NON_NESTABLE_BLOCK_TAGS = None
  620. PRESERVE_WHITESPACE_TAGS = None
  621. QUOTE_TAGS = None
  622. RESET_NESTING_TAGS = None
  623. SELF_CLOSING_TAGS = None
  624. class RobustXMLParser(BeautifulStoneSoup):
  625. """
  626. #Enterprise class names! It has come to our attention that some people
  627. #think the names of the Beautiful Soup parser classes are too silly
  628. #and "unprofessional" for use in enterprise screen-scraping. We feel
  629. #your pain! For such-minded folk, the Beautiful Soup Consortium And
  630. #All-Night Kosher Bakery recommends renaming this file to
  631. #"RobustParser.py" (or, in cases of extreme enterprisiness,
  632. #"RobustParserBeanInterface.class") and using the following
  633. #enterprise-friendly class aliases:
  634. """
  635. pass
  636. class BeautifulSOAP(BeautifulStoneSoup):
  637. """
  638. This class will push a tag with only a single string child into
  639. the tag's parent as an attribute. The attribute's name is the tag
  640. name, and the value is the string child. An example should give
  641. the flavor of the change:
  642. <foo><bar>baz</bar></foo>
  643. =>
  644. <foo bar="baz"><bar>baz</bar></foo>
  645. You can then access fooTag['bar'] instead of fooTag.barTag.string.
  646. This is, of course, useful for scraping structures that tend to
  647. use subelements instead of attributes, such as SOAP messages. Note
  648. that it modifies its input, so don't print the modified version
  649. out.
  650. I'm not sure how many people really want to use this class; let me
  651. know if you do. Mainly I like the name.
  652. """
  653. def popTag(self):
  654. pass
  655. class RobustHTMLParser(BeautifulSoup):
  656. pass
  657. class ICantBelieveItsBeautifulSoup(BeautifulSoup):
  658. """
  659. The BeautifulSoup class is oriented towards skipping over
  660. common HTML errors like unclosed tags. However, sometimes it makes
  661. errors of its own. For instance, consider this fragment:
  662. <b>Foo<b>Bar</b></b>
  663. This is perfectly valid (if bizarre) HTML. However, the
  664. BeautifulSoup class will implicitly close the first b tag when it
  665. encounters the second 'b'. It will think the author wrote
  666. "<b>Foo<b>Bar", and didn't close the first 'b' tag, because
  667. there's no real-world reason to bold something that's already
  668. bold. When it encounters '</b></b>' it will close two more 'b'
  669. tags, for a grand total of three tags closed instead of two. This
  670. can throw off the rest of your document structure. The same is
  671. true of a number of other tags, listed below.
  672. It's much more common for someone to forget to close a 'b' tag
  673. than to actually use nested 'b' tags, and the BeautifulSoup class
  674. handles the common case. This class handles the not-co-common
  675. case: where you can't believe someone wrote what they did, but
  676. it's valid HTML and BeautifulSoup screwed up by assuming it
  677. wouldn't be.
  678. """
  679. I_CANT_BELIEVE_THEYRE_NESTABLE_BLOCK_TAGS = None
  680. I_CANT_BELIEVE_THEYRE_NESTABLE_INLINE_TAGS = None
  681. NESTABLE_TAGS = None
  682. class MinimalSoup(BeautifulSoup):
  683. """
  684. The MinimalSoup class is for parsing HTML that contains
  685. pathologically bad markup. It makes no assumptions about tag
  686. nesting, but it does know which tags are self-closing, that
  687. <script> tags contain Javascript and should not be parsed, that
  688. META tags may contain encoding information, and so on.
  689. This also makes it better for subclassing than BeautifulStoneSoup
  690. or BeautifulSoup.
  691. """
  692. NESTABLE_TAGS = None
  693. RESET_NESTING_TAGS = None
  694. class SimplifyingSOAPParser(BeautifulSOAP):
  695. pass
  696. class RobustInsanelyWackAssHTMLParser(MinimalSoup):
  697. pass
  698. class RobustWackAssHTMLParser(ICantBelieveItsBeautifulSoup):
  699. pass
  700. def buildTagMap(default, *args):
  701. """
  702. Turns a list of maps, lists, or scalars into a single map.
  703. Used to build the SELF_CLOSING_TAGS, NESTABLE_TAGS, and
  704. NESTING_RESET_TAGS maps out of lists and partial maps.
  705. """
  706. pass
  707. def isList(l):
  708. """
  709. Convenience method that works with all 2.x versions of Python
  710. to determine whether or not something is listlike.
  711. """
  712. pass
  713. def isString(s):
  714. """
  715. Convenience method that works with all 2.x versions of Python
  716. to determine whether or not something is stringlike.
  717. """
  718. pass
  719. DEFAULT_OUTPUT_ENCODING = None
  720. __author__ = None
  721. __copyright__ = None
  722. __license__ = None
  723. __version__ = None
  724. chardet = None
  725. generators = None
  726. name2codepoint = None