回答編集履歴

4

テキスト修正

2020/02/02 00:54

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -119,3 +119,93 @@
119
119
 
120
120
 
121
121
  - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_2](https://repl.it/@jun68ykt/Q2389662)
122
+
123
+
124
+
125
+ ### 追記2
126
+
127
+
128
+
129
+ htmlが以下のように、`<section>`を4つ含んでおり、3つ目の`<section>` にのみ、直接の子ノードに空白文字以外のテキストが含まれているとします。
130
+
131
+ ```html
132
+
133
+ <html><body>
134
+
135
+ <section class="content">
136
+
137
+ <div>テキスト</div>
138
+
139
+ <div>テキスト</div>
140
+
141
+ </section>
142
+
143
+ <section class="content">
144
+
145
+ <div>テキスト</div>
146
+
147
+ <div>テキスト</div>
148
+
149
+ </section>
150
+
151
+ <section class="content">
152
+
153
+ <div>テキスト</div>
154
+
155
+ hello
156
+
157
+ <div>テキスト</div>
158
+
159
+ </section>
160
+
161
+ <section class="content">
162
+
163
+ <div>テキスト</div>
164
+
165
+ <div>テキスト</div>
166
+
167
+ </section>
168
+
169
+ </body></html>
170
+
171
+ ```
172
+
173
+
174
+
175
+ 上記に対して、各`section`を上から順に調べていき、直接の子ノードとして空白文字以外の文字列が含まれている`section`がみつかったら、その文字列を表示して、ループを抜けるようなプログラムを書くとすると、以下のようになります。
176
+
177
+
178
+
179
+ ```python3
180
+
181
+ def get_first_text(elm):
182
+
183
+ for child in elm.contents:
184
+
185
+ if type(child) is NavigableString and str(child).strip():
186
+
187
+ return str(child).strip()
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+ soup = BeautifulSoup(html, "html.parser")
196
+
197
+
198
+
199
+ for sec in soup.find_all('section'):
200
+
201
+ text = get_first_text(sec)
202
+
203
+ if text:
204
+
205
+ print(text)
206
+
207
+ break
208
+
209
+ ```
210
+
211
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_3](https://repl.it/@jun68ykt/Q2389663)

3

テキスト修正

2020/02/02 00:54

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -69,3 +69,53 @@
69
69
  ```
70
70
 
71
71
  - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966](https://repl.it/@jun68ykt/Q238966)
72
+
73
+
74
+
75
+ ### 追記
76
+
77
+
78
+
79
+ もし、htmlが以下
80
+
81
+ ```html
82
+
83
+ <html><body>
84
+
85
+ <section class="content">
86
+
87
+ hello1
88
+
89
+ <div>テキスト</div>
90
+
91
+ hello2
92
+
93
+ <div>テキスト</div>
94
+
95
+ hello3
96
+
97
+ </section>
98
+
99
+ </body></html>
100
+
101
+ ```
102
+
103
+ のようなものであった場合は、
104
+
105
+ ```python3
106
+
107
+ text_children = [
108
+
109
+ str(e).strip() for e in soup.section.contents
110
+
111
+ if type(e) is NavigableString
112
+
113
+ ]
114
+
115
+ ```
116
+
117
+ によって `['hello1', 'hello2', 'hello3']` が得られます。
118
+
119
+
120
+
121
+ - **動作確認用Repl.it:** [https://repl.it/@jun68ykt/Q238966_2](https://repl.it/@jun68ykt/Q2389662)

2

テキスト修正

2020/02/01 23:06

投稿

jun68ykt
jun68ykt

スコア9058

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

1

テキスト修正

2020/02/01 22:30

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -4,9 +4,7 @@
4
4
 
5
5
  ```python3
6
6
 
7
- from bs4 import BeautifulSoup
7
+ from bs4 import BeautifulSoup, NavigableString
8
-
9
-
10
8
 
11
9
 
12
10
 
@@ -44,7 +42,7 @@
44
42
 
45
43
  for e in soup.section.contents:
46
44
 
47
- if e.__class__.__name__ == 'NavigableString' and str(e).strip():
45
+ if type(e) is NavigableString and str(e).strip():
48
46
 
49
47
  text = str(e).strip()
50
48
 
@@ -52,7 +50,9 @@
52
50
 
53
51
 
54
52
 
55
- print(text) # => hellohellohellohellohello
53
+ print(text) #=> hellohellohellohellohello
54
+
55
+
56
56
 
57
57
  ```
58
58