简体   繁体   中英

Getting price data from YahooFinance causes: AttributeError 'nonetype' object has no attribute 'text'

I have a loop (runs about 200 times) to get previous close price from YahooFinance. This loop stops randomly at a point with the following error message:

WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
[...]
AttributeError 'nonetype' object has no attribute 'text'

Each time I run the script it stops at a different point. This is the script:

from yahoofinancials import YahooFinancials
import csv

with open('instruments.csv', 'r') as csvfile:
    instruments = csv.reader(csvfile, delimiter=',', quoting = csv.QUOTE_NONNUMERIC, quotechar='"')
    for instrument in instruments:
        symbol = instrument[0]
        yahoo_financials = YahooFinancials(symbol)
        price = yahoo_financials.get_prev_close_price()

Solution: instead of looping over every symbol and request the price you can create a list of symbols and give this list to the YahooFincials api and then do the request. Seems like the package can handle this perfectly although it takes some time. Here is an excerpt from the doc :

from yahoofinancials import YahooFinancials
tech_stocks = ['AAPL', 'MSFT', 'INTC']
yahoo_financials_tech = YahooFinancials(tech_stocks)
tech_stock_price_data = yahoo_financials_tech.get_prev_close_price()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM