今日、私は次の爬虫類のPythonライブラリBeautifulSoupは、操作機能の詳細な方法でドキュメントツリーとタグをトラバース紹介します
たとえば次のPythonライブラリBeautifulSoupの使用はドキュメントツリーを横断し、ラベル操作された爬虫類され、それは最も基本的な内容です
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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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')
まず、子ノード
タグは、多くの運用と越え属性の子ノードを提供.BeautifulSoup子ノードがあるタグを文字列や他のタグの複数が含まれていてもよいです。
1.タグタグ名によって得られました
print(soup.head)
print(soup.title)
<head><title>The Dormouse's story</title></head>
<title>The Dormouse's story</title>
あなたは、いくつかのメソッドのfind_allを使用することができますタグのすべてを取得したい場合は、最初のタグを取得するための名前で唯一の方法
soup.find_all('a')
[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
2.contents属性:タグは、子ノードのリストを経由して返します。
head_tag = soup.head
head_tag.contents
[<title>The Dormouse's story</title>]
title_tag = head_tag.contents[0]
title_tag
<title>The Dormouse's story</title>
title_tag.contents
["The Dormouse's story"]
3.children:属性の子ノードを循環
for child in title_tag.children:
print(child)
The Dormouse's story
4.descendants:子孫は、ノードのすべての子のためのループタグを再帰ながら内容や子供のどちらかは、子ノードに直接返されます
for child in head_tag.children:
print(child)
```bash
for child in head_tag.descendants:
print(child)
<title>The Dormouse's story</title>
The Dormouse's story
タイプの子ノードをNavigableString 5.string唯一つのタグは、タグは.stringサブノードを与えるために使用することができる場合
title_tag.string
"The Dormouse's story"
タグが唯一の子ノードを持っている場合は、使用が彼らの唯一の子供がNavigableStringノード取得.string。
head_tag.string
head_tag.string
複数のサブノードタグの場合、タグは、子ノードに対応する.stringコンテンツは、それがどれ返さないことを決定することができません
print(soup.html.string
)
None
6.strings和stripped_strings
タグは、複数のストリングを含む場合、.strings取得サイクルを使用することができます
for string in soup.strings:
print(string)
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.
...
コンテンツ.string出力は、多くのスペースと空白行が含まれている、strpped_stringsを使用して空白のコンテンツを削除します
for string in soup.stripped_strings:
print(string)
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.
...
第二に、親ノード
1.parent:要素の親ノードを取得します
title_tag = soup.title
title_tag.parent
<head><title>The Dormouse's story</title></head>
文字列は、親ノードを持っています
title_tag.string.parent
<title>The Dormouse's story</title>
2.parents:再帰的にすべての父親ノードを取得
link = soup.a
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)
p
body
html
[document]
第三に、兄弟
sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>",'lxml')
print(sibling_soup.prettify())
<html>
<body>
<a>
<b>
text1
</b>
<c>
text2
</c>
</a>
</body>
</html>
1.next_sibling和previous_sibling
sibling_soup.b.next_sibling
<c>text2</c>
sibling_soup.c.previous_sibling
<b>text1</b>
実際の文書であり、通常の文字列または空白を.next_sibling previous_sibling
soup.find_all('a')
[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
soup.a.next_sibling # 第一个<a></a>的next_sibling是,\n
```bash
'\ n' は
```bash
soup.a.next_sibling.next_sibling
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
2.next_siblings和previous_siblings
for sibling in soup.a.next_siblings:
print(repr(sibling))
',\n'
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
' and\n'
<a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>
';\nand they lived at the bottom of a well.'
for sibling in soup.find(id="link3").previous_siblings:
print(repr(sibling))
' and\n'
<a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
',\n'
<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>
'Once upon a time there were three little sisters; and their names were\n'
第四に、ロールバックと前進
1.next_element和previous_element
次または前のオブジェクトを指すが、解析された(または文字列タグ)、すなわち、ノードの深さ優先トラバーサル順序と前のノード
last_a_tag = soup.find("a", id="link3")
print(last_a_tag.next_sibling)
print(last_a_tag.next_element)
;
and they lived at the bottom of a well.
Tillie
last_a_tag.previous_element
' and\n'
2.next_elements和previous_elements
文書は次のように解析されている場合.next_elements及びprevious_elements前方または後方にアクセスすることにより、文書の内容を解析します
for element in last_a_tag.next_elements:
print(repr(element))
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
<p class="story">...</p>
'...'
'\n'
私たちは、Pythonの学習サイトをお勧めします、入力する]をクリックし、プログラムを学ぶことがいかに古い見て!基本的なPythonスクリプト、爬虫類、ジャンゴ、データマイニング、技術をプログラミング、仕事の経験だけでなく、小型のpythonパートナーのシニア入念な研究から戦闘にゼロベースの情報のプロジェクトを仕上げ!時限プログラマPythonは日常の技術を説明している方法は、学習と細部への注意を払う必要性へのいくつかを共有します