<p dir="auto">Here is the code for <code class="notranslate">get_encodings_from_content</code> in the requests.utils module:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def get_encodings_from_content(content):
    &quot;&quot;&quot;Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    &quot;&quot;&quot;

    charset_re = re.compile(r'&lt;meta.*?charset=[&quot;\']*(.+?)[&quot;\'&gt;]', flags=re.I)
    pragma_re = re.compile(r'&lt;meta.*?content=[&quot;\']*;?charset=(.+?)[&quot;\'&gt;]', flags=re.I)
    xml_re = re.compile(r'^&lt;\?xml.*?encoding=[&quot;\']*(.+?)[&quot;\'&gt;]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content))"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">get_encodings_from_content</span>(<span class="pl-s1">content</span>):
    <span class="pl-s">"""Returns encodings from given content string.</span>
<span class="pl-s"></span>
<span class="pl-s">    :param content: bytestring to extract encodings from.</span>
<span class="pl-s">    """</span>

    <span class="pl-s1">charset_re</span> <span class="pl-c1">=</span> <span class="pl-s1">re</span>.<span class="pl-en">compile</span>(<span class="pl-s">r'&lt;meta.*?charset=["\']*(.+?)["\'&gt;]'</span>, <span class="pl-s1">flags</span><span class="pl-c1">=</span><span class="pl-s1">re</span>.<span class="pl-v">I</span>)
    <span class="pl-s1">pragma_re</span> <span class="pl-c1">=</span> <span class="pl-s1">re</span>.<span class="pl-en">compile</span>(<span class="pl-s">r'&lt;meta.*?content=["\']*;?charset=(.+?)["\'&gt;]'</span>, <span class="pl-s1">flags</span><span class="pl-c1">=</span><span class="pl-s1">re</span>.<span class="pl-v">I</span>)
    <span class="pl-s1">xml_re</span> <span class="pl-c1">=</span> <span class="pl-s1">re</span>.<span class="pl-en">compile</span>(<span class="pl-s">r'^&lt;\?xml.*?encoding=["\']*(.+?)["\'&gt;]'</span>)

    <span class="pl-k">return</span> (<span class="pl-s1">charset_re</span>.<span class="pl-en">findall</span>(<span class="pl-s1">content</span>) <span class="pl-c1">+</span>
            <span class="pl-s1">pragma_re</span>.<span class="pl-en">findall</span>(<span class="pl-s1">content</span>) <span class="pl-c1">+</span>
            <span class="pl-s1">xml_re</span>.<span class="pl-en">findall</span>(<span class="pl-s1">content</span>))</pre></div>
<p dir="auto">Is there any reason to recompile the regexes on every call rather than making them module-level constants?</p>
<p dir="auto">More fundamentally, wouldn't it be more useful to have a function that just returned the first match, rather than a tuple of all matches, and therefore could often stop processing the input after a few lines, rather than always having to do three passes through the entire thing? That way, if you're fetching a large html document using <code class="notranslate">stream=True</code> from a server that may not have sent the charset in a Content-Type header, we can write:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="encoding = get_encoding_from_headers(res.headers) or \
    get_encoding_from_content(raw)  # just get the first encoding declared in content, if any"><pre class="notranslate"><span class="pl-s1">encoding</span> <span class="pl-c1">=</span> <span class="pl-en">get_encoding_from_headers</span>(<span class="pl-s1">res</span>.<span class="pl-s1">headers</span>) <span class="pl-c1">or</span> \
    <span class="pl-en">get_encoding_from_content</span>(<span class="pl-s1">raw</span>)  <span class="pl-c"># just get the first encoding declared in content, if any</span></pre></div>
<p dir="auto">which is all we care about.</p>
<p dir="auto">(Assume <code class="notranslate">raw</code> has been set already with something like:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="bio = BytesIO()
bytesread = 0
for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
    bio.write(chunk)
    bytesread += len(chunk)
    if bytesread &gt; RES_MAX_BYTES:
        return _too_big()
raw = bio.getvalue()"><pre class="notranslate"><span class="pl-s1">bio</span> <span class="pl-c1">=</span> <span class="pl-v">BytesIO</span>()
<span class="pl-s1">bytesread</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
<span class="pl-k">for</span> <span class="pl-s1">chunk</span> <span class="pl-c1">in</span> <span class="pl-s1">res</span>.<span class="pl-en">iter_content</span>(<span class="pl-s1">chunk_size</span><span class="pl-c1">=</span><span class="pl-v">CHUNK_SIZE</span>):
    <span class="pl-s1">bio</span>.<span class="pl-en">write</span>(<span class="pl-s1">chunk</span>)
    <span class="pl-s1">bytesread</span> <span class="pl-c1">+=</span> <span class="pl-en">len</span>(<span class="pl-s1">chunk</span>)
    <span class="pl-k">if</span> <span class="pl-s1">bytesread</span> <span class="pl-c1">&gt;</span> <span class="pl-v">RES_MAX_BYTES</span>:
        <span class="pl-k">return</span> <span class="pl-en">_too_big</span>()
<span class="pl-s1">raw</span> <span class="pl-c1">=</span> <span class="pl-s1">bio</span>.<span class="pl-en">getvalue</span>()</pre></div>
<p dir="auto">)</p>
<p dir="auto">The occult logo evokes strong emotion in viewers of certain faiths. An uninitiated looking for <a href="https://gnosticteachings.org/books-by-samael-aun-weor/the-doomed-aryan-race/1305-the-caduceus-of-mercury.html" rel="nofollow">more information</a> on the logo would <a href="http://www.occultopedia.com/c/caduceus.htm" rel="nofollow">easily</a> come to this <a href="https://www.samaelaunweor.org/en/the-solar-bodies/the-caduceus-of-mercury" rel="nofollow">information</a> with a google search...</p>
<blockquote>
<p dir="auto">The Gnostic Movement teaches its disciples that from their seminal system (during the esoteric practices of sexual transmutation) the lunar atoms ascend through the nervous canal of Ida and the solar atoms victoriously ascend through Pingala.</p>
</blockquote>
<p dir="auto"><a href="https://www.rollitup.org/t/the-rod-of-asclepius-vs-the-caduceus-of-mercury.374706/" rel="nofollow">RIU</a>:</p>
<blockquote>
<p dir="auto">Mercury was the god of merchants and merchandise. Also the conductor of the dead to the afterlife, and protector of thieves and deceit. The Caduceus is a herald's staff representing Mercury (Hermes) and his undertakings, that was a gift from Apollo. It did not and does not represent medicine. An officer in the U.S. Army Medical Corps mistook the Caduceus for the Staff of Asclepius and introduced the new Medical Corps symbol at the beginning of the twentieth century. Not too long after that, everyone was adopting the same mistake. Including the American Medical Association (AMA), the largest association of physicians and medical students in the United States.</p>
</blockquote>
<blockquote>
<p dir="auto">Asclepius was the son of Apollo, and the god of medicine and healing. Asclepius's daughter is Hygieia, whose bowl is the well known international symbol of pharmacy and pharmacist. Asclepius's staff has many interpretations, but basically means, the snake is thought to signify a new beginning from old problems, as in shedding of the skin. The staff or rod, is thought to mean support and structure to help deal with the turmoil of life and death and being associated with them. Asclepius was so good as a healer that he was reputed to be able to bring people back from the dead. For this he angered Hades and Zeus because he raised Hippolytus from the dead and accepted gold for it, and was killed by Zeus with a thunder bolt. Zeus then placed him amongst the stars as the constellation Ophiuchus ("The Serpent Holder").</p>
</blockquote>