Current File : //usr/lib/python3.6/site-packages/bs4/__pycache__/element.cpython-36.opt-1.pyc
3

&6]$�@s
dZyddlmZWn.ek
rBZzddlmZWYddZ[XnXddlZddlZddlZddl	Z	ddl
mZdZej
ddkZejd�Zdd	�ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d!�d!e�ZGd"d#�d#ee�Z Gd$d%�d%e �Z!Gd&d'�d'e!�Z"Gd(d)�d)e!�Z#Gd*d+�d+e#�Z$Gd,d-�d-e!�Z%Gd.d/�d/e!�Z&Gd0d1�d1e!�Z'Gd2d3�d3e�Z(Gd4d5�d5e�Z)Gd6d7�d7e*�Z+dS)8ZMIT�)�CallableN)�EntitySubstitutionzutf-8�z\s+cs&t�fdd��}|j�fdd��}|S)z>Alias one attribute name to another for backward compatibilitycs
t|��S)N)�getattr)�self)�attr��/usr/lib/python3.6/element.py�aliassz_alias.<locals>.aliascs
t|��S)N)�setattr)r)rrr	r
s)�property�setter)rr
r)rr	�_aliassrc@seZdZddd�ZdS)�NamespacedAttributeNcCsV|dkrtj||�}n*|dkr,tj||�}ntj||d|�}||_||_||_|S)N�:)�str�__new__�prefix�name�	namespace)�clsrrr�objrrr	r"szNamespacedAttribute.__new__)N)�__name__�
__module__�__qualname__rrrrr	r src@seZdZdZdS)�%AttributeValueWithCharsetSubstitutionz=A stand-in object for a character encoding specified in HTML.N)rrr�__doc__rrrr	r/src@s eZdZdZdd�Zdd�ZdS)�CharsetMetaAttributeValuez�A generic stand-in for the value of a meta tag's 'charset' attribute.

    When Beautiful Soup parses the markup '<meta charset="utf8">', the
    value of the 'charset' attribute will be one of these objects.
    cCstj||�}||_|S)N)rr�original_value)rrrrrr	r9sz!CharsetMetaAttributeValue.__new__cCs|S)Nr)r�encodingrrr	�encode>sz CharsetMetaAttributeValue.encodeN)rrrrrr rrrr	r2src@s.eZdZdZejdej�Zdd�Zdd�Z	dS)�ContentMetaAttributeValueaA generic stand-in for the value of a meta tag's 'content' attribute.

    When Beautiful Soup parses the markup:
     <meta http-equiv="content-type" content="text/html; charset=utf8">

    The value of the 'content' attribute will be one of these objects.
    z((^|;)\s*charset=)([^;]*)cCs6|jj|�}|dkr tjt|�Stj||�}||_|S)N)�
CHARSET_RE�searchrrr)rr�matchrrrr	rMsz!ContentMetaAttributeValue.__new__cs�fdd�}|jj||j�S)Ncs|jd��S)N�)�group)r$)rrr	�rewriteXsz1ContentMetaAttributeValue.encode.<locals>.rewrite)r"�subr)rrr'r)rr	r Wsz ContentMetaAttributeValue.encodeN)
rrrr�re�compile�Mr"rr rrrr	r!Bs
r!c@sVeZdZdZeddg�Zedg�Zeddg�Zedd��Z	edd	��Z
ed
d��ZdS)
�HTMLAwareEntitySubstitutiona%Entity substitution rules that are aware of some HTML quirks.

    Specifically, the contents of <script> and <style> tags should not
    undergo entity substitution.

    Incoming NavigableString objects are checked to see if they're the
    direct children of a <script> or <style> tag.
    ZscriptZstyle�preZtextareacCs.t|t�r&|jdk	r&|jj|jkr&|S||�S)N)�
isinstance�NavigableString�parentr�cdata_containing_tags)r�ns�frrr	�_substitute_if_appropriatems


z6HTMLAwareEntitySubstitution._substitute_if_appropriatecCs|j|tj�S)N)r4r�substitute_html)rr2rrr	r5wsz+HTMLAwareEntitySubstitution.substitute_htmlcCs|j|tj�S)N)r4r�substitute_xml)rr2rrr	r6|sz*HTMLAwareEntitySubstitution.substitute_xmlN)rrrr�setr1Zpreformatted_tags�preserve_whitespace_tags�classmethodr4r5r6rrrr	r,\s	

