回答編集履歴

2

変数名間違い訂正

2019/10/23 11:02

投稿

barobaro
barobaro

スコア1286

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- f.seek(0) # 先頭位置へ移動
51
+ f1.seek(0) # 先頭位置へ移動
52
52
 
53
53
  f1.write(soup.prettify())
54
54
 

1

追記

2019/10/23 11:02

投稿

barobaro
barobaro

スコア1286

test CHANGED
@@ -3,3 +3,53 @@
3
3
 
4
4
 
5
5
  with open(file1, mode='w', encoding='utf-8') as f1:
6
+
7
+
8
+
9
+ # 追記
10
+
11
+
12
+
13
+ 読込と書き込みを別々に分ける
14
+
15
+
16
+
17
+ ```python
18
+
19
+ with open(file1, mode='r', encoding='utf-8') as f1:
20
+
21
+ soup = BeautifulSoup(f1.read(), 'html.parser')
22
+
23
+
24
+
25
+ soup.find(class_='section_fuga').decompose()
26
+
27
+
28
+
29
+ with open(file1, mode='w', encoding='utf-8') as f2:
30
+
31
+ f2.write(soup.prettify())
32
+
33
+ ```
34
+
35
+
36
+
37
+ r+で読み書きする
38
+
39
+
40
+
41
+ ```python
42
+
43
+ with open(file1, mode='r+', encoding='utf-8') as f1:
44
+
45
+ soup = BeautifulSoup(f1.read(), 'html.parser')
46
+
47
+ soup.find(class_='section_fuga').decompose()
48
+
49
+
50
+
51
+ f.seek(0) # 先頭位置へ移動
52
+
53
+ f1.write(soup.prettify())
54
+
55
+ ```