a
    %ñ»j)  ã                   @   sL  d Z g d¢ZddlZddlmZmZ z0ddlmZmZm	Z	m
Z
mZmZmZ eefZW n6 eyŠ   ddlmZmZm	Z	m
Z
mZmZ eZY n0 ddd„Zdd	d
„Zddd„Zdd„ Ze dej¡jZG dd„ dƒZdd„ Zdd„ ZzddlmZ W n  e�y   ddlmZ Y n0 e d¡jZze  W n e!�y>   e"Z Y n0 dd„ Z#dS )z5External interface to the BeautifulSoup HTML parser.
)Ú
fromstringÚparseÚconvert_treeé    N)ÚetreeÚhtml)ÚBeautifulSoupÚTagÚCommentÚProcessingInstructionÚNavigableStringÚDeclarationÚDoctype)r   r   r	   r
   r   r   c                 K   s   t | ||fi |¤ŽS )a¤  Parse a string of HTML data into an Element tree using the
    BeautifulSoup parser.

    Returns the root ``<html>`` Element of the tree.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    )Ú_parse)ÚdataÚbeautifulsoupÚmakeelementÚbsargs© r   ú:E:\soccer\ml_env\Lib\site-packages\lxml/html/soupparser.pyr      s    r   c                 K   s0   t | dƒst| ƒ} t| ||fi |¤Ž}t |¡S )aY  Parse a file into an ElemenTree using the BeautifulSoup parser.

    You can pass a different BeautifulSoup parser through the
    `beautifulsoup` keyword, and a diffent Element factory function
    through the `makeelement` keyword.  By default, the standard
    ``BeautifulSoup`` class and the default factory of `lxml.html` are
    used.
    Úread)ÚhasattrÚopenr   r   ZElementTree)Úfiler   r   r   Úrootr   r   r   r   $   s    	
r   c                 C   s*   t | |ƒ}| ¡ }|D ]}| |¡ q|S )a  Convert a BeautifulSoup tree to a list of Element trees.

    Returns a list instead of a single root Element to support
    HTML-like soup with more than one root element.

    You can pass a different Element factory through the `makeelement`
    keyword.
    )Ú_convert_treeZgetchildrenÚremove)Úbeautiful_soup_treer   r   ÚchildrenÚchildr   r   r   r   3   s
    	
r   c                 K   s†   |d u rt }t|dƒr&d|vr&d|d< t|dƒr@d|vr@d|d< || fi |¤Ž}t||ƒ}t|ƒdkr||d jdkr||d S d|_|S )	NZHTML_ENTITIESZconvertEntitiesr   ZDEFAULT_BUILDER_FEATURESÚfeatureszhtml.parseré   r   )r   r   r   ÚlenÚtag)Úsourcer   r   r   Útreer   r   r   r   r   E   s    


r   z`(?:\s|[<!])*DOCTYPE\s*HTML(?:\s+PUBLIC)?(?:\s+(\'[^\']*\'|"[^"]*"))?(?:\s+(\'[^\']*\'|"[^"]*"))?c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
_PseudoTagc                 C   s   d| _ g | _|| _d S )Nr   )ÚnameÚattrsÚcontents)Úselfr(   r   r   r   Ú__init__`   s    z_PseudoTag.__init__c                 C   s
   | j  ¡ S ©N)r(   Ú__iter__)r)   r   r   r   r,   e   s    z_PseudoTag.__iter__N)Ú__name__Ú
__module__Ú__qualname__r*   r,   r   r   r   r   r%   ^   s   r%   c                 C   s  |d u rt jj}d  }}d  }}t| ƒD ]Z\}}t|tƒrl|d u rF|}|}|d u r‚|jr‚|j ¡ dkr‚|}q(|d u r(t|tƒr(|}q(|d u rœg  }}	| j	}
n2| j	d |… }| j	||d … }
| j	|d d … }	|d u�r|
 
|¡}|
d |… |j	 |
|d d …  |_	nt|
ƒ}t|ƒ}||ƒ}|}t|ƒD ]&}||ƒ}|d u�r,| |¡ |}�q,|}|	D ]&}||ƒ}|d u�r\| |¡ |}�q\|d u�rz| ¡ }W n t�y´   |j}Y n0 t|ƒ}|�sÆn>| ¡ \}}| ¡ j}|�oì|dd… |_|�o |dd… |_|S )Nr   r    éÿÿÿÿ)r   Úhtml_parserr   Ú	enumerateÚ
isinstancer   r&   ÚlowerÚ_DECLARATION_OR_DOCTYPEr(   Úindexr%   Ú_init_node_convertersÚreversedZaddpreviousZaddnextZoutput_readyÚAttributeErrorÚstringÚ_parse_doctype_declarationÚgroupsZgetroottreeÚdocinfoÚ	public_idZ
system_url)r   r   Zfirst_element_idxZlast_element_idxZ	html_rootZdeclarationÚiÚeZpre_rootZ	post_rootÚrootsÚconvert_nodeZres_rootÚprevZ	convertedZdoctype_stringÚmatchZexternal_idZsys_urir=   r   r   r   r   i   sd    


