【问题标题】:convert website table to pandas df (beautifulsoup doesn't recognize table)将网站表格转换为 pandas df(beautifulsoup 无法识别表格)
【发布时间】:2021-05-23 12:04:28
【问题描述】:

我想将网站表格转换为 pandas df,但 BeautifulSoup 无法识别该表格(下面的截图)。下面是我尝试过的代码。

from bs4 import BeautifulSoup
import requests
import pandas as pd

url = 'https://www.ndbc.noaa.gov/ship_obs.php'
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.content, 'html.parser')
tables = soup.find_all('table', rules = 'all')
#tables =soup.find_all("table",{"style":"color:#333399;"}) #instead of above line to specify table with no luck!
df = pd.read_html(table, skiprows=2, flavor='bs4')
df.head()

我也尝试了下面的代码,但没有成功

df = pd.read_html('https://www.ndbc.noaa.gov/ship_obs.php')
print(df)

【问题讨论】:

  • 好吧。数据不存储在表中。 Its a bunch of span` 标签。我想这会对你有所帮助。

标签: python pandas beautifulsoup


【解决方案1】:

略有不同的方法,还要查看列数。我跳过了顶部的行,因此您必须构建列标题并清理最后一行。

import io
url = 'https://www.ndbc.noaa.gov/ship_obs.php'
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
tablecontent = soup.find('pre')
data = BeautifulSoup(tablecontent.text, "html.parser")
s = io.StringIO(data.text)
df = pd.read_csv(s, sep='\s+', engine='python', skiprows=3, header=None)

输出(对不起,从jupyter复制出来的对齐不好)

    0   1   2   3   4   5   6   7   8   9   ... 14  15  16  17  18  19  20  21  22  23
0   SHIP    19  47.4    -61.8   40  18.1    -   -   -   29.82   ... -   -   -   -   -   -   -   -   ----    -----
1   SHIP    19  47.7    -53.2   40  8.0 -   -   -   29.76   ... -   -   -   -   -   -   -   -   ----    -----
2   SHIP    19  47.8    -54.1   50  13.0    -   -   -   29.75   ... -   -   -   -   -   -   -   -   ----    -----
3   SHIP    19  48.2    -53.4   50  13.0    -   -   -   29.78   ... -   -   -   -   -   -   -   -   ----    -----
4   SHIP    19  46.8    -71.2   110 2.9 -   -   -   30.03   ... -   -   -   -   -   -   -   -   ----    -----
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
178 SHIP    19  25.8    -78.0   40  24.1    -   4.9 4.0 30.08   ... 11  5   -   -   -   -   -   -   ----    -----
179 SHIP    19  1.5 104.8   20  22.0    -   -   -   29.87   ... 11  5   -   -   -   -   -   -   ----    -----
180 SHIP    19  57.9    1.2 180 -   -   -   -   29.35   ... 5   -   -   -   -   -   -   -   ----    -----
181 SHIP    19  35.1    -10.0   310 24.1    -   6.6 6.0 29.68   ... 5   8   14.8    10.0    310 -   -   -   ----    -----
182 182 ship    observations    reported    for 1900    GMT None    None    None    ... None    None    None    None    None    None    None    None    None    None

【讨论】:

    【解决方案2】:

    您的表格不在<table> 标签中,而是在多个<span> 标签中。

    您可以像这样将它们解析为数据框:

    import pandas as pd
    import requests
    import bs4
    
    url = f"https://www.ndbc.noaa.gov/ship_obs.php"
    soup = bs4.BeautifulSoup(requests.get(url).text, 'html.parser').find('pre').find_all("span")
    print(pd.DataFrame([r.getText().split() for r in soup]))
    

    输出:

          0     1     2      3     4     5   ...    40    41    42    43    44    45
    0    SHIP  HOUR   LAT    LON  WDIR  WSPD  ...    °T    ft   sec    °T   Acc   Ice
    1    SHIP    19  46.5  -72.3   260   5.1  ...  None  None  None  None  None  None
    2    SHIP    19  46.8  -71.2   110   2.9  ...  None  None  None  None  None  None
    3    SHIP    19  47.4  -61.8    40  18.1  ...  None  None  None  None  None  None
    4    SHIP    19  47.7  -53.2    40   8.0  ...  None  None  None  None  None  None
    ..    ...   ...   ...    ...   ...   ...  ...   ...   ...   ...   ...   ...   ...
    170  SHIP    19  17.6  -62.4   100  20.0  ...  None  None  None  None  None  None
    171  SHIP    19  25.8  -78.0    40  24.1  ...  None  None  None  None  None  None
    172  SHIP    19   1.5  104.8    20  22.0  ...  None  None  None  None  None  None
    173  SHIP    19  57.9    1.2   180     -  ...  None  None  None  None  None  None
    174  SHIP    19  35.1  -10.0   310  24.1  ...  None  None  None  None  None  None
    
    [175 rows x 46 columns]
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 2018-08-27
      • 2019-07-22
      • 2021-11-30
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多