r,c@seZdZdZdZdd�ZdS)�	Formatterz6Contains information about how to format a parse tree.�/cOs
t��dS)z1Transform certain characters into named entities.N)�NotImplementedError)r�args�kwargsrrr	�substitute_entities�szFormatter.substitute_entitiesN)rrrr�void_element_close_prefixr?rrrr	r:�sr:c@seZdZdZdd�ZdS)�
HTMLFormatterzThe default HTML formatter.cOstj||�S)N)r,r5)rr=r>rrr	�
substitute�szHTMLFormatter.substituteN)rrrrrBrrrr	rA�srAc@seZdZdZdd�ZdS)�MinimalHTMLFormatterzA minimal HTML formatter.cOstj||�S)N)r,r6)rr=r>rrr	rB�szMinimalHTMLFormatter.substituteN)rrrrrBrrrr	rC�srCc@seZdZdZdZdS)�HTML5Formatterz5An HTML formatter that omits the slash in a void tag.N)rrrrr@rrrr	rD�srDc@seZdZdZdd�ZdS)�XMLFormatterz+Substitute only the essential XML entities.cOstj||�S)N)rr6)rr=r>rrr	rB�szXMLFormatter.substituteN)rrrrrBrrrr	rE�srEc@seZdZdZdd�ZdS)�HTMLXMLFormatterzFormat XML using HTML rules.cOstj||�S)N)r,r5)rr=r>rrr	rB�szHTMLXMLFormatter.substituteN)rrrrrBrrrr	rF�srFc@s`eZdZdZe�e�e�dd�Ze�e	�dd�Z
d]dd�Zedd	��Z
d
d�Zd^dd
�Zed�Zed�Zdd�ZeZdd�ZeZeZdd�Zdd�Zd_dd�ZeZdd�Zdd�Zdd �Zd!d"�Zdidfd#d$�Z e Z!diddfd%d&�Z"e"Z#didfd'd(�Z$e$Z%diddfd)d*�Z&e&Z'e&Z(didfd+d,�Z)e)Z*diddfd-d.�Z+e+Z,e+Z-didfd/d0�Z.e.Z/diddfd1d2�Z0e0Z1e0Z2difd3d4�Z3e3Z4didfd5d6�Z5e5Z6e5Z7ed7d8��Z8ed9d:��Z9d;d<�Z:d=d>�Z;ed?d@��Z<edAdB��Z=edCdD��Z>edEdF��Z?edGdH��Z@eAjBdI�ZCeAjBd`�ZDdadLdM�ZEdNdO�ZFdbdQdR�ZGdSdT�ZHdUdV�ZIdWdX�ZJdYdZ�ZKd[d\�ZLdS)c�PageElementzeContains the navigational information for some part of the page
    (either a tag or a piece of text)N)�htmlZhtml5�minimalN)rHrINrIcCsBt|t�r|j|�}|dkr"|}nt|�r4||�}n
|j|�}|S)z2Format the given string using the given formatter.N)r.r�_formatter_for_name�callablerB)r�s�	formatter�outputrrr	�
format_string�s



zPageElement.format_stringcCs.|jdk	r|jS|jdkr&t|dd�S|jjS)aJIs this element part of an XML tree or an HTML tree?

        This is used when mapping a formatter name ("minimal") to an
        appropriate function (one that performs entity-substitution on
        the contents of <script> and <style> tags, or not). It can be
        inefficient, but it should be called very rarely.
        N�is_xmlF)�	known_xmlr0r�_is_xml)rrrr	rR�s
	

zPageElement._is_xmlcCs*|jr|jj|t��S|jj|t��SdS)z<Look up a formatter function based on its name and the tree.N)rR�XML_FORMATTERS�getrE�HTML_FORMATTERSrA)rrrrr	rJ�szPageElement._formatter_for_namecCs~||_||_|dk	r||j_||_|jr0||j_||_|jrD||j_|rh|jdk	rh|jjrh|jjd}||_|rz||j_dS)zNSets up the initial relations between this element and
        other elements.Nr%���)r0�previous_element�next_element�next_sibling�previous_sibling�contents)rr0rWrXrZrYrrr	�setup�s zPageElement.setuprYrZcCsV|jstd��||krdS||jkr,td��|j}|jj|�}|j�|j||�|S)Nz]Cannot replace one element with another when theelement to be replaced is not part of a tree.z%Cannot replace a Tag with its parent.)r0�
ValueError�index�extract�insert)r�replace_withZ
old_parent�my_indexrrr	ras
zPageElement.replace_withcCsT|j}|jstd��|jj|�}|j�x&t|jdd��D]}|j||�q<W|S)NzSCannot replace an element with its contents when thatelement is not part of a tree.)r0r]r^r_�reversedr[r`)rZ	my_parentrb�childrrr	�unwrap%szPageElement.unwrapcCs|j|�}|j|�|S)N)ra�append)rZwrap_inside�merrr	�wrap3s

zPageElement.wrapcCs�|jdk	r|jj|jj|�=|j�}|j}|jdk	rF|j|k	rF||j_|dk	r`||jk	r`|j|_d|_d|_d|_|jdk	r�|j|jk	r�|j|j_|jdk	r�|j|jk	r�|j|j_d|_|_|S)z0Destructively rips this element out of the tree.N)r0r[r^�_last_descendantrXrWrZrY)r�
last_childrXrrr	r_8s(






zPageElement.extractTcCsN|r|jr|jj}n$|}xt|t�r6|jr6|jd}qW|rJ||krJd}|S)z8Finds the last element beneath this object to be parsed.r%NrV)rYrWr.�Tagr[)rZis_initializedZaccept_selfrjrrr	riUs

zPageElement._last_descendantcCs�|dkrtd��||kr td��t|t�r>t|t�r>t|�}ddlm}t||�r�x&t|j�D]}|j||�|d7}q`WdSt	|t
|j��}t|d�r�|jdk	r�|j|kr�|j
|�}||kr�|d8}|j�||_d}|dkr�d|_||_n(|j|d}||_||j_|jd�|_|jdk	�r,||j_|jd�}|t
|j�k�r�d|_|}d}	x2|	dk�r�|dk	�r�|j}	|j}|	dk	�rVP�qVW|	dk	�r�|	|_nd|_n*|j|}
|
|_|jdk	�r�||j_|
|_|jdk	�r�||j_|jj||�dS)NzCannot insert None into a tag.z Cannot insert a tag into itself.r)�
BeautifulSoupr%r0F)r]r.rr/Zbs4rl�listr[r`�min�len�hasattrr0r^r_rZrWrYrirX)rZposition�	new_childrlZsubchildZ
current_indexZprevious_childZnew_childs_last_elementr0Zparents_next_siblingZ
next_childrrr	r`csh







zPageElement.insertcCs|jt|j�|�dS)z2Appends the given tag to the contents of this tag.N)r`ror[)r�tagrrr	rf�szPageElement.appendcCsR||krtd��|j}|dkr&td��t|t�r8|j�|j|�}|j||�dS)z�Makes the given element the immediate predecessor of this one.

        The two elements will have the same parent, and the given element
        will be immediately before this one.
        z&Can't insert an element before itself.Nz2Element has no parent, so 'before' has no meaning.)r]r0r.rGr_r^r`)rZpredecessorr0r^rrr	�
