从零开始爬虫系统学习Day4——2021.2.27
回顾与思考
在开始今天的学习之前又重新温习了一遍之前的实例,尤其是BeautifulSoup那部分,感慨这些库的开发者们是真的厉害。但是转念一想,突然意识到,官方文档我竟然一眼都没看。于是赶紧去了BS4文档官网浏览了一遍,并且以后的学习记录里也会穿插着记录一些官方文档里独有的知识。这里把文档官网贴出来留个记录BeautifulSoup官方文档(支持多语)
另外,学到这里为了更深入理解,今天恶补了一下HTML的知识,也是这篇日记的主要记录内容
HTML相关知识补充
标签
浏览器通过分析标签,并按照不同的标签显示不同的内容,例如:
<html>
<body>
<h1>这是标题</h1>
<p>这是段落</p>
</body>
</html>
其中<html></html>
之间的内容是网页内容;<body></body>
中间为可显示内容。这两个例子中,前者称为开放标签,后者成为闭合标签。
HTML元素
HTML元素指的是从开始标签到结束标签的所有代码并且可以嵌套使用,包括开始标签、元素内容和闭合内容:
开放标签 | 元素内容 | 闭合标签 |
---|---|---|
<h1> | 这是标题 | </h1> |
<p> | 这是段落 | </p> |
<br /> |
其中比较特殊的一类是空元素标签,如上面表格中的<br />
,具有换行功能,需要在开始标签进行闭合,所以没有单独的闭合标签。
特别需要注意的是,符合规范的标签需要编写者始终如一地用小写字母(虽然部分网站虽然支持不区分大小写的标签)
属性
对于一个HTML元素,通过对其赋予属性,可以实现更多功能。属性写在开始标签的关键字后和转义符(/
)之间,不同的标签有各自不同的属性,并且对于一个HTML元素,可以有多个属性,下面以一个实例来说明格式规范(尤其注意引号和多属性规范):
<ins class="adsbygoogle" style="display:inline-block;width:800px;height:250px" data-ad-client="ca-pub-3381531532877742" data-ad-slot="9384069314"></ins>
BeautifulSoup文档记录
下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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>
"""
使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link2">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>
函数调用后
soup.title
# <title>The Dormouse's story</title>
soup.title.name
# u'title'
soup.title.string
# u'The Dormouse's story'
soup.title.parent.name
# u'head'
soup.p
# <p class="title"><b>The Dormouse's story</b></p>
soup.p['class']
# u'title'
soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
从文档中找到所有<a>
标签的链接:
for link in soup.find_all('a'):
print(link.get('href'))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie
从文档中获取所有文字内容:
print(soup.get_text())
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...