"Introduction"
Beautiful Soup is a Python library that can extract data from an HTML or XML file. That is, the HTML/XMLX parser.
It can handle non-canonical tags well and generate a parse tree. It provides simple and common navigation (navigating), search and modify the parse tree operation. It can greatly save your programming time."Install"
: Click to open link
Linux Platform Installation:
If you are using a new version of Debain or Ubuntu, you can install it through the system's package management:
$ apt-get install PYTHON-BS4
Beautiful Soup 4 is released through PyPI, so if you cannot manage the installation using the System package, you can also install it by Easy_install or pip . The name of the package is Beautifulsoup4 , this package is compatible with Python2 and Python3.
$ easy_install beautifulsoup4
$ pip install beautifulsoup4
(There's a beautifulsoup in PyPI, but that's probably not what you want, that's the release of Beautiful Soup3, because many projects are still using BS3, so The BeautifulSoup package is still valid.
But if you are writing a new project, then you should install the Beautifulsoup4 )
If you do not have easy_install or pip installed , you can also download the BS4 source code and install it via setup.py.
$ Python setup.py Install
If none of the above installation methods work, Beautiful Soup's release protocol allows you to package BS4 code in your project so that you don't need to install it.
Authors develop beautiful Soup in versions Python2.7 and Python3.2, theoretically beautiful Soup should work correctly in all current Python versions
Windows platform:
After the download is complete, it needs to be decompressed, assuming it is placed under d:/it/python27. Run cmd and switch to the D:/IT/PYTHON27/BEAUTIFULSOUP4-4.3.2/directory (modified according to the directory you unzipped and the version number you downloaded). Run command: Python setup.py build and python setup.py install
Ready to install < If your Python path is added to the environment variable >
"Case"
The following section of HTML code will be used as an example. This is Alice in Wonderland a piece of content (a document that is referred to as Alice in later content ):
Html_doc = "" "
using BeautifulSoup to parse this code, you can get aBeautifulSoupobject, and can be output in the form of a standard indented format:
From BS4 Import beautifulsoupsoup = BeautifulSoup (html_doc) print (Soup.prettify ()) #
A few simple ways to browse structured data:
soup.title# <title>the dormouse ' s story</title>soup.title.name# u ' title ' soup.title.string# U ' the Dormouse ' s story ' soup.title.parent.name# U ' head ' soup.p# <p class= ' title ' ><b>the dormouse ' s story</b ></p>soup.p[' class ']# u ' title ' soup.a# <a class= ' sister ' href= ' Http://example.com/elsie ' id= ' Link1 ' > Elsie</a>soup.find_all (' a ') # [<a class= "sister" href= "Http://example.com/elsie" id= "Link1" >elsie</a >,# <a class= "sister" href= "Http://example.com/lacie" id= "Link2" >lacie</a>,# <a class= " Sister "href=" Http://example.com/tillie "id=" Link3 ">tillie</a>]soup.find (id=" Link3 ") # <a class=" Sister "Href=" Http://example.com/tillie "id=" Link3 ">Tillie</a>
Find links to all <a> tags from the documentation:
For link in Soup.find_all (' a '): print (link.get (' href ')) # http://example.com/elsie# http://example.com/lacie# Http://example.com/tillie
get all the text from the document:
Print (Soup.get_text ()) # The Dormouse ' s story## the dormouse ' s story## Once upon a time there were three Little sisters; And their names were# elsie,# Lacie and# tillie;# and they lived at the bottom of a well.## ...
"How to use"Link Address: Click to open the link
[Python] Html/xml parser Beautiful Soup