zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
U
¡ý°dã@s dZdZddlZddlmZddlmZddlZddlmZm    Z    ddl
m Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZdd    „Zd#d d „ZGd d„deƒZdd„ZdZdZd$dd„Zd%dd„Zd&dd„Zd'dd„Zd(d d!„Zed"kreej  ¡ƒdS))z=Diagnostic functions, mainly for use when doing tech support.ÚMITéN)ÚBytesIO)Ú
HTMLParser)Ú BeautifulSoupÚ __version__)Úbuilder_registryc
CsÀtdtƒtdtjƒdddg}|D]4}tjD]}||jkr2q(q2| |¡td|ƒq(d|krÆ| d¡z*dd    l    m
}td
d   t t |jƒ¡ƒWn*tk
rÄ}z td ƒW5d }~XYnXd|krzdd l}td|jƒWn,tk
r}z tdƒW5d }~XYnXt|dƒr,| ¡}|D]Š}td|ƒd}zt||d}    d}Wn8tk
r}ztd|ƒt ¡W5d }~XYnX|r°td|ƒt|     ¡ƒtdƒq0d S)z¼Diagnostic suite for isolating common problems.
 
    :param data: A string containing markup that needs to be explained.
    :return: None; diagnostics are printed to standard output.
    z'Diagnostic running on Beautiful Soup %szPython version %sú html.parserÚhtml5libÚlxmlz;I noticed that %s is not installed. Installing it may help.zlxml-xmlr©ÚetreezFound lxml version %sÚ.z.lxml is not installed or couldn't be imported.NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.Úreadz#Trying to parse your markup with %sF)ÚfeaturesTú%s could not parse the markup.z#Here's what %s did with the markup:zP--------------------------------------------------------------------------------)ÚprintrÚsysÚversionrZbuildersrÚremoveÚappendr
r ÚjoinÚmapÚstrZ LXML_VERSIONÚ ImportErrorr    ÚhasattrrrÚ    ExceptionÚ    tracebackÚ    print_excZprettify)
ÚdataZ basic_parsersÚnameZbuilderr Úer    ÚparserÚsuccessÚsoup©r$úCd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\bs4/diagnose.pyÚdiagnosesZ 
 
 
 
ÿÿ
 ÿ
ÿ      r&TcKspddlm}| dd¡}t|tƒr,| d¡}t|ƒ}|j|f||dœ|—ŽD]\}}td||j    |j
fƒqLdS)    a´Print out the lxml events that occur during parsing.
 
    This lets you see how lxml parses a document when no Beautiful
    Soup code is running. You can use this to determine whether
    an lxml-specific problem is in Beautiful Soup's lxml tree builders
    or in lxml itself.
 
    :param data: Some markup.
    :param html: If True, markup will be parsed with lxml's HTML parser.
       if False, lxml's XML parser will be used.
    rr ÚrecoverTÚutf8)Úhtmlr'z %s, %4s, %sN) r
r ÚpopÚ
isinstancerÚencoderÚ    iterparserÚtagÚtext)rr)Úkwargsr r'ÚreaderÚeventÚelementr$r$r%Ú
lxml_traceNs  
 
