
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
#6]                 @   s  d Z dZddlZddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZ ddlZddlZddlZddlZddlZddlZddlZddlZdd	 Zd#ddZG dd deZdd ZdZdZd$ddZd%ddZd&ddZd'ddZd(d d!Zed"kreejj   dS ))z=Diagnostic functions, mainly for use when doing tech support.ZMIT    N)StringIO)
HTMLParser)BeautifulSoup__version__)builder_registryc          ;   C   sX  t dt  t dtj  dddg}x>|D ]6}x0tjD ]}||jkr6P q6W |j| t d|  q*W d|kr|jd y*dd	l	m
} t d
djtt|j  W n* tk
r } zt d W Y dd}~X nX d|kryddl}t d|j  W n, tk
r } zt d W Y dd}~X nX t| dr4| j } n| jdsL| jdrdt d|   t d dS y:tjj| rt d|   t| }|j } W dQ R X W n tk
r   Y nX t   x|D ]}t d|  d}	yt| |d}
d}	W n8 tk
r" } zt d|  tj  W Y dd}~X nX |	rBt d|  t |
j  t dd  qW dS )z/Diagnostic suite for isolating common problems.z'Diagnostic running on Beautiful Soup %szPython version %szhtml.parserhtml5liblxmlz;I noticed that %s is not installed. Installing it may help.zlxml-xmlr   )etreezFound lxml version %s.z.lxml is not installed or couldn't be imported.NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.readzhttp:zhttps:z<"%s" looks like a URL. Beautiful Soup is not an HTTP client.zpYou need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup.z7"%s" looks like a filename. Reading data from the file.z#Trying to parse your markup with %sF)featuresTz%s could not parse the markup.z#Here's what %s did with the markup:-P   )printr   sysversionr   Zbuildersr   removeappendr   r	   joinmapstrZLXML_VERSIONImportErrorr   hasattrr   
startswithospathexistsopen
ValueErrorr   	Exception	traceback	print_excZprettify)dataZbasic_parsersnameZbuilderr	   er   fpparsersuccesssoup r)   /usr/lib/python3.6/diagnose.pydiagnose   sj    








r+   Tc             K   sN   ddl m} x<|jt| fd|i|D ]\}}td||j|jf  q(W dS )zPrint out the lxml events that occur during parsing.

    This lets you see how lxml parses a document when no Beautiful
    Soup code is running.
    r   )r	   htmlz%s, %4s, %sN)r   r	   Z	iterparser   r   tagtext)r"   r,   kwargsr	   Zeventelementr)   r)   r*   
