Home>Article> How many installation methods are there for python?

How many installation methods are there for python?

奋力向前
奋力向前 forward
2021-07-06 12:27:19 2713browse

For this article about two ways to install Bs4 in Python, please refer to the following

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:

pip3 install Beautifulsoup4

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

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 = """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.

...

"""

Python installation Bs4 and usage

Create a BeautifulSoup The object

code is as follows:

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.text

Installing Bs4 in Python and how to use it

Get all tag attributes

soup.a.attrs

Python 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].text

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'])

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:

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!

Statement:
This article is reproduced at:512pic.com. If there is any infringement, please contact admin@php.cn delete