insert_before�s

zPageElement.insert_beforecCsV||krtd��|j}|dkr&td��t|t�r8|j�|j|�}|j|d|�dS)z�Makes the given element the immediate successor of this one.

        The two elements will have the same parent, and the given element
        will be immediately after this one.
        z%Can't insert an element after itself.Nz1Element has no parent, so 'after' has no meaning.r%)r]r0r.rGr_r^r`)rZ	successorr0r^rrr	�insert_after�s

zPageElement.insert_aftercKs|j|j|||f|�S)zjReturns the first item that matches the given criteria and
        appears after this Tag in the document.)�	_find_one�
find_all_next)rr�attrs�textr>rrr	�	find_next�szPageElement.find_nextcKs|j|||||jf|�S)zbReturns all items that match the given criteria and appear
        after this Tag in the document.)�	_find_all�
next_elements)rrrwrx�limitr>rrr	rv�szPageElement.find_all_nextcKs|j|j|||f|�S)z{Returns the closest sibling to this Tag that matches the
        given criteria and appears after this Tag in the document.)ru�find_next_siblings)rrrwrxr>rrr	�find_next_sibling�szPageElement.find_next_siblingcKs|j|||||jf|�S)zqReturns the siblings of this Tag that match the given
        criteria and appear after this Tag in the document.)rz�
next_siblings)rrrwrxr|r>rrr	r}�szPageElement.find_next_siblingscKs|j|j|||f|�S)zkReturns the first item that matches the given criteria and
        appears before this Tag in the document.)ru�find_all_previous)rrrwrxr>rrr	�
find_previous�szPageElement.find_previouscKs|j|||||jf|�S)zcReturns all items that match the given criteria and appear
        before this Tag in the document.)rz�previous_elements)rrrwrxr|r>rrr	r��szPageElement.find_all_previouscKs|j|j|||f|�S)z|Returns the closest sibling to this Tag that matches the
        given criteria and appears before this Tag in the document.)ru�find_previous_siblings)rrrwrxr>rrr	�find_previous_siblingsz!PageElement.find_previous_siblingcKs|j|||||jf|�S)zrReturns the siblings of this Tag that match the given
        criteria and appear before this Tag in the document.)rz�previous_siblings)rrrwrxr|r>rrr	r�sz"PageElement.find_previous_siblingscKs&d}|j||df|�}|r"|d}|S)zOReturns the closest parent of this Tag that matches the given
        criteria.Nr%r)�find_parents)rrrwr>�r�lrrr	�find_parents
zPageElement.find_parentcKs|j||d||jf|�S)zFReturns the parents of this Tag that match the given
        criteria.N)rz�parents)rrrwr|r>rrr	r�!szPageElement.find_parentscCs|jS)N)rX)rrrr	�next*szPageElement.nextcCs|jS)N)rW)rrrr	�previous.szPageElement.previouscKs&d}||||df|�}|r"|d}|S)Nr%rr)r�methodrrwrxr>r�r�rrr	ru4s
zPageElement._find_onecs8|dkrd|kr|d}|d=t�t�r.�}nt�||f|�}|dkr�|r�|r�|r҈dksh�dkr�dd�|D�}t||�St�t�r҈jd�dkr��jdd�\��nd������fdd�|D�}t||�St|�}	xXyt|�}
Wntk
�rPYnX|
r�|j|
�}|r�|	j	|�|r�t
|	�|kr�Pq�W|	S)	z8Iterates over a generator looking for things that match.N�stringTcss|]}t|t�r|VqdS)N)r.rk)�.0�elementrrr	�	<genexpr>Jsz(PageElement._find_all.<locals>.<genexpr>rr%c3sB|]:}t|t�r|j�ks6|j�kr�dks6|j�kr|VqdS)N)r.rkrr)r�r�)�
local_namerrrr	r�Ws



)r.�SoupStrainer�	ResultSetr�count�splitr��
StopIterationr#rfro)rrrwrxr|�	generatorr>Zstrainer�result�results�i�foundr)r�rrr	rz;s<


	


zPageElement._find_allccs$|j}x|dk	r|V|j}qWdS)N)rX)rr�rrr	r{qs
zPageElement.next_elementsccs$|j}x|dk	r|V|j}qWdS)N)rY)rr�rrr	rxs
zPageElement.next_siblingsccs$|j}x|dk	r|V|j}qWdS)N)rW)rr�rrr	r�s
zPageElement.previous_elementsccs$|j}x|dk	r|V|j}qWdS)N)rZ)rr�rrr	r��s
zPageElement.previous_siblingsccs$|j}x|dk	r|V|j}qWdS)N)r0)rr�rrr	r��s
zPageElement.parentsz^[a-zA-Z0-9][-.a-zA-Z0-9:_]*$zX^(?P<tag>[a-zA-Z0-9][-.a-zA-Z0-9:_]*)?\[(?P<attribute>[\w-]+)(?P<operator>[=~\|\^\$\*]?)z=?"?(?P<value>[^\]"]*)"?\]$cCs.|j||�}t|t�s t|t�r*dj|�}|S)z�Force an attribute value into a string representation.

        A multi-valued attribute will be converted into a
        space-separated stirng.
        � )rTr.rm�tuple�join)r�value�defaultrrr	�_attr_value_as_string�s
