从具有不同格式的不同源中提取具有相似数据的HTML表-Python
我试图从两个不同的HTML源中提取HTML表。两者非常相似,每个表包含相同的数据,但它们的结构可能不同,列名称也不同。对于一个源,所有数据可能包含在一个表中,而另一个源可能将数据分解为两个单独的表 例如,我们可以看看AAPL和MMM股票的内幕持有人 这里的截图- 假设最终目标是提取内部人士持有的股份总数——一个单数。每个表的结构可能不同,但应该相似的是关键词,如实益或股票 任何帮助都将不胜感激。在之前的一篇文章中,我能够提取一些数据。但如果结构不同,就不能循环或重复 也尝试使用BS从具有不同格式的不同源中提取具有相似数据的HTML表-Python,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我试图从两个不同的HTML源中提取HTML表。两者非常相似,每个表包含相同的数据,但它们的结构可能不同,列名称也不同。对于一个源,所有数据可能包含在一个表中,而另一个源可能将数据分解为两个单独的表 例如,我们可以看看AAPL和MMM股票的内幕持有人 这里的截图- 假设最终目标是提取内部人士持有的股份总数——一个单数。每个表的结构可能不同,但应该相似的是关键词,如实益或股票 任何帮助都将不胜感激。在之前的一篇文章中,我能够提取一些数据。但如果结构不同,就不能循环或重复 也尝试使用BS url =
url = 'https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.find_all('table')
rows = tables.find_all('tr')
这真的很复杂,但我们来了: 导入请求 从bs4导入BeautifulSoup 进口稀土 作为pd进口熊猫 URL=['https://www.sec.gov/Archives/edgar/data/320193/000119312520001450/d799303ddef14a.htm', 'https://www.sec.gov/Archives/edgar/data/66740/000120677420000907/mmm3661701-def14a.htm'] def主URL: 带请求。会话作为请求: 对于url中的url: r=req.geturl soup=BeautifulSoupr.content,“html.parser” 对于soup.findAlla中的项,text=re.compile^Security: item=item.gethref[1:] catch=soup.finda,{'name':item}.find\u nexttable df=pd.read\u htmlstrcatch printdf df[0]。to_csvf{item}.csv,index=False,header=None mainURL 输出:
[ 0 ... 8
0 NaN ... NaN
1 NaN ... NaN
2 Name of Beneficial Owner ... NaN
3 NaN ... NaN
4 The Vanguard Group ... %
5 NaN ... NaN
6 BlackRock, Inc. ... %
7 NaN ... NaN
8 Berkshire Hathaway Inc. / Warren E. Buffett ... %
9 NaN ... NaN
10 Kate Adams ... NaN
11 NaN ... NaN
12 Angela Ahrendts ... NaN
13 NaN ... NaN
14 James Bell ... NaN
15 NaN ... NaN
16 Tim Cook ... NaN
17 NaN ... NaN
18 Al Gore ... NaN
19 NaN ... NaN
20 Andrea Jung ... NaN
21 NaN ... NaN
22 Art Levinson ... NaN
23 NaN ... NaN
24 Luca Maestri ... NaN
25 NaN ... NaN
26 Deirdre O’Brien ... NaN
27 NaN ... NaN
28 Ron Sugar ... NaN
29 NaN ... NaN
30 Sue Wagner ... NaN
31 NaN ... NaN
32 Jeff Williams ... NaN
33 NaN ... NaN
34 All current executive officers and directors a... ... NaN
[35 rows x 9 columns]]
[ 0 1 ... 18 19
0 Name and principal position NaN ... Percent of Class NaN
1 Thomas “Tony” K. Brown, Director NaN ... (5) NaN
2 Pamela J. Craig, Director NaN ... (5) NaN
3 David B. Dillon, Director NaN ... (5) NaN
4 Michael L. Eskew, Director NaN ... (5) NaN
5 Herbert L. Henkel, Director NaN ... (5) NaN
6 Amy E. Hood, Director NaN ... (5) NaN
7 Muhtar Kent, Director NaN ... (5) NaN
8 Edward M. Liddy, Director NaN ... (5) NaN
9 Dambisa F. Moyo, Director NaN ... (5) NaN
10 Gregory R. Page, Director NaN ... (5) NaN
11 Patricia A. Woertz, Director NaN ... (5) NaN
12 Michael F. Roman, Chairman of the Board, Presi... NaN ... (5) NaN
13 Inge G. Thulin, Former Executive Chairman of t... NaN ... (5) NaN
14 Nicholas C. Gangestad, Senior Vice President a... NaN ... (5) NaN
15 Ashish K. Khandpur, Executive Vice President, ... NaN ... (5) NaN
16 Julie L. Bushman, Executive Vice President, In... NaN ... (5) NaN
17 Joaquin Delgado, Former Executive Vice Preside... NaN ... (5) NaN
18 Michael G. Vale, Executive Vice President, Saf... NaN ... (5) NaN
19 All Directors and Executive Officers as a Grou... NaN ... (5) NaN
[20 rows x 20 columns]]
[ 0 1 ... 6 7
0 Name/address NaN ... Percent of Class NaN
1 The Vanguard Group(1) 100 Vanguard Blvd. Malve... NaN ... 8.78 NaN
2 State Street Corporation(2) State Street Finan... NaN ... 7.36 NaN
3 BlackRock, Inc.(3) 55 East 52nd Street New Yor... NaN ... 7.30 NaN
[4 rows x 8 columns]]
@这真的很好。不管怎样,函数是否返回一个单数输出,即内部人员所持股份的总和?这就是它可能变得棘手的地方。因为AAPL将所有内部人员放在一张桌子上,而MMM将他们放在两张桌子上。该功能在我的机器上运行良好,但在CSV返回中,我需要手动进入并尝试合计份额。如果答案旁边的复选标记对您有帮助,请随时接受我的答案。
[ 0 ... 8
0 NaN ... NaN
1 NaN ... NaN
2 Name of Beneficial Owner ... NaN
3 NaN ... NaN
4 The Vanguard Group ... %
5 NaN ... NaN
6 BlackRock, Inc. ... %
7 NaN ... NaN
8 Berkshire Hathaway Inc. / Warren E. Buffett ... %
9 NaN ... NaN
10 Kate Adams ... NaN
11 NaN ... NaN
12 Angela Ahrendts ... NaN
13 NaN ... NaN
14 James Bell ... NaN
15 NaN ... NaN
16 Tim Cook ... NaN
17 NaN ... NaN
18 Al Gore ... NaN
19 NaN ... NaN
20 Andrea Jung ... NaN
21 NaN ... NaN
22 Art Levinson ... NaN
23 NaN ... NaN
24 Luca Maestri ... NaN
25 NaN ... NaN
26 Deirdre O’Brien ... NaN
27 NaN ... NaN
28 Ron Sugar ... NaN
29 NaN ... NaN
30 Sue Wagner ... NaN
31 NaN ... NaN
32 Jeff Williams ... NaN
33 NaN ... NaN
34 All current executive officers and directors a... ... NaN
[35 rows x 9 columns]]
[ 0 1 ... 18 19
0 Name and principal position NaN ... Percent of Class NaN
1 Thomas “Tony” K. Brown, Director NaN ... (5) NaN
2 Pamela J. Craig, Director NaN ... (5) NaN
3 David B. Dillon, Director NaN ... (5) NaN
4 Michael L. Eskew, Director NaN ... (5) NaN
5 Herbert L. Henkel, Director NaN ... (5) NaN
6 Amy E. Hood, Director NaN ... (5) NaN
7 Muhtar Kent, Director NaN ... (5) NaN
8 Edward M. Liddy, Director NaN ... (5) NaN
9 Dambisa F. Moyo, Director NaN ... (5) NaN
10 Gregory R. Page, Director NaN ... (5) NaN
11 Patricia A. Woertz, Director NaN ... (5) NaN
12 Michael F. Roman, Chairman of the Board, Presi... NaN ... (5) NaN
13 Inge G. Thulin, Former Executive Chairman of t... NaN ... (5) NaN
14 Nicholas C. Gangestad, Senior Vice President a... NaN ... (5) NaN
15 Ashish K. Khandpur, Executive Vice President, ... NaN ... (5) NaN
16 Julie L. Bushman, Executive Vice President, In... NaN ... (5) NaN
17 Joaquin Delgado, Former Executive Vice Preside... NaN ... (5) NaN
18 Michael G. Vale, Executive Vice President, Saf... NaN ... (5) NaN
19 All Directors and Executive Officers as a Grou... NaN ... (5) NaN
[20 rows x 20 columns]]
[ 0 1 ... 6 7
0 Name/address NaN ... Percent of Class NaN
1 The Vanguard Group(1) 100 Vanguard Blvd. Malve... NaN ... 8.78 NaN
2 State Street Corporation(2) State Street Finan... NaN ... 7.36 NaN
3 BlackRock, Inc.(3) 55 East 52nd Street New Yor... NaN ... 7.30 NaN
[4 rows x 8 columns]]