MCPcopy Create free account
hub / github.com/clips/pattern / BeautifulSoup

Class BeautifulSoup

pattern/web/soup/BeautifulSoup.py:1470–1621  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

1468 return j
1469
1470class BeautifulSoup(BeautifulStoneSoup):
1471
1472 """This parser knows the following facts about HTML:
1473
1474 * Some tags have no closing tag and should be interpreted as being
1475 closed as soon as they are encountered.
1476
1477 * The text inside some tags (ie. 'script') may contain tags which
1478 are not really part of the document and which should be parsed
1479 as text, not tags. If you want to parse the text as tags, you can
1480 always fetch it and parse it explicitly.
1481
1482 * Tag nesting rules:
1483
1484 Most tags can't be nested at all. For instance, the occurance of
1485 a <p> tag should implicitly close the previous <p> tag.
1486
1487 <p>Para1<p>Para2
1488 should be transformed into:
1489 <p>Para1</p><p>Para2
1490
1491 Some tags can be nested arbitrarily. For instance, the occurance
1492 of a <blockquote> tag should _not_ implicitly close the previous
1493 <blockquote> tag.
1494
1495 Alice said: <blockquote>Bob said: <blockquote>Blah
1496 should NOT be transformed into:
1497 Alice said: <blockquote>Bob said: </blockquote><blockquote>Blah
1498
1499 Some tags can be nested, but the nesting is reset by the
1500 interposition of other tags. For instance, a <tr> tag should
1501 implicitly close the previous <tr> tag within the same <table>,
1502 but not close a <tr> tag in another table.
1503
1504 <table><tr>Blah<tr>Blah
1505 should be transformed into:
1506 <table><tr>Blah</tr><tr>Blah
1507 but,
1508 <tr>Blah<table><tr>Blah
1509 should NOT be transformed into
1510 <tr>Blah<table></tr><tr>Blah
1511
1512 Differing assumptions about tag nesting rules are a major source
1513 of problems with the BeautifulSoup class. If BeautifulSoup is not
1514 treating as nestable a tag your page author treats as nestable,
1515 try ICantBelieveItsBeautifulSoup, MinimalSoup, or
1516 BeautifulStoneSoup before writing your own subclass."""
1517
1518 def __init__(self, *args, **kwargs):
1519 if not kwargs.has_key('smartQuotesTo'):
1520 kwargs['smartQuotesTo'] = self.HTML_ENTITIES
1521 kwargs['isHTML'] = True
1522 BeautifulStoneSoup.__init__(self, *args, **kwargs)
1523
1524 SELF_CLOSING_TAGS = buildTagMap(None,
1525 ('br' , 'hr', 'input', 'img', 'meta',
1526 'spacer', 'link', 'frame', 'base', 'col'))
1527

Callers 15

testFindallByClassMethod · 0.90
testFindAllTextMethod · 0.90
testFindByIndexMethod · 0.90
testParentsMethod · 0.90
FollowThatTagClass · 0.90
testTextNavigationMethod · 0.90
testSiblingsMethod · 0.90
testLenMethod · 0.90
testStringMethod · 0.90
testLackOfStringMethod · 0.90
testStringAssignMethod · 0.90

Calls 1

buildTagMapFunction · 0.85

Tested by 15

testFindallByClassMethod · 0.72
testFindAllTextMethod · 0.72
testFindByIndexMethod · 0.72
testParentsMethod · 0.72
testTextNavigationMethod · 0.72
testSiblingsMethod · 0.72
testLenMethod · 0.72
testStringMethod · 0.72
testLackOfStringMethod · 0.72
testStringAssignMethod · 0.72
testTextMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…