teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

prettifyに変更

2019/09/26 10:30

投稿

barobaro
barobaro

スコア1286

answer CHANGED
@@ -43,7 +43,7 @@
43
43
 
44
44
  # ファイル保存
45
45
  with open('test.html', mode = 'w', encoding = 'utf-8') as fw:
46
- fw.write(soup)
46
+ fw.write(soup.prettify())
47
47
  ```
48
48
  ```html
49
49
  <!-- 商品一覧 -->

1

ファイルの場合を追記

2019/09/26 10:30

投稿

barobaro
barobaro

スコア1286

answer CHANGED
@@ -27,8 +27,11 @@
27
27
 
28
28
  from bs4 import BeautifulSoup
29
29
 
30
+ # ファイルを開く
30
- soup = BeautifulSoup(html, "html.parser")
31
+ soup = BeautifulSoup(open('test.html', encoding='utf-8'), 'html.parser')
31
32
 
33
+ # soup = BeautifulSoup(html, "html.parser")
34
+
32
35
  for i in soup.select("p.b_price"):
33
36
  place = int(i.get_text(strip=True).rstrip("円").replace(",", ""))
34
37
  i.string = "{:,}円+税".format(round(place / 1.08))
@@ -37,6 +40,10 @@
37
40
 
38
41
  # 整形するならこちらで
39
42
  # print(soup.prettify())
43
+
44
+ # ファイル保存
45
+ with open('test.html', mode = 'w', encoding = 'utf-8') as fw:
46
+ fw.write(soup)
40
47
  ```
41
48
  ```html
42
49
  <!-- 商品一覧 -->