【问题标题】:How to extract only header names from table into a list如何仅将表中的标题名称提取到列表中
【发布时间】:2021-11-13 12:52:55
【问题描述】:

我正在尝试仅将 Wikipedia 表中的标题值提取到列表中。以下代码是我目前所拥有的,但我无法正确获得输出。

import requests
from bs4 import BeautifulSoup


page = requests.get('https://en.wikipedia.org/wiki/List_of_chemical_elements')
soup = BeautifulSoup(page.text, "html.parser")
table = soup.find('table')



column_names = [item.get_text() for item in table.find_all('th')]   
column_names[2:18]

# current output: ['Origin of name[2][3]\n', 'Group\n','Period\n', 'Block\n' ...]    

# expected outout ['Atomic Number', 'Symbol', 'Name', 'Origin of name', 
#                'Group', 'Period', 'Standard atomic weight', 'Density', 
#                 'Melting Point'...]

【问题讨论】:

    标签: python pandas beautifulsoup python-requests


    【解决方案1】:

    我认为您需要根据 html 的结构进行一些数据清理。该表具有多索引结构,因此您不会将平面列表作为列。请记住,pandas 有 from_html() 函数,它允许您传递原始 html 字符串并为您进行解析,无需使用 BeautifulSoup 或进行任何 html 解析。

    务实地思考我相信对于这种特殊情况,最好手动进行,否则您将需要进行大量字符串操作才能获得一个干净的列名列表。手动写比较快。

    鉴于您已经完成了大部分写作,我建议您使用更简单、更省时的解决方案:

    df = pd.read_html(page.text)[0]
    column_names = ['Atomic Number', 'Symbol', 'Name', 'Origin of name', 'Group', 'Period','Block','Standard atomic weight', 'Density', 'Melting Point','Boiling Point','Specific heat capacity','Electro-negativity',"Abundance in Earth's crust",'Origin','Phase at r.t.']
    df.columns = column_names
    

    输出漂亮且可读的:

         Atomic Number Symbol  ...      Origin  Phase at r.t.
    0                1      H  ...  primordial            gas
    1                2     He  ...  primordial            gas
    2                3     Li  ...  primordial          solid
    3                4     Be  ...  primordial          solid
    4                5      B  ...  primordial          solid
    ..             ...    ...  ...         ...            ...
    113            114     Fl  ...   synthetic  unknown phase
    114            115     Mc  ...   synthetic  unknown phase
    115            116     Lv  ...   synthetic  unknown phase
    116            117     Ts  ...   synthetic  unknown phase
    117            118     Og  ...   synthetic  unknown phase
    

    否则,如果您想采用全自动方法:

    page = requests.get('https://en.wikipedia.org/wiki/List_of_chemical_elements')
    df = pd.read_html(page.text)[0]
    df.columns = df.columns.droplevel()
    

    输出:

    Element Origin of name[2][3]    Group   Period  Block   Standardatomicweight[a] Density[b][c]   Melting point[d]    Boiling point[e]    Specificheatcapacity[f] Electro­negativity[g]   Abundancein Earth'scrust[h] Origin[i]   Phase at r.t.[j]
    Atomic number.mw-parser-output .nobold{font-weight:normal}Z Symbol  Name    Unnamed: 3_level_2  Unnamed: 4_level_2  Unnamed: 5_level_2  Unnamed: 6_level_2  (Da)    ('"`UNIQ--templatestyles-00000016-QINU`"'g/cm3) (K) (K) (J/g · K)   Unnamed: 12_level_2 (mg/kg) Unnamed: 14_level_2 Unnamed: 15_level_2
    0   1   H   Hydrogen    Greek elements hydro- and -gen, 'water-forming' 1.0 1   s-block 1.008   0.00008988  14.01   20.28   14.304  2.20    1400    primordial  gas
    1   2   He  Helium  Greek hḗlios, 'sun' 18.0    1   s-block 4.0026  0.0001785   –[k]    4.22    5.193   –   0.008   primordial  gas
    2   3   Li  Lithium Greek líthos, 'stone'   1.0 2   s-block 6.94    0.534   453.69  1560    3.582   0.98    20  primordial  solid
    3   4   Be  Beryllium   Beryl, a mineral (ultimately from the name of ...   2.0 2   s-block 9.0122  1.85    1560    2742    1.825   1.57    2.8 primordial  solid
    4   5   B   Boron   Borax, a mineral (from Arabic bawraq)   13.0    2   p-block 10.81   2.34    2349    4200    1.026   2.04    10  primordial  solid
    ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
    113 114 Fl  Flerovium   Flerov Laboratory of Nuclear Reactions, part o...   14.0    7   p-block [289]   (9.928) (200)[b]    (380)   –   –   –   synthetic   unknown phase
    114 115 Mc  Moscovium   Moscow, Russia, where the element was first sy...   15.0    7   p-block [290]   (13.5)  (700)   (1400)  –   –   –   synthetic   unknown phase
    115 116 Lv  Livermorium Lawrence Livermore National Laboratory in Live...   16.0    7   p-block [293]   (12.9)  (700)   (1100)  –   –   –   synthetic   unknown phase
    116 117 Ts  Tennessine  Tennessee, United States, where Oak Ridge Nati...   17.0    7   p-block [294]   (7.2)   (700)   (883)   –   –   –   synthetic   unknown phase
    117 118 Og  Oganesson   Yuri Oganessian, Russian physicist  18.0    7   p-block [294]   (7) (325)   (450)   –   –   –   synthetic   unknown phase
    

    为了让它看起来漂亮和整洁所需的字符串清理将比编写几个列名花费更长的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多