This article mainly introduces the relevant information about Python using the Beautiful Soup module to create objects. The introduction in the article is very detailed. I believe it has certain reference value for everyone. Friends who need it can take a look below.
Installation
Install the Beautiful Soup module via pip: pip install beautifulsoup4
.
You can also use PyCharm IDE to write code. Find Project in Preferences in PyCharm, search for the Beautiful Soup module in it, and install it.
Create a BeautifulSoup object
The Beautiful Soup module is widely used to get data from web pages. We can use the Beautiful Soup module to extract any data from an HTML/XML document, for example, all links in a web page or content within tags.
To achieve this, Beautiful Soup provides different objects and methods. Any HTML/XML document can be converted into different Beautiful Soup objects. These objects have different properties and methods, and we can extract the required data from them.
Beautiful Soup has a total of three objects:
BeautifulSoup
Tag
NavigableString
Create a BeautifulSoup object
Creating a BeautifulSoup object is the starting point for any Beautiful Soup project.
BeautifulSoup can pass a string or file-like object, such as a file or web page on the machine.
Creating BeautifulSoup objects from strings
Create objects by passing a string in the constructor of BeautifulSoup.
helloworld = '<p>Hello World</p>' soup_string = BeautifulSoup(helloworld) print soup_string <html><body><p>Hello World</p></body></html>
Creating BeautifulSoup objects through file-like objects
Create objects by passing a file-like object in the constructor of BeautifulSoup. This is useful when parsing online web pages.
url = "http://www.glumes.com" page = urllib2.urlopen(url) soup = BeautifulSoup(page) print soup
In addition to passing file-like objects, we can also pass local file objects to the constructor of BeautifulSoup to generate objects.
with open('foo.html','r') as foo_file : soup_foo = BeautifulSoup(foo_file) print soup_foo
Creating BeautifulSoup objects for XML parsing
The Beautiful Soup module can also be used to parse XML.
When creating a BeautifulSoup object, the Beautiful Soup module will select the appropriate TreeBuilder class to create the HTML/XML tree. By default, the HTML TreeBuilder object is selected, which will use the default HTML parser to produce an HTML structure tree. In the above code, the BeautifulSoup object is generated from the string by parsing it into an HTML tree structure.
If we want the Beautiful Soup module to parse the input content into XML type, then we need to accurately specify the features parameter used in the Beautiful Soup constructor. By specifying the features parameter, Beautiful Soup will select the most suitable TreeBuilder class to meet the features we want.
Understanding features parameters
Each TreeBuilder will have different features depending on the parser it uses. Therefore, the input content will have different results depending on the features parameter passed to the constructor.
In the Beautiful Soup module, the parser currently used by TreeBuilder is as follows:
lxml
html5lib
html.parser
The features parameter of the BeautifulSoup constructor can accept a string list or a string value.
Currently, the features parameters and parsers supported by each TreeBuilder are as shown in the following table: