B
    j9C\ÁZ  ã               @   s  d Z dZdZdZdZdgZddlZddlZddlZddl	Z	ddl
Z
dd	lmZmZ dd
lmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddk G dd„ deƒZeZeZG dd„ deƒZG dd„ de ƒZ!G dd„ de"ƒZ#e$dkr
ddlZeej%ƒZ&e'e& (¡ ƒ dS )aH  Beautiful Soup
Elixir and Tonic
"The Screen-Scraper's Friend"
http://www.crummy.com/software/BeautifulSoup/

Beautiful Soup uses a pluggable XML or HTML parser to parse a
(possibly invalid) document into a tree representation. Beautiful Soup
provides methods and Pythonic idioms that make it easy to navigate,
search, and modify the parse tree.

Beautiful Soup works with Python 2.7 and up. It works better if lxml
and/or html5lib is installed.

For more than you ever wanted to know about Beautiful Soup, see the
documentation:
http://www.crummy.com/software/BeautifulSoup/bs4/doc/

z*Leonard Richardson (leonardr@segfault.org)z4.7.1z*Copyright (c) 2004-2019 Leonard RichardsonZMITÚBeautifulSoupé    Né   )Úbuilder_registryÚParserRejectedMarkup)ÚUnicodeDammit)ÚCDataÚCommentÚDEFAULT_OUTPUT_ENCODINGÚDeclarationÚDoctypeÚNavigableStringÚPageElementÚProcessingInstructionÚ	ResultSetÚSoupStrainerÚTagz`You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.zuYou need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).c                   sð   e Zd ZdZdZddgZdZdZd4d	d
„Zdd„ Z	dd„ Z
edd„ ƒZdd„ Zdd„ Zddi fdd„Zefdd„Zdd„ Zdd„ Zdd„ Zdd „ Zefd!d"„Zd5d#d$„Zd%d&„ Zd6d(d)„Zd*d+„ Zd7d,d-„Zd.d/„ Zd0ed1f‡ fd2d3„	Z‡  ZS )8r   a  
    This class defines the basic interface called by the tree builders.

    These methods will be called by the parser:
      reset()
      feed(markup)

    The tree builder may call these methods from its feed() implementation:
      handle_starttag(name, attrs) # See note about return value
      handle_endtag(name)
      handle_data(data) # Appends to the current data node
      endData(containerClass=NavigableString) # Ends the current data node

    No matter how complicated the underlying parser is, you should be
    able to build a tree using 'start tag' events, 'end tag' events,
    'data' events, and "done with data" events.

    If you encounter an empty-element tag (aka a self-closing tag,
    like HTML's <br> tag), call handle_starttag and then
    handle_endtag.
    z
[document]ZhtmlZfastz 
	aí  No parser was explicitly specified, so I'm using the best available %(markup_type)s parser for this system ("%(parser)s"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line %(line_number)s of the file %(filename)s. To get rid of this warning, pass the additional argument 'features="%(parser)s"' to the BeautifulSoup constructor.
Ú Nc          
      s–  dˆ krt  d¡ dˆ kr*ˆ d= t  d¡ dˆ krBˆ d= t  d¡ dˆ krZˆ d= t  d¡ d	ˆ krrˆ d	= t  d
¡ ‡ fdd„}|pŠ|ddƒ}|p˜|ddƒ}|r¶t|tƒr¶t  d¡ d}tˆ ƒdkrÞtˆ  ¡ ƒ ¡ }	td|	 ƒ‚|dkr|}
t|tƒrü|g}|dkst|ƒdkr| j	}t
j|Ž }|dkr@tdd |¡ ƒ‚|ƒ }|
|jks|
|jks|jrld}nd}d}yt d¡}W n tk
r˜   Y nX |r®|j}|j}n
tj}d}| d¡}|rè| ¡ }| d¡rè|dd… }|rt|||j|d}t j| j| dd || _|j| _| j| _tƒ | _|| _| j  | ¡ t!|d ƒrZ| "¡ }nÎt|ƒd!kr(t|t#ƒr~d"|ks”t|tƒr(d#|kr(t|tƒr¶t$j%j&s¶| 'd$¡}n|}d%}yt$j% (|¡}W n$ t)k
rò } zW dd}~X Y nX |rt|tƒr| 'd$¡}t  d&| ¡ |  *|¡ xZ| jj+|||d'D ]D\| _,| _-| _.| _/|  0¡  y|  1¡  P W n t2k
r|   Y nX q<W d| _,d| j_3dS )(a_  Constructor.

        :param markup: A string or a file-like object representing
        markup to be parsed.

        :param features: Desirable features of the parser to be used. This
        may be the name of a specific parser ("lxml", "lxml-xml",
        "html.parser", or "html5lib") or it may be the type of markup
        to be used ("html", "html5", "xml"). It's recommended that you
        name a specific parser, so that Beautiful Soup gives you the
        same results across platforms and virtual environments.

        :param builder: A specific TreeBuilder to use instead of looking one
        up based on `features`. You shouldn't need to use this.

        :param parse_only: A SoupStrainer. Only parts of the document
        matching the SoupStrainer will be considered. This is useful
        when parsing part of a document that would otherwise be too
        large to fit into memory.

        :param from_encoding: A string indicating the encoding of the
        document to be parsed. Pass this in if Beautiful Soup is
        guessing wrongly about the document's encoding.

        :param exclude_encodings: A list of strings indicating
        encodings known to be wrong. Pass this in if you don't know
        the document's encoding but you know Beautiful Soup's guess is
        wrong.

        :param kwargs: For backwards compatibility purposes, the
        constructor accepts certain keyword arguments used in
        Beautiful Soup 3. None of these arguments do anything in
        Beautiful Soup 4 and there's no need to actually pass keyword
        arguments into the constructor.
        ZconvertEntitieszˆBS4 does not respect the convertEntities argument to the BeautifulSoup constructor. Entities are always converted to Unicode characters.ZmarkupMassagez“BS4 does not respect the markupMassage argument to the BeautifulSoup constructor. The tree builder is responsible for any necessary markup massage.ZsmartQuotesTozŠBS4 does not respect the smartQuotesTo argument to the BeautifulSoup constructor. Smart quotes are always converted to Unicode characters.ZselfClosingTagsz˜BS4 does not respect the selfClosingTags argument to the BeautifulSoup constructor. The tree builder is responsible for understanding self-closing tags.ZisHTMLz”BS4 does not respect the isHTML argument to the BeautifulSoup constructor. Suggest you use features='lxml' for HTML and features='lxml-xml' for XML.c                s0   | ˆ kr,t  d| |f ¡ ˆ |  }ˆ | = |S d S )NzLThe "%s" argument to the BeautifulSoup constructor has been renamed to "%s.")ÚwarningsÚwarn)Zold_nameÚnew_nameÚvalue)Úkwargs© ú+lib/python3.7/site-packages/bs4/__init__.pyÚdeprecated_argument    s    z3BeautifulSoup.__init__.<locals>.deprecated_argumentZparseOnlyTheseÚ
parse_onlyZfromEncodingÚfrom_encodingzlYou provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.Nr   z2__init__() got an unexpected keyword argument '%s'zjCouldn't find a tree builder with the features you requested: %s. Do you need to install a parser library?ú,ZXMLZHTMLr   Ú__file__)z.pycz.pyoéÿÿÿÿ)ÚfilenameÚline_numberÚparserÚmarkup_typeé   )Ú
stacklevelÚreadé   ó   <ú<Úutf8Fzw"%s" looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.)Úexclude_encodings)4r   r   Ú
isinstanceÚstrÚlenÚlistÚkeysÚpopÚ	TypeErrorÚDEFAULT_BUILDER_FEATURESr   ÚlookupÚFeatureNotFoundÚjoinÚNAMEZALTERNATE_NAMESÚis_xmlÚsysÚ	_getframeÚ
ValueErrorÚ	f_globalsÚf_linenoÚ__dict__ÚgetÚlowerÚendswithÚdictÚNO_PARSER_SPECIFIED_WARNINGÚbuilderZ	known_xmlZ_namespacesr   Zinitialize_soupÚhasattrr&   ÚbytesÚosÚpathÚsupports_unicode_filenamesÚencodeÚexistsÚ	ExceptionÚ_check_markup_is_urlZprepare_markupÚmarkupÚoriginal_encodingZdeclared_html_encodingZcontains_replacement_charactersÚresetÚ_feedr   Úsoup)ÚselfrN   ÚfeaturesrD   r   r   r+   r   r   ÚargZoriginal_featuresZbuilder_classr#   ZcallerÚglobalsr!   r    ZfnlÚvaluesZpossible_filenameÚis_fileÚer   )r   r   Ú__init__V   sØ    '











 zBeautifulSoup.__init__c             C   s&   t | ƒ|  d¡| jdd}| j|_|S )Nzutf-8)rD   r   )ÚtyperJ   rD   rO   )rS   Úcopyr   r   r   Ú__copy__#  s    zBeautifulSoup.__copy__c             C   s&   t | jƒ}d|kr"| jjs"d |d< |S )NrD   )rB   r>   rD   Z	picklable)rS   Údr   r   r   Ú__getstate__/  s    
zBeautifulSoup.__getstate__c                sx   t ˆ tƒrd}d}nt ˆ tƒr(d}d}ndS t‡ fdd„|D ƒƒrt|ˆ krtt ˆ tƒrbˆ  dd	¡}nˆ }t d
| ¡ dS )z— 
        Check if markup looks like it's actually a url and raise a warning 
        if so. Markup can be unicode or str (py2) / bytes (py3).
        ó    )s   http:s   https:ú )zhttp:zhttps:Nc             3   s   | ]}ˆ   |¡V  qd S )N)Ú
startswith)Ú.0Úprefix)rN   r   r   ú	<genexpr>E  s    z5BeautifulSoup._check_markup_is_url.<locals>.<genexpr>zutf-8Úreplacez¿"%s" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an HTTP client like requests to get the document behind the URL, and feed that document to Beautiful Soup.)r,   rF   r-   ÚanyÚdecoder   r   )rN   ZspaceZcant_start_withZdecoded_markupr   )rN   r   rM   6  s    


z"BeautifulSoup._check_markup_is_urlc             C   s@   | j  ¡  | j  | j¡ |  ¡  x| jj| jkr:|  ¡  q"W d S )N)	rD   rP   ZfeedrN   ÚendDataÚ
currentTagÚnameÚROOT_TAG_NAMEÚpopTag)rS   r   r   r   rQ   R  s
    
zBeautifulSoup._feedc             C   sJ   t  | | | j| j¡ d| _| j ¡  g | _d | _g | _g | _	|  
| ¡ d S )Nr   )r   rZ   rD   rl   ZhiddenrP   Úcurrent_datarj   ÚtagStackÚpreserve_whitespace_tag_stackÚpushTag)rS   r   r   r   rP   \  s    
zBeautifulSoup.resetc             K   s   |  |¡ td| j||||ƒS )z+Create a new tag associated with this soup.N)Úupdater   rD   )rS   rk   Ú	namespaceÚnsprefixÚattrsZkwattrsr   r   r   Únew_tagf  s    
zBeautifulSoup.new_tagc             C   s   ||ƒS )z7Create a new NavigableString associated with this soup.r   )rS   ÚsÚsubclassr   r   r   Ú
new_stringk  s    zBeautifulSoup.new_stringc             C   s   t dƒ‚d S )Nz4BeautifulSoup objects don't support insert_before().)ÚNotImplementedError)rS   Ú	successorr   r   r   Úinsert_beforeo  s    zBeautifulSoup.insert_beforec             C   s   t dƒ‚d S )Nz3BeautifulSoup objects don't support insert_after().)rz   )rS   r{   r   r   r   Úinsert_afterr  s    zBeautifulSoup.insert_afterc             C   s@   | j  ¡ }| jr(|| jd kr(| j ¡  | j r:| j d | _| jS )Nr   )ro   r1   rp   rj   )rS   Útagr   r   r   rm   u  s    

zBeautifulSoup.popTagc             C   sN   | j d k	r| j j |¡ | j |¡ | jd | _ |j| jjkrJ| j |¡ d S )Nr   )rj   ÚcontentsÚappendro   rk   rD   Zpreserve_whitespace_tagsrp   )rS   r~   r   r   r   rq   ~  s    
zBeautifulSoup.pushTagc             C   s˜   | j r”d | j ¡}| jsPd}x|D ]}|| jkr"d}P q"W |rPd|krLd}nd}g | _ | jr‚t| jƒdkr‚| jjr~| j |¡s‚d S ||ƒ}|  	|¡ d S )Nr   TFÚ
ra   r   )
rn   r6   rp   ÚASCII_SPACESr   r.   ro   ÚtextÚsearchÚobject_was_parsed)rS   ZcontainerClassrn   Z
strippableÚiÚor   r   r   ri   ‡  s&    

zBeautifulSoup.endDatac       	      C   s˜   |dkr| j }|dk	r|}n| j}d } }}t|tƒrX|j}|j}|j}|dkrX|j}|jdk	}| |||||¡ || _|j	 
|¡ |r”|  |¡ dS )z Add an object to the parse tree.N)rj   Ú_most_recent_elementr,   r   Únext_elementÚnext_siblingÚprevious_siblingÚprevious_elementZsetupr   r€   Ú_linkage_fixer)	rS   r‡   ÚparentZmost_recent_elementrŒ   r‰   r‹   rŠ   Zfixr   r   r   r…   ¥  s$    

zBeautifulSoup.object_was_parsedc             C   sÀ   |j d }|j d }|}||krX|jdk	rX||_|j}|dk	rL||k	rLd|_||_d|_d|_t|tƒrx|j rx| d¡}d|_d|_|}x2|dkr–P n|jdk	r²|j|_||j_P |j}qŠW dS )z,Make sure linkage of this fragment is sound.r   r   NF)	r   rŽ   r‰   rŒ   r‹   rŠ   r,   r   Z_last_descendant)rS   ZelÚfirstZchildZ
descendantZprev_elÚtargetr   r   r   r   Á  s0    



zBeautifulSoup._linkage_fixerTc             C   sn   || j krdS d}t| jƒ}xLt|d ddƒD ]8}| j| }||jkr^||jkr^|r\|  ¡ }P |  ¡ }q.W |S )zÜPops the tag stack up to and including the most recent
        instance of the given tag. If inclusivePop is false, pops the tag
        stack up to but *not* including the most recent instqance of
        the given tag.Nr   r   r   )rl   r.   ro   Úrangerk   rd   rm   )rS   rk   rt   ZinclusivePopZmost_recently_poppedÚ
stack_sizer†   Útr   r   r   Ú	_popToTagé  s    


zBeautifulSoup._popToTagc          	   C   s„   |   ¡  | jr6t| jƒdkr6| jjs2| j ||¡s6dS t| | j||||| j| j	ƒ}|dkr^|S | j	dk	rp|| j	_
|| _	|  |¡ |S )a  Push a start tag on to the stack.

        If this method returns None, the tag was rejected by the
        SoupStrainer. You should proceed as if the tag had not occurred
        in the document. For instance, if this was a self-closing tag,
        don't call handle_endtag.
        r   N)ri   r   r.   ro   rƒ   Z
search_tagr   rD   rj   rˆ   r‰   rq   )rS   rk   rs   rt   ru   r~   r   r   r   Úhandle_starttag   s    


zBeautifulSoup.handle_starttagc             C   s   |   ¡  |  ||¡ d S )N)ri   r”   )rS   rk   rt   r   r   r   Úhandle_endtag  s    zBeautifulSoup.handle_endtagc             C   s   | j  |¡ d S )N)rn   r€   )rS   Údatar   r   r   Úhandle_data   s    zBeautifulSoup.handle_dataFZminimalc                sN   | j r$d}|dkrd| }d| }nd}|s2d}nd}|tt| ƒ |||¡ S )zlReturns a string or Unicode representation of this document.
        To get Unicode, pass None for encoding.r   Nz encoding="%s"z<?xml version="1.0"%s?>
r   )r8   Úsuperr   rh   )rS   Zpretty_printZeventual_encodingZ	formatterZencoding_partrd   Zindent_level)Ú	__class__r   r   rh   #  s    
zBeautifulSoup.decode)r   NNNNN)NN)NT)N) Ú__name__Ú
__module__Ú__qualname__Ú__doc__rl   r3   r‚   rC   rZ   r]   r_   ÚstaticmethodrM   rQ   rP   rv   r   ry   r|   r}   rm   rq   ri   r…   r   r”   r•   r–   r˜   r	   rh   Ú__classcell__r   r   )rš   r   r   6   s:    
 M

		
(

c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚBeautifulStoneSoupz&Deprecated interface to an XML parser.c                s(   d|d< t  d¡ tt| ƒj||Ž d S )NZxmlrT   zxThe BeautifulStoneSoup class is deprecated. Instead of using it, pass features="xml" into the BeautifulSoup constructor.)r   r   r™   r¡   rZ   )rS   Úargsr   )rš   r   r   rZ   ?  s    zBeautifulStoneSoup.__init__)r›   rœ   r   rž   rZ   r    r   r   )rš   r   r¡   <  s   r¡   c               @   s   e Zd ZdS )ÚStopParsingN)r›   rœ   r   r   r   r   r   r£   G  s   r£   c               @   s   e Zd ZdS )r5   N)r›   rœ   r   r   r   r   r   r5   J  s   r5   Ú__main__))rž   Ú
__author__Ú__version__Z__copyright__Z__license__Ú__all__rG   Úrer9   Ú	tracebackr   rD   r   r   Zdammitr   Úelementr   r   r	   r
   r   r   r   r   r   r   r   r   Z_sZ_soupr¡   rL   r£   r;   r5   r›   ÚstdinrR   ÚprintZprettifyr   r   r   r   Ú<module>   s8   4    

