【问题标题】:Remove extra table from web scraping results in python从python中的网络抓取结果中删除额外的表
【发布时间】: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


    【解决方案1】:

    尝试将[:1] 添加到soup.findAll('tbody') 它将限制结果仅限于第一个表。

    【讨论】:

      【解决方案2】:

      该页面以法语呈现,因此您想要的内容在我的浏览器中如下所示。

      检查 HTML 我看到几个表有相同的id,即inlineTable。要选择正确的,即使发布者改变了该表格在页面上的位置,也需要能够以其他方式对其进行识别。我注意到标题“Classe de Cabine”是该表独有的,它很可能在英文版中以“Cabin class”的形式提供。让我们使用它。

      首先,获取带有id 的所有表。查看每个表格的“Classe de Cabine”文本。当你发现它吐出一行时,除了标题行。

      >>> import requests
      >>> page = requests.get('https://www.flyingblue.com/earn-and-spend-miles/airlines/partner/180/china-eastern.html').text
      >>> from bs4 import BeautifulSoup
      >>> soup = BeautifulSoup(page, 'lxml')
      >>> required_tables = soup.select('#inlineTable')
      >>> len(required_tables)
      7
      >>> for table in required_tables:
      ...     if 'Classe de cabine' in table.text:
      ...         rows = table.findAll('tr')
      ...         for row in rows[1:]:
      ...             row
      ...             
      <tr class="table-highlite-light">
      <td rowspan="2" width="33%">Première Classe</td>
      <td width="33%">F, U</td>
      <td width="33%">150 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>P</td>
      <td>125 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>Classe Affaires</td>
      <td>J, C, D, I</td>
      <td>125 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>Premium Economy Classe</td>
      <td>W</td>
      <td>110 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td rowspan="4">Classe Économique</td>
      <td>Y, B</td>
      <td>100 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>E, H, M</td>
      <td>75 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>L, N, R, S, V, K</td>
      <td>50 %</td>
      </tr>
      <tr class="table-highlite-light">
      <td>T</td>
      <td>30%</td>
      </tr>
      <tr class="table-highlite-light">
      <td>Non éligible pour l’accumulation</td>
      <td>Z, Q, G</td>
      <td>0 %</td>
      </tr>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-29
        • 2022-01-21
        • 1970-01-01
        • 2019-09-12
        • 1970-01-01
        相关资源
        最近更新 更多