【问题标题】:assigning a function that returns a list to a list gives an Index Error将返回列表的函数分配给列表会产生索引错误
【发布时间】:2020-08-07 15:47:17
【问题描述】:

我有一个函数可以返回一个名为get_next_3_numbers_after_match 的列表。每当我尝试将该列表分配给另一个列表时,它都会出现索引错误。代码如下。

def get_next_3_numbers_after_match(MatchList):
     NumList = [Num.strip() for Num in Matchlist if Num[0]=='$' or Num[0].isdigit()==True]
     return NumList

def get_eps():
     for url in list(urls):
         try:
             BASE_URL = "https://www.example.com/"
             soup = BeautiflSoup(requests.get(url, headers).content, 'lxml')
             test = soup.prettify().split('Revenue')
         except IndexError:
             BASE_URL = "https://www.example.com/2"
             soup = BeautiflSoup(requests.get(url, headers).content, 'lxml')
             test = soup.prettify().split('Revenue')[1]
         finally:
             RevenueList = []
           
             RevenueList.extend(get_next_3_numbers_after_match(soup.prettify()\
                        .split('Revenue')[1].split("\n")[0:21]))
     return RevenueList

运行 for 循环会出现以下错误: RevenueList.extend() IndexError: List index out of range

【问题讨论】:

  • soup.prettify().split('Revenue')[1].split("\n") 在循环的一次迭代中可能没有 20 个元素。尝试将该行分割成单独的行,并在每个步骤中检查输出,以验证您得到的结果是否确实是您所期望的
  • 能分享一下网址吗,想获取什么信息?
  • @AndrejKesely 我想获取 sec 股票数据。苹果的网址是:sec.gov/Archives/edgar/data/320193/000032019319000119/R2.htm。我要获取的数据是收入数据,它有 3 列,我想要所有这些

标签: python python-3.x list beautifulsoup


【解决方案1】:

要从该表中提取信息,您可以使用以下脚本:

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  

【讨论】:

  • 嘿,这太棒了!我不知道这是否可能,但每个 Id(例如净销售额、毛利率)都有一个标识符,您可以在查看页面源代码时看到该标识符。对于第一个净销售额列,“标识符”是“defref_us-gaap_RevenueFromContractWithCustomerExcludingAssessedTax”。有什么方法可以用真实标识符替换诸如净销售额之类的东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2018-07-09
  • 2020-02-03
  • 2013-04-24
  • 1970-01-01
相关资源
最近更新 更多