【发布时间】: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)
【问题讨论】:
-
好吧。数据不存储在表中。 It
s a bunch ofspan` 标签。我想这会对你有所帮助。
标签: python pandas beautifulsoup