Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
CSS selectors: BeautifulSoup4
installation:pip install beautifulsoup4
Official documents: http: //beautifulsoup.readthedocs.io/zh_CN/v4.4.0
BeautifulSoup 用来解析 HTML 比较简单,API非常人性化,支持CSS选择器、
Python标准库中的HTML解析器,也支持 lxml 的 XML解析器。
Use the library must first be imported bs4
Parser
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" 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="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
#创建 Beautiful Soup 对象
soup = BeautifulSoup(html,'lxml')
#打开本地 HTML 文件的方式来创建对象
#soup = BeautifulSoup(open('index.html'))
#格式化输出 soup 对象的内容
print(soup.prettify())
Four types of objects
Beautiful Soup complex HTML documents converted into a complex tree structure, each node is Python objects, all objects can be grouped into four kinds:
《1》Tag:Tag 通俗点讲就是 HTML 中的一个个标签
《2》NavigableString:字符串常被包含在tag内.Beautiful Soup用NavigableString类来包装tag中的字符串:通过string来获得。
《3》BeautifulSoup:BeautifulSoup 对象表示的是一个文档的内容。大部分时候,可以把它当作 Tag 对象,
是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性
《4》Comment:Comment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号。
Selector
This is another One and find_all way to find a method the same purpose in. When writing CSS, the tag name without any modification, before class names., Before the id plus # we can also use a similar method to screen elements here, with the method is soup.select (), return type is List
. 1) by the tag name lookup
soup.select('title')
soup.select('b')
2) Find by class name
print soup.select('.sister')
3) Find the id name
print soup.select('#link1')
4) Find a combination
print soup.select('p #link1')
5) Find a property
print(soup.select('a[class="sister"]'))
soup.select('a[href="http://example.com/elsie"]')
- Access to content
soup = BeautifulSoup(html, 'lxml')
print (type(soup.select('title')))
print (soup.select('title')[0].get_text())
for title in soup.select('title'):
print (title.get_text())
print (title.attrs['class'])