要从该表中提取信息,您可以使用以下脚本:
import requests
from bs4 import BeautifulSoup
url = 'https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/R2.htm'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
all_data = []
for tr in soup.select('tr'):
tds = [td.get_text(strip=True) for td in tr.select('td') if td.get_text(strip=True)]
if len(tds) == 4:
all_data.append(tds)
for row in all_data:
print('{:<50} {:<10} {:<10} {:<10}'.format(*row))
打印:
Net sales $ 260,174 $ 265,595 $ 229,234
Cost of sales 161,782 163,756 141,048
Gross margin 98,392 101,839 88,186
Research and development 16,217 14,236 11,581
Selling, general and administrative 18,245 16,705 15,261
Total operating expenses 34,462 30,941 26,842
Operating income 63,930 70,898 61,344
Other income/(expense), net 1,807 2,005 2,745
Income before provision for income taxes 65,737 72,903 64,089
Provision for income taxes 10,481 13,372 15,738
Net income $ 55,256 $ 59,531 $ 48,351
Basic (in dollars per share) $ 11.97 $ 12.01 $ 9.27
Diluted (in dollars per share) $ 11.89 $ 11.91 $ 9.21
Basic (in shares) 4,617,834 4,955,377 5,217,242
Diluted (in shares) 4,648,913 5,000,109 5,251,692
Net sales $ 213,883 $ 225,847 $ 196,534
Cost of sales 144,996 148,164 126,337
Net sales 46,291 39,748 32,700
Cost of sales $ 16,786 $ 15,592 $ 14,711
或使用pandas:
import pandas as pd
url = 'https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/R2.htm'
df = pd.read_html(url)[0]
print(df)
打印:
CONSOLIDATED STATEMENTS OF OPERATIONS - USD ($) shares in Thousands, $ in Millions 12 Months Ended
CONSOLIDATED STATEMENTS OF OPERATIONS - USD ($) shares in Thousands, $ in Millions Sep. 28, 2019 Sep. 29, 2018 Sep. 30, 2017
0 Net sales $ 260,174 $ 265,595 $ 229,234
1 Cost of sales 161782 163756 141048
2 Gross margin 98392 101839 88186
3 Operating expenses: NaN NaN NaN
4 Research and development 16217 14236 11581
5 Selling, general and administrative 18245 16705 15261
6 Total operating expenses 34462 30941 26842
7 Operating income 63930 70898 61344
8 Other income/(expense), net 1807 2005 2745
9 Income before provision for income taxes 65737 72903 64089
10 Provision for income taxes 10481 13372 15738
11 Net income $ 55,256 $ 59,531 $ 48,351
12 Earnings per share: NaN NaN NaN
13 Basic (in dollars per share) $ 11.97 $ 12.01 $ 9.27
14 Diluted (in dollars per share) $ 11.89 $ 11.91 $ 9.21
15 Shares used in computing earnings per share: NaN NaN NaN
16 Basic (in shares) 4617834 4955377 5217242
17 Diluted (in shares) 4648913 5000109 5251692
18 Products NaN NaN NaN
19 Net sales $ 213,883 $ 225,847 $ 196,534
20 Cost of sales 144996 148164 126337
21 Services NaN NaN NaN
22 Net sales 46291 39748 32700
23 Cost of sales $ 16,786 $ 15,592 $ 14,711
编辑:要从第一列获取 id,您可以这样做:
import re
import requests
from bs4 import BeautifulSoup
url = 'https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/R2.htm'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
all_data = []
for tr in soup.select('tr'):
tds = [td for td in tr.select('td') if td.get_text(strip=True)]
if len(tds) == 4:
tds[0] = re.search(r"'(.*?)'", tds[0].a['onclick']).group(1)
tds[1:] = [td.get_text(strip=True) for td in tds[1:]]
all_data.append(tds)
for row in all_data:
print('{:<90} {:<10} {:<10} {:<10}'.format(*row))
打印:
defref_us-gaap_RevenueFromContractWithCustomerExcludingAssessedTax $ 260,174 $ 265,595 $ 229,234
defref_us-gaap_CostOfGoodsAndServicesSold 161,782 163,756 141,048
defref_us-gaap_GrossProfit 98,392 101,839 88,186
defref_us-gaap_ResearchAndDevelopmentExpense 16,217 14,236 11,581
defref_us-gaap_SellingGeneralAndAdministrativeExpense 18,245 16,705 15,261
defref_us-gaap_OperatingExpenses 34,462 30,941 26,842
defref_us-gaap_OperatingIncomeLoss 63,930 70,898 61,344
defref_us-gaap_NonoperatingIncomeExpense 1,807 2,005 2,745
defref_us-gaap_IncomeLossFromContinuingOperationsBeforeIncomeTaxesExtraordinaryItemsNoncontrollingInterest 65,737 72,903 64,089
defref_us-gaap_IncomeTaxExpenseBenefit 10,481 13,372 15,738
defref_us-gaap_NetIncomeLoss $ 55,256 $ 59,531 $ 48,351
defref_us-gaap_EarningsPerShareBasic $ 11.97 $ 12.01 $ 9.27
defref_us-gaap_EarningsPerShareDiluted $ 11.89 $ 11.91 $ 9.21
defref_us-gaap_WeightedAverageNumberOfSharesOutstandingBasic 4,617,834 4,955,377 5,217,242
defref_us-gaap_WeightedAverageNumberOfDilutedSharesOutstanding 4,648,913 5,000,109 5,251,692
defref_us-gaap_RevenueFromContractWithCustomerExcludingAssessedTax $ 213,883 $ 225,847 $ 196,534
defref_us-gaap_CostOfGoodsAndServicesSold 144,996 148,164 126,337
defref_us-gaap_RevenueFromContractWithCustomerExcludingAssessedTax 46,291 39,748 32,700
defref_us-gaap_CostOfGoodsAndServicesSold $ 16,786 $ 15,592 $ 14,711