zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
U
¡ý°d8:ã
@sòdZddgZzddlmZWn.ek
rJZzddlmZW5dZ[XYnXddlmZddlm    Z    ddl
m Z dd    l m Z mZmZmZmZdd
lmZmZmZmZmZmZmZmZdd lmZd Zd d„ZGdd„deƒZGdd„deeƒZ dS)ÚMITÚLXMLTreeBuilderForXMLÚLXMLTreeBuilderé)ÚCallableN)ÚBytesIO)ÚStringIO)Úetree)ÚCommentÚDoctypeÚNamespacedAttributeÚProcessingInstructionÚXMLProcessingInstruction)ÚDetectsXMLParsedAsHTMLÚFASTÚHTMLÚHTMLTreeBuilderÚ
PERMISSIVEÚParserRejectedMarkupÚ TreeBuilderÚXML)ÚEncodingDetectorÚlxmlcCstdd„t| ¡ƒDƒƒS)zInvert a dictionary.css|]\}}||fVqdS©N©)Ú.0ÚkÚvrrúHd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\bs4/builder/_lxml.pyÚ    <genexpr>'sz_invert.<locals>.<genexpr>)ÚdictÚlistÚitems)ÚdrrrÚ_invert%sr#csäeZdZejZdZeZdZ    dgZ
e    e e e egZdZeddZeeƒZ‡fdd„Zd    d
„Zd d „Zd d„Zd*‡fdd„    Zdd„Zd+dd„Zdd„Zdd„Zifdd„Zdd„Zdd„Z d d!„Z!d"d#„Z"d$d%„Z#d&d'„Z$d(d)„Z%‡Z&S),rTzlxml-xmlÚxmliz$http://www.w3.org/XML/1998/namespace)r$cs tt|ƒ |¡| |j¡dS)zLet the BeautifulSoup object know about the standard namespace
        mapping.
 
        :param soup: A `BeautifulSoup`.
        N)ÚsuperrÚinitialize_soupÚ_register_namespacesÚDEFAULT_NSMAPS)ÚselfÚsoup©Ú    __class__rrr&Dsz%LXMLTreeBuilderForXML.initialize_soupcCs6t| ¡ƒD]$\}}|r ||jjkr ||jj|<q dS)aÿLet the BeautifulSoup object know about namespaces encountered
        while parsing the document.
 
        This might be useful later on when creating CSS selectors.
 
        This will track (almost) all namespaces, even ones that were
        only in scope for part of the document. If two namespaces have
        the same prefix, only the first one encountered will be
        tracked. Un-prefixed namespaces are not tracked.
 
        :param mapping: A dictionary mapping namespace prefixes to URIs.
        N)r r!r*Ú _namespaces)r)ÚmappingÚkeyÚvaluerrrr'Ms z*LXMLTreeBuilderForXML._register_namespacescCs"|jdk    r|jStj|dd|dS)zÍFind the default parser for the given encoding.
 
        :param encoding: A string.
        :return: Either a parser object or a class, which
          will be instantiated with default arguments.
        NFT©ÚtargetZ strip_cdataZrecoverÚencoding)Ú_default_parserrÚ    XMLParser©r)r3rrrÚdefault_parseres
ÿz$LXMLTreeBuilderForXML.default_parsercCs(| |¡}t|tƒr$||dd|d}|S)z¥Instantiate an appropriate parser for the given encoding.
 
        :param encoding: A string.
        :return: A parser object such as an `etree.XMLParser`.
        FTr1)r7Ú
isinstancer)r)r3ÚparserrrrÚ
parser_forqs
 
ÿz LXMLTreeBuilderForXML.parser_forNc sL||_|dk    rt|ƒ|_d|_|jg|_t|jƒg|_t    t
|ƒj f|ŽdSr) r4ÚsetÚempty_element_tagsr*ÚDEFAULT_NSMAPS_INVERTEDÚnsmapsrr(Úactive_namespace_prefixesr%rÚ__init__)r)r9r<Úkwargsr+rrr@s
 
