Home>Article>Backend Development> Make Python more convenient-an introduction to Python’s commonly used standard libraries
Which
Python commonly used librariesinPythonwill make programmers put it down? So much so that I became addicted. Today we will sort out suchcommonly used Python libraries. You are welcome to add or modify related library content in the comment area or private message me.
First introduce thebeautifulsouplibrary, because when I first came into contact withcrawler, 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 = """The Dormouse's story The Dormouse's story
Once upon a time there were three little sisters; and their names were , Lacie and Tillie; and they lived at the bottom of a well.
...
"""
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('.myclass #box') #后代选择器 soup.select('head>title') #子选择器 soup.select('div+p') #相邻兄弟选择器 soup.select('div~p’) #后续兄弟选择器At the same time, you can also add attribute search.
soup.select('.myclass a[id="box"]')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!