z!PageElement._attr_value_as_stringcs�s�S��fdd�}|SdS)Ncs|j�ko�|�S)N)r)rr)�function�tag_namerr	�_match�sz1PageElement._tag_name_matches_and.<locals>._matchr)rr�r�r�r)r�r�r	�_tag_name_matches_and�sz!PageElement._tag_name_matches_and�cs�|dkr��fdd�S|dkr0��fdd�}|S|dkrF��fdd�S|d	kr\��fd
d�S|dkrr��fdd�S|d
kr���fdd�}|S�fdd�SdS)z�Create a function that performs a CSS selector operation.

        Takes an operator, attribute and optional value. Returns a
        function that will return True for elements that match that
        combination.
        �=cs|j���kS)N)r�)�el)�	attributer�rr	�<lambda>�sz0PageElement._attribute_checker.<locals>.<lambda>�~cs&|j�g�}t|t�s|j�}�|kS)N)rTr.rmr�)r��attribute_value)r�r�rr	�_includes_value�s
z7PageElement._attribute_checker.<locals>._includes_value�^cs|j�d�j��S)Nr�)r��
startswith)r�)r�r�rr	r��s�$cs|j�d�j��S)Nr�)r��endswith)r�)r�r�rr	r��s�*cs�|j�d�kS)Nr�)r�)r�)r�r�rr	r��s�|cs"|j�d�}|�kp |j�d�S)Nr��-)r�r�)r�r�)r�r�rr	�_is_or_starts_with_dash�sz?PageElement._attribute_checker.<locals>._is_or_starts_with_dashcs
|j��S)N)�has_attr)r�)r�rr	r��sNr)r�operatorr�r�r�r�r)r�r�r	�_attribute_checker�szPageElement._attribute_checkercCs|jS)N)r{)rrrr	�
nextGenerator�szPageElement.nextGeneratorcCs|jS)N)r)rrrr	�nextSiblingGenerator�sz PageElement.nextSiblingGeneratorcCs|jS)N)r�)rrrr	�previousGenerator�szPageElement.previousGeneratorcCs|jS)N)r�)rrrr	�previousSiblingGenerator�sz$PageElement.previousSiblingGeneratorcCs|jS)N)r�)rrrr	�parentGenerator�szPageElement.parentGenerator)rI)NNNNN)TTzs^(?P<tag>[a-zA-Z0-9][-.a-zA-Z0-9:_]*)?\[(?P<attribute>[\w-]+)(?P<operator>[=~\|\^\$\*]?)=?"?(?P<value>[^\]"]*)"?\]$)N)r�)MrrrrrArDrCrUrFrErSrOrrRrJr\rZnextSiblingZpreviousSiblingraZreplaceWithreZreplace_with_childrenZreplaceWithChildrenrhr_riZ_lastRecursiveChildr`rfrsrtryZfindNextrvZfindAllNextr~ZfindNextSiblingr}ZfindNextSiblingsZfetchNextSiblingsr�ZfindPreviousr�ZfindAllPreviousZ
fetchPreviousr�ZfindPreviousSiblingr�ZfindPreviousSiblingsZfetchPreviousSiblingsr�Z
findParentr�ZfindParentsZfetchParentsr�r�rurzr{rr�r�r�r)r*�tag_name_re�attribselect_rer�r�r�r�r�r�r�r�rrrr	rG�s�


J
6	
	

+rGc@s\eZdZdZdZdZdd�Zdd�Zdd�Zd	d
�Z	ddd
�Z
edd��Zej
dd��ZdS)r/r�NcCs2t|t�rtj||�}ntj||t�}|j�|S)a-Create a new NavigableString.

        When unpickling a NavigableString, this method is called with
        the string in DEFAULT_OUTPUT_ENCODING. That encoding needs to be
        passed in to the superclass's __new__ or the superclass won't know
        how to handle non-ASCII characters.
        )r.rr�DEFAULT_OUTPUT_ENCODINGr\)rr��urrr	r�s

zNavigableString.__new__cCst|�|�S)z�A copy of a NavigableString has the same contents and class
        as the original, but it is not connected to the parse tree.
        )�type)rrrr	�__copy__szNavigableString.__copy__cCs
t|�fS)N)r)rrrr	�__getnewargs__szNavigableString.__getnewargs__cCs$|dkr|Std|jj|f��dS)z�text.string gives you text. This is for backwards
        compatibility for Navigable*String, but for CData* it lets you
        get the string without the CData wrapper.r�z!'%s' object has no attribute '%s'N)�AttributeError�	__class__r)rrrrr	�__getattr__s
zNavigableString.__getattr__rIcCs|j||�}|j||jS)N)rO�PREFIX�SUFFIX)rrMrNrrr	�output_readyszNavigableString.output_readycCsdS)Nr)rrrr	r#szNavigableString.namecCstd��dS)Nz)A NavigableString cannot be given a name.)r�)rrrrr	r's)rI)rrrr�r�rQrr�r�r�r�rrr
rrrr	r/�s
r/c@seZdZdZddd�ZdS)�PreformattedStringz�A NavigableString not subject to the normal formatting rules.

    The string will be passed into the formatter (to trigger side effects),
    but the return value will be ignored.
    rIcCs|j||�|j||jS)zUCData strings are passed into the formatter.
        But the return value is ignored.)rOr�r�)rrMrrr	r�2szPreformattedString.output_readyN)rI)rrrrr�rrrr	r�+sr�c@seZdZdZdZdS)�CDataz	<![CDATA[z]]>N)rrrr�r�rrrr	r�8sr�c@seZdZdZdZdZdS)�ProcessingInstructionzA SGML processing instruction.z<?�>N)rrrrr�r�rrrr	r�=sr�c@seZdZdZdZdZdS)�XMLProcessingInstructionzAn XML processing instruction.z<?z?>N)rrrrr�r�rrrr	r�Csr�c@seZdZdZdZdS)�Commentz<!--z-->N)rrrr�r�rrrr	r�Hsr�c@seZdZdZdZdS)�Declarationz<?z?>N)rrrr�r�rrrr	r�Nsr�c@s eZdZedd��ZdZdZdS)�DoctypecCsN|pd}|dk	r2|d|7}|dk	rF|d|7}n|dk	rF|d|7}t|�S)Nr�z PUBLIC "%s"z "%s"z SYSTEM "%s")r�)rrZpub_idZ	system_idr�rrr	�for_name_and_idsUszDoctype.for_name_and_idsz
