Make Python more convenient-an introduction to Python's commonly used standard libraries

Tomorin
Release: 2018-08-15 17:31:47
Original
1742 people have browsed it

Which

Python commonly used libraries in Python will make programmers put it down? So much so that I became addicted. Today we will sort out such commonly used Python libraries. You are welcome to add or modify related library content in the comment area or private message me.

First introduce the beautifulsoup library, because when I first came into contact with crawler, I saw everyone strongly recommending this library. I used it later and found it really good. But programmers, how can they die in a library, haha.

The installation process of several libraries will not be described again. Please refer to anaconda. -_-.

The following examples are all tested using this.

html = """
<html><head><title>The Dormouse&#39;s story</title></head>
<body>
<p class="myclass" name="dromouse"><b>The Dormouse&#39;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" class="myclass" id="box"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="myclass" id="box1">Lacie</a> and
<a href="http://example.com/tillie" class="myclass" id="box2">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
Copy after login

BeautifulSoup

##Beautiful Soup supports the HTML parser in the Python standard library and also supports some third-party parsers. For example lxml HTML, lxml XML, html5lib. These libraries need to be installed though, otherwise it will use python's internal standard library.

There are two ways to create beautifulsoup objects.

1. Soup=BeautifulSoup(html) #Create with variable content

2. soup=BeautifulSoup('mysite.html') #Create with local file

BeautifulSoup converts HTML into a complex tree structure. Each node is a Python object. Students with front-end knowledge will know that it is similar to a DOM object. There are roughly four types of objects in BeautifulSoup, Tag, NavigableString, BeautifulSoup, and Comment. Since most of our daily operations are to extract information from each tag, I will briefly describe the commonly used Tag objects.

Tag

tag is a tag of HTML.

For example, head, title, a, p, etc. in HTML.

In actual operation, we will find the required tags through the selector, and then obtain the required information by operating the Tag object. In BeautifulSoup, findAll() and find() are commonly used to search the document tree to get the tags you need. At the same time, BeautifulSoup also supports CSS syntax for search, select() method, and the return type is list.

ps:

1.findAll() is equivalent to find_all()

2. If you know the front-end better, use the select() method to compare smoothly.

find()

find() is equivalent to limit=1 in findAll(), but find() returns the result, findAll() returns is a list.

CSS Selector

Beautifulsoup supports CSS syntax selectors to find the required tags.

select(CSS选择器)
例:
soup.select(&#39;.myclass #box&#39;)
#后代选择器
soup.select(&#39;head>title&#39;)
#子选择器
soup.select(&#39;div+p&#39;)
#相邻兄弟选择器
soup.select(&#39;div~p’)
#后续兄弟选择器
Copy after login

At the same time, you can also add attribute search.

soup.select(&#39;.myclass a[id="box"]&#39;)
Copy after login
The select() method returns a list.

The above are almost the commonly used functions of BeautifulSoup

The above is the detailed content of Make Python more convenient-an introduction to Python's commonly used standard libraries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template