| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
569 バイト追加 、 2020年2月15日 (土) 08:00
編集の要約なし
==Beautiful Soup (HTML XML解析)==
[[Python]]
*http://www.crummy.com/software/BeautifulSoup/
import BeautifulSoup # To get everything
<&lt;blockquote>&gt;No module named BeautifulSoup エラーとなる場合<&lt;/blockquote>&gt;
from bs4 import BeautifulSoup # To get everything
==解析==
*文字列およびファイルハンドルによる文書解析
soup = BeautifulSoup(open("index.html"))
soup = BeautifulSoup("<&lt;html>&gt;data<&lt;/html>&gt;")
*URLを指定して解析
*もっとも知られているのが、class
*Beautiful Soupでは、リストとして扱う
css_soup = BeautifulSoup('<&lt;p class="body strikeout"><&gt;&lt;/p>&gt;')
css_soup.p['class']
# ["body", "strikeout"]
*XMLでは、複数値として扱わない
xml_soup = BeautifulSoup('<&lt;p class="body strikeout"><&gt;&lt;/p>&gt;', 'xml')
xml_soup.p['class']
# u'body strikeout'
# u'Extremely bold'
type(tag.string)
# <&lt;class 'bs4.element.NavigableString'>&gt;
*unicode() で、Unicode 文字列に変換できる
unicode_string = unicode(tag.string)
# u'Extremely bold'
type(unicode_string)
# <&lt;type 'unicode'>&gt;
===BeautifulSoup===
*文書全体を表す
===コメント===
*Commentオブジェクトは、NavigableStringの特殊型
markup = "<&lt;b><&gt;&lt;!--Hey, buddy. Want to buy a used parser?--><&gt;&lt;/b>&gt;"
soup = BeautifulSoup(markup)
comment = soup.b.string
type(comment)
# <&lt;class 'bs4.element.Comment'>&gt;
==treeのナビゲート==
===下がる===
====.contents と .children====
head_tag.contents
[<&lt;title>&gt;The Dormouse's story<&lt;/title>&gt;]
for child in title_tag.children:
for child in head_tag.descendants:
print(child)
# <&lt;title>&gt;The Dormouse's story<&lt;/title>&gt;
# The Dormouse's story
====.string====
title_tag = soup.title
title_tag
# <&lt;title>&gt;The Dormouse's story<&lt;/title>&gt;
title_tag.parent
# <&lt;head><&gt;&lt;title>&gt;The Dormouse's story<&lt;/title><&gt;&lt;/head>&gt;
====.paretns====
link = soup.a
link
# <&lt;a class="sister" href="http://example.com/elsie" id="link1">&gt;Elsie<&lt;/a>&gt;
for parent in link.parents:
if parent is None:
====.next_sibling と .prebious_sibling====
sibling_soup.b.next_sibling
# <&lt;c>&gt;text2<&lt;/c>&gt;
sibling_soup.c.previous_sibling
# <&lt;b>&gt;text1<&lt;/b>&gt;
====.next_siblings と .prebious_siblings====
for sibling in soup.a.next_siblings:
*以下はほぼ同等
soup.find_all('title', limit=1)
# [<&lt;title>&gt;The Dormouse's story<&lt;/title>&gt;]
soup.find('title')
# <&lt;title>&gt;The Dormouse's story<&lt;/title>&gt;
=====CSSクラスで検索=====
soup.find("b", { "class" : "lime" })
# <&lt;b class="lime">&gt;Lime<&lt;/b>&gt;
====find_parents() と find_parent()====
====find_next_siblings() と find_next_sibling()====
paraText = soup.find(text='This is paragraph ')
paraText.findNextSiblings('b')
# [<&lt;b>&gt;one<&lt;/b>&gt;]
paraText.findNextSibling(text = lambda(text): len(text) == 1)
soup.select("body a")
====直下のタグ====
soup.select("head > &gt; title")
====CSS class====
soup.select(".sister")
tag.string = "New link text."
===append()===
soup = BeautifulSoup("<&lt;a>&gt;Foo<&lt;/a>&gt;")
soup.a.append("Bar")
===BeautifulSoup.new_string() と .new_tag()===
====new_string()====
soup = BeautifulSoup("<&lt;b><&gt;&lt;/b>&gt;")
tag = soup.b
tag.append("Hello")
tag.append(new_string)
tag
# <&lt;b>&gt;Hello there.<&lt;/b>&gt;
tag.contents
# [u'Hello', u' there']
====new_tag()====
soup = BeautifulSoup("<&lt;b><&gt;&lt;/b>&gt;")
original_tag = soup.b
original_tag.append(new_tag)
original_tag
# <&lt;b><&gt;&lt;a href="http://www.example.com"><&gt;&lt;/a><&gt;&lt;/b> &gt;
new_tag.string = "Link text."
original_tag
# <&lt;b><&gt;&lt;a href="http://www.example.com">&gt;Link text.<&lt;/a><&gt;&lt;/b>&gt;
===insert()===
markup = '<&lt;a href="http://example.com/">&gt;I linked to <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;'
soup = BeautifulSoup(markup)
tag = soup.a
tag.insert(1, "but did not endorse ")
tag
# <&lt;a href="http://example.com/">&gt;I linked to but did not endorse <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;
tag.contents
# [u'I linked to ', u'but did not endorse', <&lt;i>&gt;example.com<&lt;/i>&gt;]
===insert_before() と insert_after()===
===clear()===
markup = '<&lt;a href="http://example.com/">&gt;I linked to <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;'
soup = BeautifulSoup(markup)
tag = soup.a
tag.clear()
tag
# <&lt;a href="http://example.com/"><&gt;&lt;/a>&gt;
===extract()===
*タグもしくは文字列をツリーから削除
markup = '<&lt;a href="http://example.com/">&gt;I linked to <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;'
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag
# <&lt;a href="http://example.com/">&gt;I linked to<&lt;/a>&gt;
i_tag
# <&lt;i>&gt;example.com<&lt;/i>&gt;
===decompose()===
*タグをツリーから取り除く
markup = '<&lt;a href="http://example.com/">&gt;I linked to <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;'
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag
# <&lt;a href="http://example.com/">&gt;I linked to<&lt;/a>&gt;
===replace_with()===
*タグおよび文字列をツリーから取り除き、別のタグおよび文字列に置き換える
===wrap()===
*要素をタグでラップする
soup = BeautifulSoup("<&lt;p>&gt;I wish I was bold.<&lt;/p>&gt;")
soup.p.string.wrap(soup.new_tag("b"))
# <&lt;b>&gt;I wish I was bold.<&lt;/b>&gt;
===unwrap()===
*タグをはがす
markup = '<&lt;a href="http://example.com/">&gt;I linked to <&lt;i>&gt;example.com<&lt;/i><&gt;&lt;/a>&gt;'
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag.i.unwrap()
a_tag
# <&lt;a href="http://example.com/">&gt;I linked to example.com<&lt;/a>&gt;
==出力==
===Pretty-printing===

案内メニュー