Python:BS4で特定のクラス内のあるクラスを取り出す2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
beautifulsoup4の動作テスト | |
''' | |
from bs4 import BeautifulSoup | |
# HTMLデータを取得する | |
html = """ | |
<ol> | |
<li class="best1"><td class><p class="waku">グリム童話</p><p class="waku">怖い話が多い</p></td><tr> | |
<li class="best2"><td class><p class="waku">ギリシャ神話</p><p class="waku">神々の名前が漫画のキャラに使われていたりする</p></td><tr> | |
</ol> | |
""" | |
# HTMLをBeautifulSoupに渡して解析 | |
soup = BeautifulSoup(html, 'html.parser') | |
# h2タグのwakuクラスの部分のみ取り出したい | |
str = soup.find("li", class_="best2") | |
strs = str.find_all("p", class_="waku") | |
# 画面に表示 | |
print(strs[0].text) | |
print(strs[1].text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment