Home >Common Problem >How many installation methods are there for python?
##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 EnterInstallation 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 = """<html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" rel="external nofollow" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" rel="external nofollow" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """Python installation Bs4 and usageCreate a BeautifulSoup The object code is as follows:
soup = BeautifulSoup(html_doc,“html.parser”)Python installation Bs4 and usage methodFormatted document outputThe code is as follows:
soup.prettify()Python Install Bs4 and how to use itGet the titleThe code is as follows:
soup.title.textInstalling Bs4 in Python and how to use itGet all tag attributes
soup.a.attrsPython installation Bs4 and usage methodDetermine whether it contains a certain tag attributeThe code is as follows:
soup.a.has_attr(‘class')Python installation Bs4 and usage methodGet The code of the sub-element of the tag is as follows:
list(soup.p.children)Python installation Bs4 and usage methodThe code is as follows:
list(soup.p.children)[0].textPython installation Bs4 and usage methodRemove all tagsThe 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 itFind the specified idThe code is as follows:
soup.find(id=‘link3')Python installation Bs4 and how to use itFind all text contentThe code is as follows:
soup.get_text()The content of the simple example used will be introduced here firstRecommended 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!