a
    %ñ»j¾"  ã                   @   sh  d Z ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZ zeZW n eyn   eefZY n0 zddlmZ W n eyž   ddlmZ Y n0 zddlmZ W n eyÎ   ddlmZ Y n0 G d	d
„ d
eƒZzddlmZ W n e�y   Y n0 G dd„ deƒZeƒ Zdd„ Zddd„Zddd„Zddd„Zd dd„Z d!dd„Z!dd„ Z"eƒ Z#dS )"z?
An interface to html5lib that mimics the lxml.html interface.
é    N)Ú
HTMLParser)ÚTreeBuilder)Úetree)ÚElementÚXHTML_NAMESPACEÚ_contains_block_level_tag)Úurlopen)Úurlparsec                   @   s   e Zd ZdZddd„ZdS )r   z*An html5lib HTML parser with lxml as tree.Fc                 K   s   t j| f|tdœ|¤Ž d S ©N)ÚstrictÚtree)Ú_HTMLParserÚ__init__r   ©Úselfr   Úkwargs© r   ú;E:\soccer\ml_env\Lib\site-packages\lxml/html/html5parser.pyr      s    zHTMLParser.__init__N)F©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   )ÚXHTMLParserc                   @   s   e Zd ZdZddd„ZdS )r   z+An html5lib XHTML Parser with lxml as tree.Fc                 K   s   t j| f|tdœ|¤Ž d S r
   )Ú_XHTMLParserr   r   r   r   r   r   r   *   s    zXHTMLParser.__init__N)Fr   r   r   r   r   r   '   s   r   c                 C   s(   |   |¡}|d ur|S |   dt|f ¡S )Nz{%s}%s)Úfindr   )r   ÚtagÚelemr   r   r   Ú	_find_tag0   s    
r   c                 C   s^   t | tƒstdƒ‚|du rt}i }|du r8t | tƒr8d}|durH||d< |j| fi |¤Ž ¡ S )zÍ
    Parse a whole document into a string.

    If `guess_charset` is true, or if the input is not Unicode but a
    byte string, the `chardet` library will perform charset guessing
    on the string.
    ústring requiredNTÚ
useChardet)Ú
isinstanceÚ_stringsÚ	TypeErrorÚhtml_parserÚbytesÚparseZgetroot)ÚhtmlÚguess_charsetÚparserÚoptionsr   r   r   Údocument_fromstring7   s    
r+   Fc                 C   sš   t | tƒstdƒ‚|du rt}i }|du r8t | tƒr8d}|durH||d< |j| dfi |¤Ž}|r–t |d tƒr–|r–|d  ¡ r�t d|d  ¡‚|d= |S )a`  Parses several HTML elements, returning a list of elements.

    The first item in the list may be a string.  If no_leading_text is true,
    then it will be an error if there is leading text, and it will always be
    a list of only elements.

    If `guess_charset` is true, the `chardet` library will perform charset
    guessing on the string.
    r   NFr    Údivr   zThere is leading text: %r)	r!   r"   r#   r$   r%   ZparseFragmentÚstripr   ÚParserError)r'   Úno_leading_textr(   r)   r*   Úchildrenr   r   r   Úfragments_fromstringO   s$    
ÿr1   c                 C   sÌ   t | tƒstdƒ‚t|ƒ}t| ||| d�}|rvt |tƒs>d}t|ƒ}|rrt |d tƒrh|d |_|d= | |¡ |S |s„t 	d¡‚t
|ƒdkršt 	d¡‚|d }|jrÂ|j ¡ rÂt 	d|j ¡‚d	|_|S )
aÂ  Parses a single HTML element; it is an error if there is more than
    one element, or if anything but whitespace precedes or follows the
    element.

    If 'create_parent' is true (or is a tag name) then a parent node
    will be created to encapsulate the HTML in a single element.  In
    this case, leading or trailing text is allowed.

    If `guess_charset` is true, the `chardet` library will perform charset
    guessing on the string.
    r   )r(   r)   r/   r,   r   zNo elements foundé   zMultiple elements foundzElement followed by text: %rN)r!   r"   r#   Úboolr1   r   ÚtextÚextendr   r.   ÚlenÚtailr-   )r'   Zcreate_parentr(   r)   Zaccept_leading_textÚelementsÚnew_rootÚresultr   r   r   Úfragment_fromstringq   s4    
þ




r;   c                 C   sÜ   t | tƒstdƒ‚t| ||d�}| dd… }t |tƒrB| dd¡}| ¡  ¡ }| d¡sb| d¡rf|S t	|d	ƒ}t
|ƒr||S t	|d
ƒ}t
|ƒdkrÂ|jr¢|j ¡ sÂ|d jrº|d j ¡ sÂ|d S t|ƒrÒd|_nd|_|S )a   Parse the html, returning a single element/document.

    This tries to minimally parse the chunk of text, without knowing if it
    is a fragment or a document.

    'base_url' will set the document's base_url attribute (and the tree's
    docinfo.URL)

    If `guess_charset` is true, or if the input is not Unicode but a
    byte string, the `chardet` library will perform charset guessing
    on the string.
    r   )r)   r(   Né2   ÚasciiÚreplacez<htmlz	<!doctypeÚheadÚbodyr2   éÿÿÿÿr   r,   Úspan)r!   r"   r#   r+   r%   ÚdecodeÚlstripÚlowerÚ
startswithr   r6   r4   r-   r7   r   r   )r'   r(   r)   ÚdocÚstartr?   r@   r   r   r   Ú
fromstring�   s2    
ÿ


ÿÿrI   c                 C   s~   |du rt }t| tƒs(| }|du r\d}n4t| ƒrFt| ƒ}|du r\d}nt| dƒ}|du r\d}i }|rl||d< |j|fi |¤ŽS )a*  Parse a filename, URL, or file-like object into an HTML document
    tree.  Note: this returns a tree, not an element.  Use
    ``parse(...).getroot()`` to get the document root.

    If ``guess_charset`` is true, the ``useChardet`` option is passed into
    html5lib to enable character detection.  This option is on by default
    when parsing from URLs, off by default when parsing from file(-like)
    objects (which tend to return Unicode more often than not), and on by
    default when parsing from a file path (which is read in binary mode).
    NFTÚrbr    )r$   r!   r"   Ú_looks_like_urlr   Úopenr&   )Zfilename_url_or_filer(   r)   Úfpr*   r   r   r   r&   Ó   s"    

r&   c                 C   s@   t | ƒd }|sdS tjdkr8|tjv r8t|ƒdkr8dS dS d S )Nr   FÚwin32r2   T)r	   ÚsysÚplatformÚstringÚascii_lettersr6   )ÚstrÚschemer   r   r   rK   ÷   s    
ÿ
þrK   )NN)FNN)FNN)NN)NN)$r   rO   rQ   Zhtml5libr   r   Z html5lib.treebuilders.etree_lxmlr   Zlxmlr   Z	lxml.htmlr   r   r   Ú
basestringr"   Ú	NameErrorr%   rS   Úurllib2r   ÚImportErrorÚurllib.requestr	   Úurllib.parser   r   Zxhtml_parserr   r+   r1   r;   rI   r&   rK   r$   r   r   r   r   Ú<module>   sJ   
  ÿ
"  ÿ
,
6
$