<!DOCTYPE z>
N)rrrr9r�r�r�rrrr	r�Ssr�c	@sHeZdZdZdhdd�Zed�Zdd�Zedd	��Z	e	Z
ed
d��Zejdd��Zd
e
effdd�Zee�Zedd��Zdd
e
effdd�ZeZee�Zdd�Zdidd�Zdd�Zdjdd�Zdkdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Z d-d.�Z!d/d0�Z"d1d2�Z#d3d4�Z$d5d6�Z%d7d8�Z&dld:d;�Z'd<d=�Z(d>d?�Z)e*�rTe(Z)Z'e+dd@dAfdBdC�Z,dDdE�Z-de+d@fdFdG�Z.dmdHdI�Z/de+d@fdJdK�Z0de+d@fdLdM�Z1e+d
dNfdOdP�Z2didQdfdRdS�Z3e3Z4didQddfdTdU�Z5e5Z6e5Z7edVdW��Z8edXdY��Z9dZd[d\gZ:d
Z;e<j=d]�Z>d^d_�Z?dnd`da�Z@dbdc�ZAddde�ZBdfdg�ZCdS)orkz=Represents a found HTML tag with its attributes and contents.Nc
Cs�|dkrd|_n|j|_|dkr(td��||_||_||_|dk	rJ|j}
n|	rTg}
ntj}
|
|_|dkrni}n4|r�|dk	r�|jr�|j	|j|�}q�t
|�}nt
|�}|r�|j|_n|	|_||_
g|_|j||�d|_|dk	r�|j|�|j|�|_nd|_dS)zBasic constructor.Nz%No value provided for new tag's name.F)�parser_classr�r]rrrr8r,Zcdata_list_attributesZ$_replace_cdata_list_attribute_values�dictrPrQrwr[r\�hiddenZset_up_substitutions�can_be_empty_element)r�parser�builderrrrrwr0r�rPr8rrr	�__init__isB


zTag.__init__r�c	Csht|�d|j|j|j|j|j|jd�}xdD]}t||t||��q,Wx|j	D]}|j
|j��qNW|S)z�A copy of a Tag is a new Tag, unconnected to the parse tree.
        Its contents are a copy of the old Tag's contents.
        N)rPr�r�)r�r�)r�r�rrrrwrRrrr[rfr�)rZclonerrdrrr	r��s
zTag.__copy__cCst|j�dko|jS)a7Is this tag an empty-element tag? (aka a self-closing tag)

        A tag that has contents is never an empty-element tag.

        A tag that has no contents may or may not be an empty-element
        tag. It depends on the builder used to create the tag. If the
        builder has a designated list of empty-element tags, then only
        a tag whose name shows up in that list is considered an
        empty-element tag.

        If the builder has no designated list of empty-element tags,
        then any tag with no contents is an empty-element tag.
        r)ror[r�)rrrr	�is_empty_element�szTag.is_empty_elementcCs0t|j�dkrdS|jd}t|t�r*|S|jS)aqConvenience property to get the single string within this tag.

        :Return: If this tag has a single string child, return value
         is that string. If this tag has no children, or more than one
         child, return value is None. If this tag has one child tag,
         return value is the 'string' attribute of the child tag,
         recursively.
        r%Nr)ror[r.r/r�)rrdrrr	r��s


z
Tag.stringcCs|j�|j|j|��dS)N)�clearrfr�)rr�rrr	r��sFccs^xX|jD]N}|dkr t|t�s|dk	r6t|�|kr6q|rP|j�}t|�dkrPq|VqWdS)z�Yield all strings of certain classes, possibly stripping them.

        By default, yields only NavigableString and CData objects. So
        no comments, processing instructions, etc.
        Nr)�descendantsr.r/r��stripro)rr��typesZ
descendantrrr	�_all_strings�szTag._all_stringsccsx|jd�D]
}|VqWdS)NT)r�)rr�rrr	�stripped_strings�szTag.stripped_stringsr�cCs|jdd�|j||d�D��S)zP
        Get all child strings, concatenated using the given separator.
        cSsg|]}|�qSrr)r�rLrrr	�
<listcomp>�sz Tag.get_text.<locals>.<listcomp>)r�)r�r�)rZ	separatorr�r�rrr	�get_text�szTag.get_textcCs8|j�|}x&|dk	r2|j}|jj�g|_|}qWdS)z/Recursively destroys the contents of this tree.N)r_rX�__dict__r�r[)rr�r�rrr	�	decompose�s

z
Tag.decomposecCs^|r:xT|jdd�D] }t|t�r,|j�q|j�qWn x|jdd�D]}|j�qJWdS)zP
        Extract all children. If decompose is True, decompose instead.
        N)r[r.rkr�r_)rr�r�rrr	r�s

z	Tag.clearcCs0x"t|j�D]\}}||kr|SqWtd��dS)z�
        Find the index of a child by identity, not value. Avoids issues with
        tag.contents.index(element) getting the index of equal elements.
        zTag.index: element not in tagN)�	enumerater[r])rr�r�rdrrr	r^sz	Tag.indexcCs|jj||�S)z�Returns the value of the 'key' attribute for the tag, or
        the value given for 'default' if it doesn't have that
        attribute.)rwrT)r�keyr�rrr	rTszTag.getcCs |j||�}t|t�s|g}|S)z-The same as get(), but always returns a list.)rTr.rm)rr�r�r�rrr	�get_attribute_lists
zTag.get_attribute_listcCs
||jkS)N)rw)rr�rrr	r�&szTag.has_attrcCst|�j�S)N)r�__hash__)rrrr	r�)szTag.__hash__cCs
|j|S)zqtag[key] returns the value of the 'key' attribute for the tag,
        and throws an exception if it's not there.)rw)rr�rrr	�__getitem__,szTag.__getitem__cCs
t|j�S)z0Iterating over a tag iterates over its contents.)�iterr[)rrrr	�__iter__1szTag.__iter__cCs
t|j�S)z:The length of a tag is the length of its list of contents.)ror[)rrrr	�__len__5szTag.__len__cCs
||jkS)N)r[)r�xrrr	�__contains__9szTag.__contains__cCsdS)z-A tag is non-None even if it has no contents.Tr)rrrr	�__bool__<szTag.__bool__cCs||j|<dS)zKSetting tag[key] sets the value of the 'key' attribute for the
        tag.N)rw)rr�r�rrr	�__setitem__@szTag.__setitem__cCs|jj|d�dS)z;Deleting tag[key] deletes all 'key' attributes for the tag.N)rw�pop)rr�rrr	�__delitem__EszTag.__delitem__cOs|j||�S)z�Calling a tag like a function is the same as calling its
        find_all() method. Eg. tag('a') returns a list of all the A tags
        found within this tag.)�find_all)rr=r>rrr	�__call__IszTag.__call__cCsvt|�dkr@|jd�r@|dd�}tjdt|d��|j|�S|jd�r`|dkr`|j|�Std|j|f��dS)	N�rkz�.%(name)sTag is deprecated, use .find("%(name)s") instead. If you really were looking for a tag called %(name)sTag, use .find("%(name)sTag"))r�__r[z!'%s' object has no attribute '%s'���)	ror��warnings�warnr��findr�r�r�)rrrr�rrr	r�Os

zTag.__getattr__cCs�||krdSt|d�sXt|d�sXt|d�sX|j|jksX|j|jksXt|�t|�kr\dSx(t|j�D]\}}||j|krhdSqhWdS)zReturns true iff this tag has the same name, the same attributes,
        and the same contents (recursively) as the given tag.Trrwr[F)rprrwror�r[)r�otherr�Zmy_childrrr	�__eq__`sz
