回答編集履歴

4

テキスト修正

2020/02/02 00:54

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -58,4 +58,49 @@
58
58
  ```
59
59
  によって `['hello1', 'hello2', 'hello3']` が得られます。
60
60
 
61
- - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_2](https://repl.it/@jun68ykt/Q2389662)
61
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_2](https://repl.it/@jun68ykt/Q2389662)
62
+
63
+ ### 追記2
64
+
65
+ htmlが以下のように、`<section>`を4つ含んでおり、3つ目の`<section>` にのみ、直接の子ノードに空白文字以外のテキストが含まれているとします。
66
+ ```html
67
+ <html><body>
68
+ <section class="content">
69
+ <div>テキスト</div>
70
+ <div>テキスト</div>
71
+ </section>
72
+ <section class="content">
73
+ <div>テキスト</div>
74
+ <div>テキスト</div>
75
+ </section>
76
+ <section class="content">
77
+ <div>テキスト</div>
78
+ hello
79
+ <div>テキスト</div>
80
+ </section>
81
+ <section class="content">
82
+ <div>テキスト</div>
83
+ <div>テキスト</div>
84
+ </section>
85
+ </body></html>
86
+ ```
87
+
88
+ 上記に対して、各`section`を上から順に調べていき、直接の子ノードとして空白文字以外の文字列が含まれている`section`がみつかったら、その文字列を表示して、ループを抜けるようなプログラムを書くとすると、以下のようになります。
89
+
90
+ ```python3
91
+ def get_first_text(elm):
92
+ for child in elm.contents:
93
+ if type(child) is NavigableString and str(child).strip():
94
+ return str(child).strip()
95
+
96
+
97
+
98
+ soup = BeautifulSoup(html, "html.parser")
99
+
100
+ for sec in soup.find_all('section'):
101
+ text = get_first_text(sec)
102
+ if text:
103
+ print(text)
104
+ break
105
+ ```
106
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_3](https://repl.it/@jun68ykt/Q2389663)

3

テキスト修正

2020/02/02 00:54

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -33,4 +33,29 @@
33
33
  print(text) #=> hellohellohellohellohello
34
34
 
35
35
  ```
36
- - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966](https://repl.it/@jun68ykt/Q238966)
36
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966](https://repl.it/@jun68ykt/Q238966)
37
+
38
+ ### 追記
39
+
40
+ もし、htmlが以下
41
+ ```html
42
+ <html><body>
43
+ <section class="content">
44
+ hello1
45
+ <div>テキスト</div>
46
+ hello2
47
+ <div>テキスト</div>
48
+ hello3
49
+ </section>
50
+ </body></html>
51
+ ```
52
+ のようなものであった場合は、
53
+ ```python3
54
+ text_children = [
55
+ str(e).strip() for e in soup.section.contents
56
+ if type(e) is NavigableString
57
+ ]
58
+ ```
59
+ によって `['hello1', 'hello2', 'hello3']` が得られます。
60
+
61
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_2](https://repl.it/@jun68ykt/Q2389662)

2

テキスト修正

2020/02/01 23:06

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,5 +1,11 @@
1
- こんにちは。以下で `hellohellohellohellohello` を得られると思います。
1
+ こんにちは。
2
2
 
3
+ ご質問の意図が、
4
+
5
+ - Beautifulsoupのみを使って子要素以下のテキストを除いたテキストを取得したい
6
+
7
+ ということだと、以下のようにすればよいかと思います。
8
+
3
9
  ```python3
4
10
  from bs4 import BeautifulSoup, NavigableString
5
11
 

1

テキスト修正

2020/02/01 22:30

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,9 +1,8 @@
1
1
  こんにちは。以下で `hellohellohellohellohello` を得られると思います。
2
2
 
3
3
  ```python3
4
- from bs4 import BeautifulSoup
4
+ from bs4 import BeautifulSoup, NavigableString
5
5
 
6
-
7
6
  html = '''
8
7
  <html><body>
9
8
  <section class="content">
@@ -21,10 +20,11 @@
21
20
  text = None
22
21
 
23
22
  for e in soup.section.contents:
24
- if e.__class__.__name__ == 'NavigableString' and str(e).strip():
23
+ if type(e) is NavigableString and str(e).strip():
25
24
  text = str(e).strip()
26
25
  break
27
26
 
28
- print(text) # => hellohellohellohellohello
27
+ print(text) #=> hellohellohellohellohello
28
+
29
29
  ```
30
30
  - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966](https://repl.it/@jun68ykt/Q238966)