For this article about two ways to install Bs4 in Python, please refer to the following
##Installation method one
①Enter the python file Folder execution instructions (provided that pip instructions are supported):
The code is as follows:
pip3 install Beautifulsoup4
Copy after login
Python 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
(With various network restrictions like our company, if you use pip, you will be unable to install and keep looping in retry):
①Enter the official website to download the compressed package: Beautiful Soup official website download link
②Extract the compressed package into a python file, enter the decompressed file and enter the command (the previous python is indispensable):
The code is as follows:
python setup.py install
Copy after login
Python 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 = """<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>
"""
Copy after login
Python installation Bs4 and usage
Create a BeautifulSoup The object
code is as follows:
soup = BeautifulSoup(html_doc,“html.parser”)
Copy after login
Python installation Bs4 and usage method
Formatted document output
The code is as follows:
soup.prettify()
Copy after login
Python Install Bs4 and how to use it
Get the title
The code is as follows:
soup.title.text
Copy after login
Installing Bs4 in Python and how to use it
Get all tag attributes
soup.a.attrs
Copy after login
Python installation Bs4 and usage method
Determine whether it contains a certain tag attribute
The code is as follows:
soup.a.has_attr(‘class')
Copy after login
Python installation Bs4 and usage method
Get The code of the sub-element
of the tag is as follows:
list(soup.p.children)
Copy after login
Python installation Bs4 and usage method
The code is as follows:
list(soup.p.children)[0].text
Copy after login
Python 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'])
Copy after login
Python installation Bs4 and how to use it
Find the specified id
The code is as follows:
soup.find(id=‘link3')
Copy after login
Python installation Bs4 and how to use it
Find all text content
The code is as follows:
soup.get_text()
Copy after login
The content of the simple example used will be introduced here first
Recommended learning:
php video tutorial
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!