Tag.__eq__cCs
||kS)zZReturns true iff this tag is not identical to the other tag,
        as defined in __eq__.r)rr�rrr	�__ne__qsz
Tag.__ne__�unicode-escapecCstr|j�S|j|�SdS)zRenders this tag as a string.N)�PY3K�decoder )rrrrr	�__repr__vszTag.__repr__cCs|j�S)N)r)rrrr	�__unicode__�szTag.__unicode__cCstr|j�S|j�SdS)N)rrr )rrrr	�__str__�szTag.__str__rI�xmlcharrefreplacecCs|j|||�}|j||�S)N)rr )rr�indent_levelrM�errorsr�rrr	r �sz
Tag.encodecCs|dk	o|j|jkS)z"Should this tag be pretty-printed?N)rr8)rrrrr	�_should_pretty_print�szTag._should_pretty_printcCsRt|t�r t|�r |j|�}g}|jr�x�t|jj��D]�\}}|dkrP|}nrt|t�sdt|t�rpdj	|�}n0t|t
�s�t
|�}nt|t�r�|dk	r�|j|�}|j
||�}t
|�dtj|�}|j|�q:Wd}	d}
d}|jr�|jd}|j�rd}	t|t��r |j�p|	}	nd||jf}
|j|�}d}
d}|dk	�rHd|d}|�r\|}
|d}nd}|j|||�}|j�r||}n�g}d}|�r�ddj	|�}|dk	�r�|j|�|jd||j||	f�|�r�|jd	�|j|�|�r|�r|d
d	k�r|jd	�|�r|
�r|j|
�|j|
�|dk	�rD|
�rD|j�rD|jd	�dj	|�}|S)a�Returns a Unicode representation of this tag and its contents.

        :param eventual_encoding: The tag is destined to be
           encoded into this encoding. This method is _not_
           responsible for performing that encoding. This information
           is passed in so that it can be substituted in if the
           document contains a <META> tag that mentions the document's
           encoding.
        Nr�r�r�rz</%s%s>r%z
<%s%s%s%s>�
rV)r.r:rKrJrw�sorted�itemsrmr�r�rrr rOrZquoted_attribute_valuerfrr�r@rr	�decode_contentsr�rY)rr�eventual_encodingrMrwr��valZdecodedrx�closeZcloseTagr�pretty_printZspaceZindent_spaceZindent_contentsr[rLZattribute_stringrrr	r�sx


















z
Tag.decodecCs*|dkr|jd|d�S|j|d|d�SdS)NT)rM)rr )rrrMrrr	�prettify�szTag.prettifycCs�t|t�r t|�r |j|�}|dk	}g}x�|D]�}d}t|t�rP|j|�}nt|t�rn|j|j|||��|r�|r�|j	dkr�|j
�}|r2|r�|j	dkr�|jd|d�|j|�|r2|j	dkr2|jd�q2Wdj|�S)avRenders the contents of this tag as a Unicode string.

        :param indent_level: Each line of the rendering will be
           indented this many spaces.

        :param eventual_encoding: The tag is destined to be
           encoded into this encoding. This method is _not_
           responsible for performing that encoding. This information
           is passed in so that it can be substituted in if the
           document contains a <META> tag that mentions the document's
           encoding.

        :param formatter: The output formatter responsible for converting
           entities to Unicode characters.
        Nr-r�r%r
r�)r.r:rKrJr/r�rkrfrrr�r�)rrrrMrrL�crxrrr	r
s(




zTag.decode_contentscCs|j|||�}|j|�S)acRenders the contents of this tag as a bytestring.

        :param indent_level: Each line of the rendering will be
           indented this many spaces.

        :param eventual_encoding: The bytestring will be in this encoding.

        :param formatter: The output formatter responsible for converting
           entities to Unicode characters.
        )r
