Python爬虫系列(五):分析HTML结构

今晚,被烦死了。9点多才下班,就想回来看书学习,结果被唠叨唠叨个小时,我不断喊不要和我聊天了,还反复说。我只想安安静静看看书,学习学习,全世界都不要打扰我

接着上一个讨论,我们今晚要分析HTML结构了

1.获取元素

html_doc = """
<html>
<head>
<title>The Dormouse's story
</title>
</head>

<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>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
# 直接访问head元素
print(soup.head)
# 多个a元素只会返回第一个
print(soup.a)
# 通过层级获取 注意:上面没有body,soup会自动补全
print(soup.body.p)
# 获取所有a,返回a的数组
print(soup.find_all('a'))

2.获取内容和子元素

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
head_tag = soup.head
# 通过列表输出文档元素
print(head_tag.contents)
title_tag = head_tag.contents[1]
print('---------------------------')
print(title_tag)
print('---------------------------')
print(title_tag.contents)
print('---------------------------')
# BeautifulSoup 对象本身一定会包含子节点,也就是说<html>标签也是 BeautifulSoup 对象的子节点:
print(len(soup.contents))
print(soup.contents[0].name)
print('---------------------------')
# 通过tag的 .children 生成器,可以对tag的子节点进行循环
#title_tag.children是<title>The Dormouse's story</title>
for child in title_tag.children:
print(child)

# 字符串没有 .contents 属性,因为字符串没有子节点:
print('---------------------------')
text = title_tag.contents[0]
print(text.contents) #直接报错 :AttributeError: 'NavigableString' object has no attribute 'contents'

3.跨级获取子节点

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
head_tag = soup.head
# 递归获取所有子节点
for child in head_tag.descendants:
print('-----------------------')
print(child)

# 直接子节点
print(len(list(soup.children)))
# 跨级子节点:包括换行符 都算成了子节点。在使用的过程中,一定要注意判空
print(len(list(soup.descendants)))

4.string字符串

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
head_tag = soup.head
# title_tag.string是title标签的子节点。即便是纯粹的string 这个就是 NavigableString
# print(title_tag.string)
# 如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同:
print(head_tag.contents)
# 如果tag包含了多个子节点,tag就无法确定 .string 方法应该调用哪个子节点的内容, .string 的输出结果是 None :
print(soup.html.string)

5.遍历字符串和去除空白字符

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
# 如果tag中包含多个字符串 [2] ,可以使用 .strings 来循环获取:
# for string in soup.strings:
# print(repr(string))


# 输出的字符串中可能包含了很多空格或空行, 使用.stripped_strings
# 可以去除多余空白内容:
for string in soup.stripped_strings:
print(repr(string))

去除空白字符

6.父节点

每个节点都有自己的父节点。除了第一个

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "lxml")
title_tag = soup.title
print('1.-------------------')
print(title_tag)
print('2.-------------------')
print(title_tag.parent)
print('3.-------------------')
# 文档title的字符串也有父节点:<title>标签
print(title_tag.string.parent)
print('4.-------------------')
# 文档的顶层节点比如<html>的父节点是 BeautifulSoup 对象:
html_tag = soup.html
print(type(html_tag.parent))
print('5.-------------------')
# BeautifulSoup 对象的 .parent 是None:
print(soup.parent)

遍历所有父节点:

soup = BeautifulSoup(html_doc, "lxml")
link = soup.a
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)

7.兄弟节点

同一个级别的节点查找

sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>", "lxml")
# b后面一个兄弟节点
print(sibling_soup.b.next_sibling)
# c前面一个兄弟节点
print(sibling_soup.c.previous_sibling)
# 同一节点没有前一个
print(sibling_soup.b.previous_sibling)
# 同一节点没有最后一个
print(sibling_soup.c.next_sibling)

获取前后所有的兄弟节点

注意:这些节点包括字符串和符号

soup = BeautifulSoup(html_doc, "lxml")
for sibling in soup.a.next_siblings:
print('--------------------------')
print(repr(sibling))


for sibling in soup.find(id="link3").previous_siblings:
print('******************************')
print(repr(sibling))

8.回退和前进

注意看next_sibling和next_element的差别

soup = BeautifulSoup(html_doc, "lxml")
last_a_tag = soup.find("a", id="link3")
# 是指link3之后的内容
print(last_a_tag.next_sibling)
print('----------------------')
# 是指link3内部之后的内容
print(last_a_tag.next_element)
print(last_a_tag.next_element.next_element)

soup = BeautifulSoup(html_doc, "lxml")
last_a_tag = soup.find("a", id="link3")

#next_elements和previous_elements 会访问当前节点内部往后和外部往后的所有内容。内部往后优先
for element in last_a_tag.next_elements:
print(repr(element))

还有一节,培训新手的文档就结束了。真心希望同学们要好好学习,不然我白讲。 不下班回家陪我的小公举,却来义务教你写代码,而且是从零开始手把手教。兄弟们要加油啊

原文地址:https://www.cnblogs.com/zijiyanxi/p/7719735.html