##Installation method one ①Enter the python file Folder execution instructions (provided that pip instructions are supported): The code is as follows:For this article about two ways to install Bs4 in Python, please refer to the following
pip3 install Beautifulsoup4Python installation Bs4 and how to use it ②Press Enter to complete the installation. If the following red appears The content in the box means that the installation is successful ③Verify whether it can be run successfully, run cmd to execute, reference the module import bs4 and press Enter without reporting an error, which proves that the installation is complete and can be used normally: Installation method two
python setup.py installPython installation Bs4 and how to use it ③After the operation is completed, enter python, and then enter help('modules') to view all the modules you currently have in python, as follows: ④Install as above Completed, also check whether bs4 can be imported normally, enter: import bs4 and press Enter Installation method three (If you are a python3 partner, you will find that the above two methods still do not work, run help(' modules') also cannot find the bs4 module, you need to use the following method at this time): ①After performing the second method above, copy the bs4 folder in the BeautifulSoup4 folder to the python installation directory ②Cut the Tools/scripts/2to3.py file in the python installation directory to the lib in the python installation directory ③Cd in cmd to the lib directory, and then Run python 2to3.py bs4 -w. Basic usage: The code is as follows:
import bs4 from bs4 import BeautifulSoup html_doc = """Python installation Bs4 and usage Create a BeautifulSoup The object code is as follows: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.
...
"""
soup = BeautifulSoup(html_doc,“html.parser”)Python installation Bs4 and usage method Formatted document output The code is as follows:
soup.prettify()Python Install Bs4 and how to use it Get the title The code is as follows:
soup.title.textInstalling Bs4 in Python and how to use it Get all tag attributes
soup.a.attrsPython installation Bs4 and usage method Determine whether it contains a certain tag attribute The code is as follows:
soup.a.has_attr(‘class')Python installation Bs4 and usage method Get The code of the sub-element of the tag is as follows:
list(soup.p.children)Python installation Bs4 and usage method The code is as follows:
list(soup.p.children)[0].textPython installation Bs4 and usage method Remove all tags The code is as follows:
soup.find_all(‘a') for a in soup.find_all(‘a'): print(a.attrs[‘href'])Python installation Bs4 and how to use it Find the specified id The code is as follows:
soup.find(id=‘link3')Python installation Bs4 and how to use it Find all text content The code is as follows:
soup.get_text()The content of the simple example used will be introduced here first Recommended learning:
The above is the detailed content of How many installation methods are there for python?. For more information, please follow other related articles on the PHP Chinese website!