【问题标题】:Missing Dictionary Value from Webpage网页中缺少字典值
【发布时间】:2019-06-15 01:21:59
【问题描述】:

我正在制作一个从 yahoo Finance 获取技术信息的股票筛选器。 该程序创建一个字典,其中包含显示在统计页面上的所有技术。我有点困惑,因为它没有“Price/Book”值,即使它遵循与包含的其他技术相同的 html 格式,例如“权益回报率”。

我有点困惑,因为它没有“价格/账面价值”,即使它遵循与其他技术相同的 html 格式,例如“权益回报率”。

def scrape_yahoo(stock):
    technicals = {}
    try:
        url = ('http://finance.yahoo.com/q/ks?s='+stock)
        page = urllib2.urlopen(url)
        soup = BeautifulSoup(page, 'html.parser')
        tables = soup.findAll('table', {"class" : 'table-qsp-stats'})    # Found using page inspection
        for table in tables:
            table_body = table.find('tbody')
            rows = table_body.find_all('tr')

            for row in rows:
                col_name = row.find_all('span')                            # Use span to avoid supscripts
                col_name = [cell.text.strip() for cell in col_name]
                col_val = row.find_all('td')
                col_val = [cell.text.strip() for cell in col_val]
                technicals[col_name[0]] = col_val[1]                    # col_val[0] is the name cell (with subscript)
        return technicals
    except Exception as e:
        print('Failed, exception: ', str(e))



def scrape(stock_list, interested, technicals):
    SuggestedStocks = []
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)
        condition_1 = float(technicals.get('Return on Equity',0).replace('%','').replace('N/A','-100')) > 25
        condition_2 = float(technicals.get('Trailing P/E',0).replace('N/A','0')) < 25
        condition_3 = float(technicals.get('Price/Book(mrq)',0)) <8
        condition_4 = float(technicals.get('Beta (3Y Monthly)',0)) <1.1
        if condition_1 and condition_2 and condition_3 and condition_4:
            print(each_stock)

            SuggestedStocks.append(each_stock)  
            for ind in interested: 

                print(ind + ": "+ technicals[ind])         
            print("------")
            time.sleep(1)   
                                               # Use delay to avoid getting flagged as bot
    #return technicals
    print(SuggestedStocks)


def main():

    stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD', 'AAP', 'AES', 'AMG', 'AFL', 'A', 'APD', 'AKAM', 'ALK', 'ALB', 'ARE', 'ALXN', 'ALGN', 'ALLE', 'AGN', 'ADS', 'LNT', 'ALL', 'GOOGL', 'GOOG', 'MO', 'AMZN', 'AMCR', 'AEE', 'AAL', 'AEP', 'AXP', 'AIG']
    interested = ['Return on Equity', 'Revenue', 'Quarterly Revenue Growth','Trailing P/E', 'Beta (3Y Monthly)']#,'Price/Book(mrq)']
    technicals = {}

    tech = scrape(stock_list, interested, technicals)
    print(tech)

main()

KeyError: 'Price/Book(mrq)',我明白这个错误是什么意思,我的主要问题是为什么这个值没有添加到字典中?

【问题讨论】:

    标签: python beautifulsoup urllib finance


    【解决方案1】:

    页面上的字段名称是“Price/Book (mrq)”而不是“Price/Book(mrq)”,你漏掉了一个空格。

    【讨论】:

    • 我添加了一个空格,但它仍然给我 KeyError: 'Price/Book (mrq)'
    • 没关系,我想通了。在 html 代码下,该字段标记为“Price/Book”。感谢您的帮助!'
    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多