zLXMLTreeBuilderForXML.__init__cCs0|ddkr$t|dd… dd¡ƒSd|fSdS)NrÚ{éÚ})ÚtupleÚsplit)r)ÚtagrrrÚ    _getNsTags zLXMLTreeBuilderForXML._getNsTagc
cs¼|j }|rt|_t |¡nt|_t|tƒr`t|ƒdkrR|ddkrR|dd…}|d|dfVt|tƒr~|     d¡d|dfV|g}|g}t
|||||d}|j D]}    |j |    |dfVq¢dS)aARun any preliminary steps necessary to make incoming markup
        acceptable to the parser.
 
        lxml really wants to get a bytestring and convert it to
        Unicode itself. So instead of using UnicodeDammit to convert
        the bytestring to Unicode using different encodings, this
        implementation uses EncodingDetector to iterate over the
        encodings, and tell lxml to try to parse the document as each
        one in turn.
 
        :param markup: Some markup -- hopefully a bytestring.
        :param user_specified_encoding: The user asked to try this encoding.
        :param document_declared_encoding: The markup itself claims to be
            in this encoding.
        :param exclude_encodings: The user asked _not_ to try any of
            these encodings.
 
        :yield: A series of 4-tuples:
         (markup, encoding, declared encoding,
          has undergone character replacement)
 
         Each 4-tuple represents a strategy for converting the
         document to Unicode and parsing it. Each strategy will be tried 
         in turn.
        rurCNFÚutf8)Úknown_definite_encodingsÚuser_encodingsÚis_htmlÚexclude_encodings) Úis_xmlr Úprocessing_instruction_classrZwarn_if_markup_looks_like_xmlr r8ÚstrÚlenÚencoderÚ    encodingsÚmarkup)
r)rTZuser_specified_encodingrMZdocument_declared_encodingrLrJrKÚdetectorr3rrrÚprepare_markup•s2 
 
 
ÿý
z$LXMLTreeBuilderForXML.prepare_markupc
CsÆt|tƒrt|ƒ}nt|tƒr&t|ƒ}| |j¡}z\| |jj    ¡|_
|j
  |¡t |ƒdkr‚| |j¡}t |ƒdkrP|j
  |¡qP|j
  ¡Wn2tttjfk
rÀ}z t|ƒ‚W5d}~XYnXdS)Nr)r8ÚbytesrrPrÚreadÚ
CHUNK_SIZEr:r*Úoriginal_encodingr9ÚfeedrQÚcloseÚUnicodeDecodeErrorÚ LookupErrorrÚ ParserErrorr)r)rTÚdataÚerrrr[Ûs
 
 
     zLXMLTreeBuilderForXML.feedcCs|jg|_dSr)r=r>)r)rrrr\ðszLXMLTreeBuilderForXML.closec CsRt|ƒ}d}t|ƒdkr4t|jƒdkr4|j d¡nŠt|ƒdkr¾| |¡|j t|ƒ¡t|jdƒ}| |¡d|kr€|d=|j |¡| ¡}t    | 
¡ƒD]\}}t d|dƒ}|||<q i}    t    | 
¡ƒD]F\}
} |  |
¡\}}
|dkrö| |    |
<qÎ|  |¡}t ||
|ƒ}
| |    |
<qÎ|    }|  |¡\}}|  |¡}|jj|||||jdddS)NrrCéÿÿÿÿÚÚxmlnszhttp://www.w3.org/2000/xmlns/)Ú
namespaces)rrQr>Úappendr'r#r?ÚupdateÚcopyr r!r rHÚ_prefix_for_namespacer*Úhandle_starttag) r)ÚnameÚattrsZnsmapÚnsprefixZcurrent_mappingÚprefixÚ    namespaceÚ    attributeZ    new_attrsÚattrr0rrrÚstartósL 
 
 ÿ
 
 
 
 
þzLXMLTreeBuilderForXML.startcCs<|dkr dSt|jƒD] }|dk    r||kr||SqdS)z9Find the currently active prefix for the given namespace.N)Úreversedr>)r)roÚinverted_nsmaprrrri2s z+LXMLTreeBuilderForXML._prefix_for_namespacecCs˜|j ¡|jjd}| |¡\}}d}|dk    r\t|jƒD] }|dk    r:||kr:||}q\q:|j ||¡t|jƒdkr”|j ¡}|dk    r”|j     ¡dS)NrbrC)
r*ÚendDataZtagStackrHrsr>Ú handle_endtagrQÚpopr?)r)rkZ completed_tagrormrtZout_of_scope_nsmaprrrÚend;s
 
zLXMLTreeBuilderForXML.endcCs4|j ¡|d|}|j |¡|j |j¡dS)Nú )r*ruÚ handle_datarO)r)r2r`rrrÚpiQs
  zLXMLTreeBuilderForXML.picCs|j |¡dSr)r*rz©r)Úcontentrrrr`WszLXMLTreeBuilderForXML.datacCs(|j ¡t |||¡}|j |¡dSr)r*rur
Zfor_name_and_idsZobject_was_parsed)r)rkZpubidÚsystemÚdoctyperrrrZs
zLXMLTreeBuilderForXML.doctypecCs&|j ¡|j |¡|j t¡dS)z#Handle comments as Comment objects.N)r*rurzr    r|rrrÚcomment_s
 zLXMLTreeBuilderForXML.commentcCsd|S)úSee `TreeBuilder`.z)<?xml version="1.0" encoding="utf-8"?>
%sr©r)ÚfragmentrrrÚtest_fragment_to_documentesz/LXMLTreeBuilderForXML.test_fragment_to_document)NN)NNN)'Ú__name__Ú
__module__Ú __qualname__rr5ZDEFAULT_PARSER_CLASSrNr rOÚNAMEÚALTERNATE_NAMESÚLXMLrrrÚfeaturesrYrr(r#r=r&r'r7r:r@rHrVr[r\rrrirxr{r`rr€r„Ú __classcell__rrr+rr)s:
           þ
F ?    c@sFeZdZeZdgZeeeeegZ    dZ
e Z dd„Z dd„Zdd„Zd    S)
rz    lxml-htmlFcCstjSr)rÚ
HTMLParserr6rrrr7sszLXMLTreeBuilder.default_parserc
Csf|jj}z&| |¡|_|j |¡|j ¡Wn2tttj    fk
r`}z t
|ƒ‚W5d}~XYnXdSr) r*rZr:r9r[r\r]r^rr_r)r)rTr3rarrrr[vs  zLXMLTreeBuilder.feedcCsd|S)rz<html><body>%s</body></html>rr‚rrrr„€sz)LXMLTreeBuilder.test_fragment_to_documentN)r…r†r‡rŠrˆr‰rrrr‹rNr rOr7r[r„rrrrrjs
)!Ú __license__Ú__all__Úcollections.abcrÚ ImportErrorraÚ collectionsÚiorrrrZ bs4.elementr    r
r r r Z bs4.builderrrrrrrrrZ
bs4.dammitrrŠr#rrrrrrÚ<module>s&þ   (     C