&





r   c                    s˜   i ‰g ‰‡‡fdd„}‡‡fdd„‰d‡‡fdd„	‰dd„ ‰d	d
„ ‰ |t tƒ‡‡‡‡fdd„ƒ}|tƒdd„ ƒ}|tƒdd„ ƒ}|tƒ‡ fdd„ƒ}ˆS )Nc                     s   ‡‡‡ fdd„}|S )Nc                    s    ˆD ]}| ˆ |< ˆ  |¡ q| S r+   )Úappend)ÚhandlerÚt)Ú
convertersÚordered_node_typesÚtypesr   r   ÚaddÃ   s    z5_init_node_converters.<locals>.converter.<locals>.addr   )rJ   rK   ©rH   rI   )rJ   r   Ú	converterÂ   s    z(_init_node_converters.<locals>.converterc                    s$   ˆD ]}t | |ƒrˆ |   S qd S r+   )r3   )ÚnoderG   rL   r   r   Úfind_best_converterÊ   s    
z2_init_node_converters.<locals>.find_best_converterc                    sN   zˆ t | ƒ }W n& ty6   ˆ| ƒ }ˆ t | ƒ< Y n0 |d u rDd S || |ƒS r+   )ÚtypeÚKeyError)Úbs_nodeÚparentrF   )rH   rO   r   r   rB   Ð   s    z+_init_node_converters.<locals>.convert_nodec                 S   sT   t | tƒrBi }|  ¡ D ](\}}t |tƒr2d |¡}t|ƒ||< qndd„ | D ƒ}|S )Nú c                 S   s   i | ]\}}|t |ƒ“qS r   ©Úunescape)Ú.0ÚkÚvr   r   r   Ú
<dictcomp>â   ó    z<_init_node_converters.<locals>.map_attrs.<locals>.<dictcomp>)r3   ÚdictÚitemsÚlistÚjoinrV   )Zbs_attrsÚattribsrX   rY   r   r   r   Ú	map_attrsÚ   s    


z(_init_node_converters.<locals>.map_attrsc                 S   s:   t | ƒdkr| jpd| | _n| d jp*d| | d _d S )Nr   Ú r0   )r!   ÚtextÚtail)rS   rc   r   r   r   Úappend_textå   s    z*_init_node_converters.<locals>.append_textc              	      s    | j }|d ur2|rˆ|ƒnd }tj|| j|d�}n|r>ˆ|ƒni }ˆ| j|d�}| D ]F}zˆt|ƒ }W n tyz   Y n0 |d urT|||ƒ qTˆ ||ƒ qT|S )N)Zattrib)r'   r   Z
SubElementr&   rP   rQ   )rR   rS   r'   r`   Úresr   rF   )rB   rH   r   ra   r   r   Úconvert_tagí   s     
z*_init_node_converters.<locals>.convert_tagc                 S   s    t  | ¡}|d ur| |¡ |S r+   )r   ZHtmlCommentrE   ©rR   rS   rf   r   r   r   Úconvert_comment  s    

z._init_node_converters.<locals>.convert_commentc                 S   s>   |   d¡r| d d… } tj|  dd¡Ž }|d ur:| |¡ |S )Nú?r0   rT   r    )Úendswithr   r
   ÚsplitrE   rh   r   r   r   Ú
convert_pi  s    

z)_init_node_converters.<locals>.convert_pic                    s   |d urˆ |t | ƒƒ d S r+   rU   )rR   rS   )re   r   r   Úconvert_text  s    z+_init_node_converters.<locals>.convert_text)N)r   r%   r	   r
   r   )r   rM   rg   ri   rm   rn   r   )re   rB   rH   rO   r   ra   rI   r   r7   ¾   s     



r7   )Úname2codepointz&(\w+);c                 C   s   | sdS dd„ }t || ƒS )Nrb   c                 S   s6   zt t|  d¡ ƒW S  ty0   |  d¡ Y S 0 d S )Nr    r   )Úunichrro   ÚgrouprQ   )Úmr   r   r   Úunescape_entity5  s    z!unescape.<locals>.unescape_entity)Úhandle_entities)r:   rs   r   r   r   rV   1  s    rV   )NN)NN)N)$Ú__doc__Ú__all__ÚreZlxmlr   r   Zbs4r   r   r	   r
   r   r   r   r5   ÚImportErrorr   r   r   r   ÚcompileÚ
IGNORECASErD   r;   r%   r   r7   Úhtml.entitiesro   ÚhtmlentitydefsÚsubrt   rp   Ú	NameErrorÚchrrV   r   r   r   r   Ú<module>   s<   $ 



üUc
