eaiovnaovbqoebvqoeavibavo 3 (6]XY@sdZdZdZdZdZdgZddlZddlZddlZddl Z ddl Z dd l m Z m Z dd lmZdd lmZmZmZmZmZmZmZmZmZmZmZd d kGdddeZeZeZGdddeZGddde Z!Gddde"Z#e$dkr ddlZeej%Z&e'e&j(dS)aHBeautiful 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)z4.6.3z*Copyright (c) 2004-2018 Leonard RichardsonZMIT BeautifulSoupN)builder_registryParserRejectedMarkup) UnicodeDammit) CDataCommentDEFAULT_OUTPUT_ENCODING DeclarationDoctypeNavigableString PageElementProcessingInstruction ResultSet SoupStrainerTagz`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`).cseZdZdZdZddgZdZdZd2d d Zd d Z d dZ e ddZ ddZ ddZddifddZefddZddZddZddZdd Zefd!d"Zd3d#d$Zd4d&d'Zd(d)Zd5d*d+Zd,d-Zd.ed/ffd0d1 ZZS)6ra 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
tag), call handle_starttag and then handle_endtag. z [document]ZhtmlZfastz aNo 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" sdkrtjddkr*d=tjddkrBd=tjddkrZd=tjdd krrd =tjd fd d }|p|d d}|p|dd}|rt|trtjdd}tdkrtjj} td| |dkr|} t|tr|g}|dkst|dkr|j }t j |} | dkr@t ddj || }| |jkpZ| |jks|jrld} nd} d} ytjd} Wntk rYnX| r| j}| j}n tj}d}|jd}|r|j}|jd(r|dd)}|rt|||j| d}tj|j|dd||_|j|_|j|_||j_||_t |d rN|j!}nt|d!krt|t"rrd"|kst|trd#|krt|trt#j$j% r|j&d$}n|}d%}yt#j$j'|}Wn$t(k r}zWYdd}~XnX|rt|tr|j&d$}tjd&||j)|xZ|jj*|||d'D]D\|_+|_,|_-|_.|j/y|j0PWnt1k rrYnXq2Wd|_+d|j_dS)*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. ZconvertEntitieszBS4 does not respect the convertEntities argument to the BeautifulSoup constructor. Entities are always converted to Unicode characters.Z markupMassagezBS4 does not respect the markupMassage argument to the BeautifulSoup constructor. The tree builder is responsible for any necessary markup massage.Z smartQuotesTozBS4 does not respect the smartQuotesTo argument to the BeautifulSoup constructor. Smart quotes are always converted to Unicode characters.ZselfClosingTagszBS4 does not respect the selfClosingTags argument to the BeautifulSoup constructor. The tree builder is responsible for understanding self-closing tags.ZisHTMLzBS4 does not respect the isHTML argument to the BeautifulSoup constructor. Suggest you use features='lxml' for HTML and features='lxml-xml' for XML.cs0|kr,tjd||f|}|=|SdS)NzLThe "%s" argument to the BeautifulSoup constructor has been renamed to "%s.")warningswarn)Zold_namenew_namevalue)kwargs/usr/lib/python3.6/__init__.pydeprecated_arguments z3BeautifulSoup.__init__..deprecated_argumentZparseOnlyThese parse_onlyZ fromEncoding from_encodingzlYou provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.Nrz2__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?,ZXMLZHTMLr__file__.pyc.pyo)filename line_numberparser markup_type) stacklevelread<<utf8Fzw"%s" looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.)exclude_encodings)rr )2rr isinstancestrlenlistkeyspop TypeErrorDEFAULT_BUILDER_FEATURESrlookupFeatureNotFoundjoinNAMEZALTERNATE_NAMESis_xmlsys _getframe ValueError f_globalsf_lineno__dict__getlowerendswithdictNO_PARSER_SPECIFIED_WARNINGbuilderZ known_xmlsouprhasattrr'bytesospathsupports_unicode_filenamesencodeexists Exception_check_markup_is_urlZprepare_markupmarkuporiginal_encodingZdeclared_html_encodingZcontains_replacement_charactersreset_feedr)selfrQfeaturesrFrrr,rrargZoriginal_featuresZ builder_classr$Zcallerglobalsr"r!ZfnlvaluesZpossible_filenameis_fileer)rr__init__Xs'                       zBeautifulSoup.__init__cCs&t||jd|jdd}|j|_|S)Nzutf-8)rFr)typerMrFrR)rUcopyrrr__copy__$szBeautifulSoup.__copy__cCs(t|j}d|kr$|jj r$d|d<|S)NrF)rDr@rFZ picklable)rUdrrr __getstate__0s zBeautifulSoup.__getstate__csxttrd}d }nttr(d}d}ndStfdd |Drt|krtttrbjd d }n}tjd |dS)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).  http:https: http:https:Nc3s|]}j|VqdS)N) startswith).0prefix)rQrr Fsz5BeautifulSoup._check_markup_is_url..zutf-8replacez"%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.)rcrd)rfrg)r.rIr/anydecoderr)rQZspaceZcant_start_withZdecoded_markupr)rQrrP7s   z"BeautifulSoup._check_markup_is_urlcCs@|jj|jj|j|jx|jj|jkr:|jq"WdS)N) rFrSZfeedrQendData currentTagname ROOT_TAG_NAMEpopTag)rUrrrrTSs  zBeautifulSoup._feedcCsJtj|||j|jd|_|jjg|_d|_g|_g|_ |j |dS)Nr) rr\rFrrZhiddenrS current_datarptagStackpreserve_whitespace_tag_stackpushTag)rUrrrrS]s zBeautifulSoup.resetcKs|j|td|j||||S)z+Create a new tag associated with this soup.N)updaterrF)rUrq namespacensprefixattrsZkwattrsrrrnew_taggs zBeautifulSoup.new_tagcCs||S)z7Create a new NavigableString associated with this soup.r)rUssubclassrrr new_stringlszBeautifulSoup.new_stringcCs tddS)Nz4BeautifulSoup objects don't support insert_before().)NotImplementedError)rU successorrrr insert_beforepszBeautifulSoup.insert_beforecCs tddS)Nz3BeautifulSoup objects don't support insert_after().)r)rUrrrr insert_aftersszBeautifulSoup.insert_aftercCs@|jj}|jr(||jdkr(|jj|jr:|jd|_|jS)Nrr-r-)rur3rvrp)rUtagrrrrsvs    zBeautifulSoup.popTagcCsJ|jr|jjj||jj||jd|_|j|jjkrF|jj|dS)Nrr-)rpcontentsappendrurqrFZpreserve_whitespace_tagsrv)rUrrrrrws   zBeautifulSoup.pushTagcCs|jrdj|j}|jsPd}x|D]}||jkr"d}Pq"W|rPd|krLd}nd}g|_|jrt|jdkr|jj s|jj| rdS||}|j |dS)NrTF rer) rtr8rv ASCII_SPACESrr0rutextsearchobject_was_parsed)rUZcontainerClassrtZ strippableiorrrros&    zBeautifulSoup.endDatac CsV|p|j}|p|j}d}}}t|trF|j}|j}|j}|sF|j}|j|||||||_|j j ||jrRt |j d}x4|dkr|j ||krP|d8}qWt d||f|dkr|}d}n|j |d}}|t |j dkr|j}d}n|j |d}}||_|r||_||_|r.||_||_|r@||_||_|rR||_dS)z Add an object to the parse tree.Nrrz[Error building tree: supposedly %r was inserted into %r after the fact, but I don't see it!) rp_most_recent_elementr.r next_element next_siblingprevious_siblingprevious_elementZsetuprrr0r=) rUrparentZmost_recent_elementrrrrindexrrrrsR        zBeautifulSoup.object_was_parsedTcCsn||jkrdSd}t|j}xLt|dddD]8}|j|}||jkr^||jkr^|r\|j}P|j}q.W|S)zPops 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.Nrrr-)rrr0rurangerqrjrs)rUrqrzZ inclusivePopZmost_recently_popped stack_sizertrrr _popToTags    zBeautifulSoup._popToTagc Cs|j|jr8t|jdkr8|jjs4|jj|| r8dSt||j|||||j|j }|dkr`|S|j rn||j _ ||_ |j ||S)aPush 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. rN) rorr0rurZ search_tagrrFrprrrw)rUrqryrzr{rrrrhandle_starttags   zBeautifulSoup.handle_starttagcCs|j|j||dS)N)ror)rUrqrzrrr handle_endtagszBeautifulSoup.handle_endtagcCs|jj|dS)N)rtr)rUdatarrr handle_dataszBeautifulSoup.handle_dataFZminimalcsN|jr$d}|dkrd|}d|}nd}|s2d}nd}|tt|j|||S)zlReturns a string or Unicode representation of this document. To get Unicode, pass None for encoding.rNz encoding="%s"z r)r:superrrn)rUZ pretty_printZeventual_encodingZ formatterZ encoding_partrjZ indent_level) __class__rrrns  zBeautifulSoup.decode)rNNNNN)NN)NT)N)__name__ __module__ __qualname____doc__rrr5rrEr\r_ra staticmethodrPrTrSr|r rrrrsrwrorrrrrr rn __classcell__rr)rrr8s8 L        9  cs eZdZdZfddZZS)BeautifulStoneSoupz&Deprecated interface to an XML parser.cs(d|d<tjdtt|j||dS)NZxmlrVzxThe BeautifulStoneSoup class is deprecated. Instead of using it, pass features="xml" into the BeautifulSoup constructor.)rrrrr\)rUargsr)rrrr\5szBeautifulStoneSoup.__init__)rrrrr\rrr)rrr2src@s eZdZdS) StopParsingN)rrrrrrrr=src@s eZdZdS)r7N)rrrrrrrr7@sr7__main__))r __author__ __version__Z __copyright__Z __license____all__rJrer; tracebackrrFrrZdammitrelementrrr r r r r rrrrrZ_sZ_souprrOrr=r7rstdinrGprintZprettifyrrrrs6 4z