回答編集履歴

1

コード追記

2020/07/08 05:55

投稿

can110
can110

スコア38266

test CHANGED
@@ -3,3 +3,49 @@
3
3
  不要なタグは除いてテキストだけを取得できます。
4
4
 
5
5
  違いについては[BeautifulSoupでstringとtextの挙動の明確な違い – Python](https://lets-hack.tech/programming/languages/python/bs4-text-or-string/)などを参照ください。
6
+
7
+ ```Python
8
+
9
+ import bs4
10
+
11
+
12
+
13
+ html = """
14
+
15
+ <div id="recipe-main">
16
+
17
+ <div class="items_row">
18
+
19
+ <div class="target_item"><span class="name">ロケット</span></div>
20
+
21
+ </div>
22
+
23
+ <div class="items_row">
24
+
25
+ <div class="target_item"><span class="name"><a class="image_link" href="/test.example">飛行機</a>
26
+
27
+ </span></div>
28
+
29
+ </div>
30
+
31
+ """
32
+
33
+
34
+
35
+ ps = bs4.BeautifulSoup(html,'html.parser')
36
+
37
+ item_all = ps.find("div", attrs={"id": "recipe-main"})
38
+
39
+
40
+
41
+ for item in item_all.findAll("div", attrs={"class": "target_item"}):
42
+
43
+ print(item.text)
44
+
45
+
46
+
47
+ #ロケット
48
+
49
+ #飛行機
50
+
51
+ ```