【问题标题】:python beautiful soup table?python 漂亮的汤桌?
【发布时间】:2020-08-27 02:58:33
【问题描述】:

请问我如何在表格中捕捉这些值。我需要日期、时间、储备和播放值。 每次我只得到整个表的完整列表时,我不知道如何捕获其中的给定值 非常感谢您的帮助。

 <table class="list">
    <tr class="head">
        <th>Date</th>
        <th>Time</th>
        <th>Play</th>
        <th>Tickets</th>
        <th>&nbsp;</th>
    </tr>
    

        
        

            <tr class="t1">
                <th>Th
                    03. 09. 2020</th>
                <td>
                    19:00</td>
                <td>Racek</td>
                <td class="center">4</td>
                <td>
                    
                    
                        
                            <a href="/rezervace/detail?id=2618"
                               title="Reserve tickets for this performance">
                                reserve
                            </a>
                        
                        
                    

                </td>
            </tr>

【问题讨论】:

标签: python-3.x beautifulsoup html-table


【解决方案1】:

首先,您应该发布一些您自己尝试过的代码。但无论如何,这里有另一种方式。

from simplified_scrapy import SimplifiedDoc,req
html = '''
<table class="list">
     <tr class="head">
          <th>Date</th>
          <th>Time</th>
          <th>Play</th>
          <th>Tickets</th>
          <th>&nbsp;</th>
     </tr>
     <tr class="t1">
          <th>Th
          03. 09. 2020</th>
          <td>
          19:00</td>
          <td>Racek</td>
          <td class="center">4</td>
          <td>
               <a href="/rezervace/detail?id=2618"
                    title="Reserve tickets for this performance">
                    reserve
               </a>
          </td>
     </tr>
</table>
'''
doc = SimplifiedDoc(html)
# First method
table = doc.getTable('table')
print (table)

# Second method
table = doc.getElement('table', attr='class', value='list').trs.children.text
print (table)

结果:

[['Date', 'Time', 'Play', 'Tickets', ''], ['Th 03. 09. 2020', '19:00', 'Racek', '4', 'reserve']]
[['Date', 'Time', 'Play', 'Tickets', ''], ['Th 03. 09. 2020', '19:00', 'Racek', '4', 'reserve']]

这里有更多示例:https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

【讨论】:

    【解决方案2】:

    此脚本将使用BeautifulSoup 解析表格,然后将各个行打印到屏幕:

    import re
    from bs4 import BeautifulSoup
    
    html = '''
    <table class="list">
         <tr class="head">
              <th>Date</th>
              <th>Time</th>
              <th>Play</th>
              <th>Tickets</th>
              <th>&nbsp;</th>
         </tr>
         <tr class="t1">
              <th>Th
              03. 09. 2020</th>
              <td>
              19:00</td>
              <td>Racek</td>
              <td class="center">4</td>
              <td>
                   <a href="/rezervace/detail?id=2618"
                        title="Reserve tickets for this performance">
                        reserve
                   </a>
              </td>
         </tr>
    </table>
    '''
    
    soup = BeautifulSoup(html, 'html.parser')
    
    all_data = []
    for row in soup.select('tr'):
        all_data.append([re.sub(r'\s{2,}', ' ', d.get_text(strip=True)) for d in row.select('td, th')])
    
    # print data to screen:
    
    # print header:
    print('{:<25}{:<15}{:<15}{:<15}{:<15}'.format(*all_data[0]))
    
    # print rows:
    for date, time, play, tickets, reserve in all_data[1:]:
        print('{:<25}{:<15}{:<15}{:<15}{:<15}'.format(date, time, play, tickets, reserve))
    

    打印:

    Date                     Time           Play           Tickets                       
    Th 03. 09. 2020          19:00          Racek          4              reserve        
    

    【讨论】:

      【解决方案3】:

      使用 pandas 的简单方法

      import pandas as pd
      
      table = """
      <table class="list">
          <tr class="head">
              <th>Date</th>
              <th>Time</th>
              <th>Play</th>
              <th>Tickets</th>
              <th>&nbsp;</th>
          </tr>
          <tr class="t1">
              <th>Th
                  03. 09. 2020</th>
              <td>
                  19:00</td>
              <td>Racek</td>
              <td class="center">4</td>
              <td>
                  <a href="/rezervace/detail?id=2618" title="Reserve tickets for this performance">
                      reserve
                  </a>
              </td>
          </tr>
      </table>
      """"
      
      df = pd.read_html(table)[0]
      

      然后就可以访问“df”内的数据了

      df["Date"]
      # 0    Th  03. 09. 2020
      # Name: Date, dtype: object
      df["Time"]
      # 0    19:00
      # Name: Time, dtype: object
      df["Play"]
      # 0    Racek
      # Name: Play, dtype: object
      df["Tickets"]
      # 0    4
      

      【讨论】:

        猜你喜欢
        • 2017-12-23
        • 2018-01-13
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 2013-03-21
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多