根据特定列 HTML Table 提取 Headers - Python
Extract HTML Table Based on Specific Column Headers - Python
我正在尝试从以下 URL 中提取 html table。
例如,第 44 页上的 2019 年董事薪酬 Table。我相信 table 没有特定的 ID,例如 'Compensation Table' 等。要提取table 我只能想到匹配列名或关键字,例如 "Stock Awards" 或 "All Other Compensation" 然后抓取关联的 table.
有没有一种简单的方法可以根据列名提取这些 table?或者更简单的方法?
谢谢!
我在抓取方面相对较新 HTML tables..我的代码如下
from bs4 import BeautifulSoup
import requests
url = 'https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.find_all('tr')
当然可以,根据 documentation.
使用 pandas
read_html
函数使用 match
和 attrs
import pandas as pd
df = pd.read_html(
"https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm", attrs={'style': 'border-collapse: collapse; width: 100%; font: 9pt Arial, Helvetica, Sans-Serif'}, match="Non-Employee Directors")
print(df)
df[0].to_csv("data.csv", index=False, header=False)
输出:View-Online
我正在尝试从以下 URL 中提取 html table。
例如,第 44 页上的 2019 年董事薪酬 Table。我相信 table 没有特定的 ID,例如 'Compensation Table' 等。要提取table 我只能想到匹配列名或关键字,例如 "Stock Awards" 或 "All Other Compensation" 然后抓取关联的 table.
有没有一种简单的方法可以根据列名提取这些 table?或者更简单的方法?
谢谢!
我在抓取方面相对较新 HTML tables..我的代码如下
from bs4 import BeautifulSoup
import requests
url = 'https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.find_all('tr')
当然可以,根据 documentation.
使用pandas
read_html
函数使用 match
和 attrs
import pandas as pd
df = pd.read_html(
"https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm", attrs={'style': 'border-collapse: collapse; width: 100%; font: 9pt Arial, Helvetica, Sans-Serif'}, match="Non-Employee Directors")
print(df)
df[0].to_csv("data.csv", index=False, header=False)
输出:View-Online