ÿÿÿr4c@s`eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dd„Z
dd„Z dd„Z dd„Z dS)ÚAnnouncingParserzèSubclass of HTMLParser that announces parse events, without doing
    anything else.
 
    You can use this to get a picture of how html.parser sees a given
    document. The easiest way to do this is to call `htmlparser_trace`.
    cCs t|ƒdS)N)r)ÚselfÚsr$r$r%Ú_plszAnnouncingParser._pcCs| d|¡dS)Nz%s START©r8)r6rÚattrsr$r$r%Úhandle_starttagosz AnnouncingParser.handle_starttagcCs| d|¡dS)Nz%s ENDr9©r6rr$r$r%Ú handle_endtagrszAnnouncingParser.handle_endtagcCs| d|¡dS)Nz%s DATAr9©r6rr$r$r%Ú handle_datauszAnnouncingParser.handle_datacCs| d|¡dS)Nz
%s CHARREFr9r<r$r$r%Úhandle_charrefxszAnnouncingParser.handle_charrefcCs| d|¡dS)Nz %s ENTITYREFr9r<r$r$r%Úhandle_entityref{sz!AnnouncingParser.handle_entityrefcCs| d|¡dS)Nz
%s COMMENTr9r>r$r$r%Úhandle_comment~szAnnouncingParser.handle_commentcCs| d|¡dS)Nz%s DECLr9r>r$r$r%Ú handle_declszAnnouncingParser.handle_declcCs| d|¡dS)Nz%s UNKNOWN-DECLr9r>r$r$r%Ú unknown_decl„szAnnouncingParser.unknown_declcCs| d|¡dS)Nz%s PIr9r>r$r$r%Ú    handle_pi‡szAnnouncingParser.handle_piN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r8r;r=r?r@rArBrCrDrEr$r$r$r%r5dsr5cCstƒ}| |¡dS)zÂPrint out the HTMLParser events that occur during parsing.
 
    This lets you see how HTMLParser parses a document when no
    Beautiful Soup code is running.
 
    :param data: Some markup.
    N)r5Úfeed)rr!r$r$r%Úhtmlparser_traceŠsrKZaeiouZbcdfghjklmnpqrstvwxyzécCs:d}t|ƒD](}|ddkr"t}nt}|t |¡7}q |S)z#Generate a random word-like string.Úér)ÚrangeÚ _consonantsÚ_vowelsÚrandomÚchoice)Úlengthr7ÚiÚtr$r$r%Úrword˜s  rWécCsd dd„t|ƒDƒ¡S)z'Generate a random sentence-like string.ú css|]}tt dd¡ƒVqdS)rXé    N)rWrRÚrandint)Ú.0rUr$r$r%Ú    <genexpr>¥szrsentence.<locals>.<genexpr>)rrO)rTr$r$r%Ú    rsentence£sr^éècCs¤dddddddg}g}t|ƒD]r}t dd    ¡}|dkrPt |¡}| d
|¡q|d krp| tt d d ¡ƒ¡q|d krt |¡}| d|¡qdd |¡dS)z+Randomly generate an invalid HTML document.ÚpÚdivÚspanrUÚbÚscriptÚtableréz<%s>érXrNz</%s>z<html>Ú
z</html>)rOrRr[rSrr^r)Ú num_elementsZ    tag_namesÚelementsrUrSZtag_namer$r$r%Úrdoc§s  
 
rk順c
Cs$tdtƒt|ƒ}tdt|ƒƒdddgddfD]z}d}z"t ¡}t||ƒ}t ¡}d}Wn6tk
r”}ztd    |ƒt ¡W5d
}~XYnX|r4td |||fƒq4d d l    m
}t ¡}|  |¡t ¡}td||ƒd d
l }    |      ¡}t ¡}| |¡t ¡}td||ƒ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    rFTrNz"BS4+%s parsed the markup in %.2fs.rr z$Raw lxml parsed the markup in %.2fs.z(Raw html5lib parsed the markup in %.2fs.)rrrkÚlenÚtimerrrrr
r ZHTMLr    rÚparse)
rirr!r"Úar#rcr r r    r$r$r%Úbenchmark_parsers¹s4 
 
 
rqr
cCsXt ¡}|j}t|ƒ}tt||d}t d|||¡t     |¡}| 
d¡|  dd¡dS)z7Use Python's profiler on a randomly generated document.)Úbs4rr!zbs4.BeautifulSoup(data, parser)Z
cumulativez _html5lib|bs4é2N) ÚtempfileÚNamedTemporaryFilerrkÚdictrrÚcProfileZrunctxÚpstatsZStatsZ
sort_statsZ print_stats)rir!Z
filehandleÚfilenamerÚvarsÚstatsr$r$r%ÚprofileÙs
 
r|Ú__main__)T)rL)rX)r_)rl)rlr
)!rIÚ __license__rwÚiorÚ html.parserrrrrrZ bs4.builderrÚosrxrRrtrnrrr&r4r5rKrQrPrWr^rkrqr|rFÚstdinrr$r$r$r%Ú<module>s8   8
&