r )rrrrMr[rrr	�encode_contents+szTag.encode_contentsrcCs|sd}|j||d�S)N)rr)r)rrZprettyPrintZindentLevelrrr	�renderContents=szTag.renderContentsTcKs*d}|j||||df|�}|r&|d}|S)zLReturn only the first child of this Tag matching the given
        criteria.Nr%r)r�)rrrw�	recursiverxr>r�r�rrr	r�Fs
zTag.findcKs&|j}|s|j}|j|||||f|�S)a�Extracts a list of Tag objects that match the given
        criteria.  You can specify the name of the Tag and any
        attributes you want the Tag to have.

        The value of a key-value pair in the 'attrs' map can be a
        string, a list of strings, a regular expression object, or a
        callable that takes a string and returns whether or not the
        string matches for some custom definition of 'matches'. The
        same is true of the tag name.)r��childrenrz)rrrwrrxr|r>r�rrr	r�QszTag.find_allcCs
t|j�S)N)r�r[)rrrr	reszTag.childrenccs@t|j�sdS|j�j}|jd}x||k	r:|V|j}q$WdS)Nr)ror[rirX)rZstopNodeZcurrentrrr	r�js



zTag.descendantsr��+r�z
"[^"]*:[^"]*"cCs|j|dd�}|r|dSdS)z9Perform a CSS selection operation on the current element.r%)r|rN)�select)r�selectorr�rrr	�
select_oneyszTag.select_onec)s:d|kr�g}dd�|jd�D�}t�}t�}x�|D]~}|dkrJtd|��||krTq2|j|��j||d�}	x0|	D](}
t|
�}||krr|j|
�|j|�qrW|r2t|�|kr2Pq2W|Stj|�}�g}
|d3�j	kr�td|d4���j
r�td	|��x�t|�D�]�\}}g}tg�}||d�j	k�rB�j
�rtd
��q�j
�rVtd|�d�d�d}�j
j|�}|dk	�r�|j�\�}}}�j|||�}�n�d
|k�r�|jd
d�\���fdd�}|}�nxd|k�r|jdd�\�}t|jd����fdd�}|}�n<d|k�rԈjj|��r�|jdd�\�}�dk�r@td��tjd|�}g}|dk�rd|}d}n|j�\}}|dk�r�yt|�}Wntd��YnX|dk�r�td��Gdd�dt�}||�j}ntd��nl|dk�r�n`|dk�r�dd��nL|d k�rd!d��n8|d"k�r d#d$�} | �n �jj|��r4|�ntd%|����rh||d����fd&d'�}!|!}"nV|dk�r��j
�r���r�d(}#n�}#td)|#��j
�r��fd*d+�}$|$}"nd,d�}"n|}"d-}%�x|
D�]}&�j
�r�td.|&jt|&j�f�x�|"|&�D]�}
t|
t��s�q���r$|
j�k�r$�q�|dk	�rTy||
�}'Wntk
�rRPYnX|dk�sd|'�r��j
�r�td/|
jt|
j�f�t|
�|k�r�|j|
�|jt|
��n �j
�r�td0|
jt|
j�f��q�W�q�W|}
�qW|�rt|
�|k�r|
d|�}
�j
�r6td1�x"|
D]}(td2|(j|(jf��qW|
S)5z9Perform a CSS selection operation on the current element.�,cSsg|]}|j��qSr)r�)r�r�rrr	r��szTag.select.<locals>.<listcomp>r�z"Invalid group selection syntax: %s)r|r%z-Final combinator "%s" is missing an argument.zRunning CSS selector "%s"z0  Token was consumed by the previous combinator.z Considering token "%s"N�#cs|jdd��kS)N�id)rT)rr)�tag_idrr	�
id_matches�szTag.select.<locals>.id_matches�.cs�j|jdg��S)N�class)�issubsetrT)�	candidate)�classesrr	�
classes_match�sz!Tag.select.<locals>.classes_matchrz0A pseudo-class must be prefixed with a tag name.z([a-zA-Z\d-]+)\(([a-zA-Z\d]+)\)znth-of-typezMOnly numeric values are currently supported for the nth-of-type pseudo-class.z2nth-of-type pseudo-class value must be at least 1.c@seZdZdd�Zdd�ZdS)zTag.select.<locals>.CountercSsd|_||_dS)Nr)r��destination)rr'rrr	r��sz$Tag.select.<locals>.Counter.__init__cSs&|jd7_|j|jkrdSdSdS)Nr%TF)r�r')rrrrrr	�nth_child_of_type�sz-Tag.select.<locals>.Counter.nth_child_of_typeN)rrrr�r(rrrr	�Counter�sr)z?Only the following pseudo-classes are implemented: nth-of-type.r�r�cSs|jS)N)r)rrrrr	r�szTag.select.<locals>.<lambda>r�cSs|jS)N)r)rrrrr	r�srcss|jd�VdS)NT)r~)rrrrr	�next_tag_sibling	sz$Tag.select.<locals>.next_tag_siblingz)Unsupported or invalid CSS selector: "%s"c3st�jr(td�|j|jf�tdd�x4|j���D]$}�jrTtd|j|jf�|Vq6W�jrptdd�dS)Nz-    Calling select("%s") recursively on %s %sr��(z,(Recursive select picked up candidate %s %s))�
_select_debug�printrrwr)rrr�)�
next_token�recursive_candidate_generatorrrr	�recursive_selects
z$Tag.select.<locals>.recursive_selectz[any]z-   Default candidate generator, tag name="%s"c3s8x2|jD](}t|t�sq�r*|j�kr*q|VqWdS)N)r�r.rkr)rrrd)r�rr	�default_candidate_generator7s
z/Tag.select.<locals>.default_candidate_generatorcSs|jS)N)r�)rrrrr	r�@srz(    Running candidate generator on %s %sz     SUCCESS %s %sz     FAILURE %s %szFinal verdict:z %s %srVrV)r�r7r]�addrrrfro�shlex�_selector_combinatorsr,r-r�r�r$�groupsr��quoted_colonr#r)�intr<�objectr(r�r�reprrwr.rkr�))rrZ_candidate_generatorr|�contextZ	selectorsZused_selectorsZselected_object_idsZpartial_selectorZ
candidatesr$Z	object_id�tokensZcurrent_contextr^�tokenZnew_contextZnew_context_idsZchecker�mr�r�r�r �klassr&ZpseudoZpseudo_attributesr�Zpseudo_typeZpseudo_valuer)r*r0Z_use_candidate_generatorZcheckr1r�rrr�r�r)r%r.r/rrr�r	r�s
























$

z
Tag.selectcCs|jS)N)r)rrrr	�childGeneratorkszTag.childGeneratorcCs|jS)N)r�)rrrr	�recursiveChildGeneratornszTag.recursiveChildGeneratorcCstjd|�|j|�S)z�This was kind of misleading because has_key() (attributes)
        was different from __in__ (contents). has_key() is gone in
        Python 3, anyway.z2has_key is deprecated. Use has_attr("%s") instead.)r�r�r�)rr�rrr	�has_keyqszTag.has_key)	NNNNNNNNN)F)N)N)r)NrI)NN)Drrrrr�rZparserClassr�rr�Z
isSelfClosingr�r
r/r�r�Zstringsr�r�ZgetTextrxr�r�r^rTr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�r r	rrr
rrr�Z	findChildr�ZfindAllZfindChildrenrr�r4r,r)r*r6rrr?r@rArrrr	rkes�
3





\
)	


lrkc@sTeZdZdZdidfdd�Zdd�Zdd�Zdifd	d
�ZeZdd�Z	dd
d�Z
dS)r�zMEncapsulates a number of ways of matching a markup element (tag or
    text).NcKs�|j|�|_t|t�s"||d<d}d|kr<|d|d<|d=|r\|rX|j�}|j|�n|}i}x&t|j��D]\}}|j|�||<qnW||_|j|�|_	dS)Nr"Zclass_)
�_normalize_search_valuerr.r��copy�updatermrrwrx)rrrwrxr>Znormalized_attrsr�r�rrr	r�~s"
zSoupStrainer.__init__cCs�t|t�s.t|�s.t|d�s.t|t�s.|dkr2|St|t�rF|jd�St|d�r�g}xJ|D]B}t|d�r�t|t�r�t|t�r�|j|�qZ|j|j|��qZW|Stt|��S)Nr$�utf8r�)	r.rrKrp�bool�bytesrrfrB)rr�Z	new_value�vrrr	rB�s



z$SoupStrainer._normalize_search_valuecCs |jr|jSd|j|jfSdS)Nz%s|%s)rxrrw)rrrr	r�szSoupStrainer.__str__c
Cs$d}d}t|t�r|}|}t|jt�o0t|t�}|jsd|sd|rP|j||j�sd|r�|j||j�r�|rv|j||�}nrd}d}xht|jj��D]V\}}	|s�t|d�r�|}ni}x|D]\}
}|||
<q�W|j	|�}|j||	�s�d}Pq�W|r�|r�|}n|}|�r |j
�r |j|j|j
��r d}|S)NTrTF)r.rkrr�_matchesrmrwrrprTrxr�)
rZmarkup_nameZmarkup_attrsr��markupZcall_function_with_tag_datar$Zmarkup_attr_mapr�
match_against�krHZ
attr_valuerrr	�
search_tag�sB


"zSoupStrainer.search_tagcCs�d}t|d�rHt|ttf�rHx�|D]}t|t�r$|j|�r$|}Pq$Wnpt|t�rr|jsf|jsf|jr�|j	|�}nFt|t�s�t|t�r�|jr�|jr�|j
||j�r�|}ntd|j��|S)Nr�z&I don't know how to match against a %s)
rpr.rkrr/r#rxrrwrMrI�	Exceptionr�)rrJr�r�rrr	r#�s"





zSoupStrainer.searchc	Cs�d}t|t�st|t�rPx|D]}|j||�rdSqW|jdj|�|�rLdSdS|dkr`|dk	St|t�rr||�S|}t|t�r�|j}|j|�}|dkr�|St	|d�o�t|t
��r|s�t�}xL|D]@}|jr�|}nt
|�}||kr�q�q�|j|�|j|||�r�dSq�WdSd}|�r.t|t
��r.||k}|�rLt	|d��rL|j|�S|�r�t|t��r�|j�r�|j|jd|j|�S|S)NFTr�r�r#r)r.rmr�rIr�rrkrrBrprr7r�rr2r#r)	rrJrKZ
already_triedr��itemZoriginal_markupr�r$rrr	rIsV







zSoupStrainer._matches)N)rrrrr�rBrrMZ	searchTagr#rIrrrr	r�zs'r�cs,eZdZdZff�fdd�	Zdd�Z�ZS)r�zTA ResultSet is just a list that keeps track of the SoupStrainer
    that created it.cstt|�j|�||_dS)N)�superr�r��source)rrQr�)r�rr	r�VszResultSet.__init__cCstd|��dS)Nz�ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?)r�)rr�rrr	r�ZszResultSet.__getattr__)rrrrr�r��
__classcell__rr)r�r	r�Ssr�),Z__license__�collections.abcr�ImportError�e�collectionsr)r3�sysr�Z
bs4.dammitrr��version_inforr*Z
whitespace_rerrrrrr!r,r8r:rArCrDrErFrGr/r�r�r�r�r�r�r�rkr�rmr�rrrr	�<module>s^
%
R9
Z
Page not found – Hello World !