私のコードは、削除したい余分なテーブルを生成します。私はこれ以外のすべてのテーブルを削除したいと思います。PythonのWebスクレイピング結果から余分なテーブルを削除
マイコード
import csv
from bs4 import BeautifulSoup
import requests
import pandas as pd
import telnetlib as tn
import os
#import sys
cwd = os.getcwd()
print (os.getcwd)
cwd = os.getcwd()
os.chdir('c:\\Users\STaiwo\Desktop\My R code')
page = requests.get("https://www.flyingblue.com/earn-and-spend-
miles/airlines/partner/180/china-eastern.html", verify = False)
print(page.content) ### Collects HTML content of site
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify()) ## Cleans up the content of the site
for table in soup.findAll('tbody'):
print('Table')
list_of_rows = []
for row in table.findAll('tr')[1:]:
list_of_cells = []
for cell in row.findAll('td'):
text = ((cell.text.replace(' ', '')))
list_of_cells.append(text)
list_of_rows.append(list_of_cells)
print(list_of_rows)
結果私は現在取得しています: 表 [[ 'ファーストクラス'、 'F、U'、 '150%']、[ 'P 125%]、[ビジネスクラス]、[J、C、D、I、125%]、[プレミアムエコノミークラス]、[W]、[110%]、['エコノミークラス、Y、B、100%]、[E、H、M、75%]、[L、N、R、S、V、K、50%] ['T'、'30% ']、['発生対象外 '、' Z、Q、G '、' 0% ']] 表 [] 表 [] テーブル [マイルの距離:6,482 '、'合計 ']、['予約サブクラス:125% '、' 8,103 ']、[' 8,103 '] テーブル [[' 「合計」、「サブクラス125%」、「エリートボーナス75%」、「12,965」、「8,103」、「4,862」] 表 [['マイル:6,482 '、'合計 ']、[予約サブカテゴリ:50%]、[3,241]]、[' 3,241 '] テーブル [[マイルの距離:6,482'、 '合計]]、 [ '予約サブクラス:50%'、 'エリートボーナス:N/A'、 '3241']、[ '3241'、 '0']
欲しい結果: 表 [ ['ファーストクラス'、 'F、U'、 '150%']、['P'、 '125%]'、 'バス「プレミアムエコノミークラス」、「W」、「110%」、「エコノミークラス」、「Y」、「B」、「C」、 100%]、['E、H、M'、 '75%']、['L、N、R、S、V、K'、 '50%']、['T'、 '30% ]、['発生対象外'、 'Z、Q、G'、 '0%']]