回答編集履歴

2

改行忘れてた

2019/10/11 16:34

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -44,6 +44,6 @@
44
44
 
45
45
  h4 = exa.find(re.compile("h[1-6]"), text=re.compile("経済関係$"))
46
46
 
47
- honbun1.append(''.join([m.string.strip() for m in elements_to_next_header(h4)]))
47
+ honbun1.append('\n'.join([m.string.strip() for m in elements_to_next_header(h4)]))
48
48
 
49
49
  ```

1

「次のヘッダまでの要素」というヘルパ関数を導入

2019/10/11 16:34

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -10,16 +10,40 @@
10
10
 
11
11
  ```python
12
12
 
13
+ h4 = exa.find("h4", text=re.compile("経済関係"))
14
+
13
15
  for m in h4.find_next_siblings("p", limit = 3):
14
16
 
15
17
  honbun1.append(m.string)
16
18
 
17
19
  ```
18
20
 
19
-
21
+ 日付単位で1回だけappendするように修正
20
22
 
21
23
  ```python
24
+
25
+ h4 = exa.find("h4", text=re.compile("経済関係"))
22
26
 
23
27
  honbun1.append(''.join([m.string for m in h4.find_next_siblings("p", limit = 3)]))
24
28
 
25
29
  ```
30
+
31
+ ↓ pかどうか曖昧だし3個かどうかも曖昧なので「次のヘッダまでの要素」というヘルパ関数を導入
32
+
33
+ ```python
34
+
35
+ def elements_to_next_header(start):
36
+
37
+ next = start.find_next_sibling()
38
+
39
+ while next and not re.match('h[1-6]', next.name):
40
+
41
+ yield next
42
+
43
+ next = next.find_next_sibling()
44
+
45
+ h4 = exa.find(re.compile("h[1-6]"), text=re.compile("経済関係$"))
46
+
47
+ honbun1.append(''.join([m.string.strip() for m in elements_to_next_header(h4)]))
48
+
49
+ ```