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's story</title></head> <body> <p class="myclass" name="dromouse"><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" 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> """
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 content2. 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’) #后续兄弟选择器
soup.select('.myclass a[id="box"]')
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!