【发布时间】:2017-05-26 16:45:30
【问题描述】:
我的代码产生了我想删除的额外表格。我想删除除此之外的所有其他表。
我的代码
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%' ], ['Premium Economy Class', 'W', '110%'], ['Economy Class', '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']] 桌子 [['距离以英里为单位:6,482','总计'],['预订子类:125%','精英奖金:75%','12,965'],['8,103','4,862']] 桌子 [['距离以英里为单位:6,482','总计'],['预订子类:50%','3,241'],['3,241']] 桌子 [['距离以英里为单位:6,482','总计'],['预订子类:50%','精英奖金:N / A','3,241'],['3,241','0']]
我想要的结果: 桌子 [['头等舱', 'F, U', '150%'], ['P', '125%'], ['商务舱', 'J, C, D, I', '125%' ], ['Premium Economy Class', 'W', '110%'], ['Economy Class', 'Y, B', '100%'], ['E, H, M', '75%' ], ['L, N, R, S, V, K', '50%'], ['T', '30%'], ['不符合应计条件', 'Z, Q, G', '0%']]
【问题讨论】:
标签: python web beautifulsoup screen-scraping