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

Method start_meta

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

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.

(self, attrs)

Source from the content-addressed store, hash-verified

1574 CHARSET_RE = re.compile("((^|;)\s*charset=)([^;]*)", re.M)
1575
1576 def start_meta(self, attrs):
1577 """Beautiful Soup can detect a charset included in a META tag,
1578 try to convert the document to that charset, and re-parse the
1579 document from the beginning."""
1580 httpEquiv = None
1581 contentType = None
1582 contentTypeIndex = None
1583 tagNeedsEncodingSubstitution = False
1584
1585 for i in range(0, len(attrs)):
1586 key, value = attrs[i]
1587 key = key.lower()
1588 if key == 'http-equiv':
1589 httpEquiv = value
1590 elif key == 'content':
1591 contentType = value
1592 contentTypeIndex = i
1593
1594 if httpEquiv and contentType: # It's an interesting meta tag.
1595 match = self.CHARSET_RE.search(contentType)
1596 if match:
1597 if (self.declaredHTMLEncoding is not None or
1598 self.originalEncoding == self.fromEncoding):
1599 # An HTML encoding was sniffed while converting
1600 # the document to Unicode, or an HTML encoding was
1601 # sniffed during a previous pass through the
1602 # document, or an encoding was specified
1603 # explicitly and it worked. Rewrite the meta tag.
1604 def rewrite(match):
1605 return match.group(1) + "%SOUP-ENCODING%"
1606 newAttr = self.CHARSET_RE.sub(rewrite, contentType)
1607 attrs[contentTypeIndex] = (attrs[contentTypeIndex][0],
1608 newAttr)
1609 tagNeedsEncodingSubstitution = True
1610 else:
1611 # This is our first pass through the document.
1612 # Go through it again with the encoding information.
1613 newCharset = match.group(3)
1614 if newCharset and newCharset != self.originalEncoding:
1615 self.declaredHTMLEncoding = newCharset
1616 self._feed(self.declaredHTMLEncoding)
1617 raise StopParsing
1618 pass
1619 tag = self.unknown_starttag("meta", attrs)
1620 if tag and tagNeedsEncodingSubstitution:
1621 tag.containsSubstitutions = True
1622
1623class StopParsing(Exception):
1624 pass

Callers

nothing calls this directly

Calls 5

lenFunction · 0.85
_feedMethod · 0.80
searchMethod · 0.45
groupMethod · 0.45
unknown_starttagMethod · 0.45

Tested by

no test coverage detected