lxml_traceZ   s    $r1   c               @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )AnnouncingParserz?Announces HTMLParser parse events, without doing anything else.c             C   s   t | d S )N)r   )selfsr)   r)   r*   _pg   s    zAnnouncingParser._pc             C   s   | j d|  d S )Nz%s START)r5   )r3   r#   Zattrsr)   r)   r*   handle_starttagj   s    z AnnouncingParser.handle_starttagc             C   s   | j d|  d S )Nz%s END)r5   )r3   r#   r)   r)   r*   handle_endtagm   s    zAnnouncingParser.handle_endtagc             C   s   | j d|  d S )Nz%s DATA)r5   )r3   r"   r)   r)   r*   handle_datap   s    zAnnouncingParser.handle_datac             C   s   | j d|  d S )Nz
%s CHARREF)r5   )r3   r#   r)   r)   r*   handle_charrefs   s    zAnnouncingParser.handle_charrefc             C   s   | j d|  d S )Nz%s ENTITYREF)r5   )r3   r#   r)   r)   r*   handle_entityrefv   s    z!AnnouncingParser.handle_entityrefc             C   s   | j d|  d S )Nz
%s COMMENT)r5   )r3   r"   r)   r)   r*   handle_commenty   s    zAnnouncingParser.handle_commentc             C   s   | j d|  d S )Nz%s DECL)r5   )r3   r"   r)   r)   r*   handle_decl|   s    zAnnouncingParser.handle_declc             C   s   | j d|  d S )Nz%s UNKNOWN-DECL)r5   )r3   r"   r)   r)   r*   unknown_decl   s    zAnnouncingParser.unknown_declc             C   s   | j d|  d S )Nz%s PI)r5   )r3   r"   r)   r)   r*   	handle_pi   s    zAnnouncingParser.handle_piN)__name__
__module____qualname____doc__r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r)   r)   r)   r*   r2   d   s   r2   c             C   s   t  }|j|  dS )zPrint out the HTMLParser events that occur during parsing.

    This lets you see how HTMLParser parses a document when no
    Beautiful Soup code is running.
    N)r2   Zfeed)r"   r&   r)   r)   r*   htmlparser_trace   s    rC   ZaeiouZbcdfghjklmnpqrstvwxyz   c             C   s>   d}x4t | D ](}|d dkr$t}nt}|tj|7 }qW |S )z#Generate a random word-like string.    r   )range_consonants_vowelsrandomchoice)lengthr4   itr)   r)   r*   rword   s    rO      c             C   s   dj dd t| D S )z'Generate a random sentence-like string. c             s   s   | ]}t tjd dV  qdS )rP   	   N)rO   rJ   randint).0rM   r)   r)   r*   	<genexpr>   s    zrsentence.<locals>.<genexpr>)r   rG   )rL   r)   r)   r*   	rsentence   s    rV     c             C   s   dddddddg}g }x~t | D ]r}tjdd	}|dkrRtj|}|jd
|  q |dkrr|jttjdd q |dkr tj|}|jd|  q W ddj| d S )z+Randomly generate an invalid HTML document.pZdivspanrM   bZscripttabler      z<%s>   rP   rF   z</%s>z<html>
z</html>)rG   rJ   rS   rK   r   rV   r   )num_elementsZ	tag_nameselementsrM   rK   Ztag_namer)   r)   r*   rdoc   s    

ra   順 c       
      C   s(  t dt  t| }t dt|  xdddgddgD ]z}d}y"tj }t||}tj }d}W n6 tk
r } zt d	|  tj  W Y d
d
}~X nX |r6t d||| f  q6W ddl	m
} tj }|j| tj }t d||   dd
l}	|	j }tj }|j| tj }t d||   d
S )z.Very basic head-to-head performance benchmark.z1Comparative parser benchmark on Beautiful Soup %sz3Generated a large invalid HTML document (%d bytes).r   r,   r   zhtml.parserFTz%s could not parse the markup.Nz"BS4+%s parsed the markup in %.2fs.r   )r	   z$Raw lxml parsed the markup in %.2fs.z(Raw html5lib parsed the markup in %.2fs.)r   r   ra   lentimer   r   r    r!   r   r	   ZHTMLr   r   parse)
r_   r"   r&   r'   ar(   rZ   r$   r	   r   r)   r)   r*   benchmark_parsers   s4    


rg   r   c             C   sX   t j }|j}t| }tt||d}tjd||| tj	|}|j
d |jdd d S )N)bs4r"   r&   zbs4.BeautifulSoup(data, parser)Z
cumulativez_html5lib|bs42   )tempfileZNamedTemporaryFiler#   ra   dictrh   cProfileZrunctxpstatsZStatsZ
sort_statsZprint_stats)r_   r&   Z
filehandlefilenamer"   varsZstatsr)   r)   r*   profile   s    

rp   __main__)T)rD   )rP   )rW   )rb   )rb   r   )!rB   Z__license__rl   ior   Zhtml.parserr   rh   r   r   Zbs4.builderr   r   rm   rJ   rj   rd   r    r   r+   r1   r2   rC   rI   rH   rO   rV   ra   rg   rp   r?   stdinr   r)   r)   r)   r*   <module>   s8   C

!	



 

