回答編集履歴

2

Update

2022/07/21 08:54

投稿

melian
melian

スコア19825

test CHANGED
@@ -16,7 +16,9 @@
16
16
 
17
17
  ## 必要な情報を、タグごと取得
18
18
  contents = soup.find('div', class_='main_text')
19
+ contents = ''.join(
19
- contents = ''.join(str(i) if i.name in ('br', 'ruby') else i.text.strip() for i in contents)
20
+ str(i) if i.name in ('br', 'ruby') else i.text.strip().replace('\r', '')
21
+ for i in contents)
20
22
  return title, author, contents
21
23
 
22
24
  if __name__ == '__main__':

1

Update

2022/07/21 08:49

投稿

melian
melian

スコア19825

test CHANGED
@@ -11,8 +11,8 @@
11
11
  soup = BeautifulSoup(html_text.content, 'html.parser')
12
12
 
13
13
  # タイトルと著者名を取得する
14
- title = soup.find('h1')
14
+ title = soup.find('h1').text
15
- author = soup.find('h2')
15
+ author = soup.find('h2').text
16
16
 
17
17
  ## 必要な情報を、タグごと取得
18
18
  contents = soup.find('div', class_='main_text')
@@ -21,8 +21,8 @@
21
21
 
22
22
  if __name__ == '__main__':
23
23
  title, author, contents = get('https://www.aozora.gr.jp/cards/000329/files/18376_12100.html')
24
+ with open(f'青空文庫_{title}_{author}.txt', 'w') as f:
24
- print(title)
25
+ f.write(f'{title}\n')
25
- print(author)
26
+ f.write(f'{author}\n\n')
26
- print()
27
- print(contents)
27
+ f.write(contents)
28
28
  ```