Need to extract all h2 text from some links and I tried it by using BeautifulSoup, but it didn't worked.
I also want to output them to CSV
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import csv
r01 = requests.get("https://www.seikatsu110.jp/library/vermin/vr_termite/23274/")
r02 = requests.get("https://yuko-navi.com/termite-control-subsidies")
soup_content01 = BeautifulSoup(r01.content, "html.parser")
soup_content02 = BeautifulSoup(r02.content, "html.parser")
alltxt01 = soup_content01.get_text()
alltxt02 = soup_content02.get_text()
with open('h2.csv', 'w+',newline='',encoding='utf-8') as f:
n = 0
for subheading01 in soup_content01.find_all('h2'):
sh01 = subheading01.get_text()
writer = csv.writer(f, lineterminator='\n')
writer.writerow([n, sh01])
n += 1
for subheading02 in soup_content02.find_all('h2'):
sh02 = subheading02.get_text()
writer = csv.writer(f, lineterminator='\n')
writer.writerow([n, sh01, sh02])
n += 1
pass
expected csv output is as below:
| sh01 | sh02 |
|---|---|
| シロアリ駆除に適用される補助金や保険は? | 1章 シロアリ駆除工事に補助金はない! |
| シロアリ駆除の費用を補助金なしで抑える方法 | 2章 確定申告時に「雑損控除」申請がおすすめ |
| シロアリ駆除の費用ってどれくらいかかる? | 3章 「雑損控除」として負担してもらえる金額 |
| 要件を満たせば加入できるシロアリ専門の保険がある? | 4章 「雑損控除」の申請方法 |
| シロアリには5年保証がある! | 5章 損したくないなら信頼できる業者を選ぼう! |
| まとめ | まとめ |
| この記事の監修者 ナカザワ氏について | |
| この記事の監修者 ナカザワ氏について | |
| シロアリ駆除のおすすめ記事 | |
| 関連記事カテゴリ一覧 | |
| シロアリ駆除の記事アクセスランキング | |
| シロアリ駆除の最新記事 | |
| カテゴリ別記事⼀覧 | |
| シロアリ駆除の業者を地域から探す | |
| 関連カテゴリから業者を探す | |
| シロアリ駆除業者ブログ | |
| サービスカテゴリ | |
| 生活110番とは | |
| 加盟希望・ログイン |
Please somebody tell me what's wrong with this code.