ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.79k stars 2.44k forks source link

does yfinance work with UK LSE stocks? #523

Closed norricorp closed 3 years ago

norricorp commented 3 years ago

I have tried the MSFT example and it works. But if I use LSE codes, I get the following

>>> brwm = yf.Ticker("BRWM")
>>> brwm.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\ticker.py", line 138, in info
    return self.get_info()
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\base.py", line 446, in get_info
    self._get_fundamentals(proxy)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\base.py", line 340, in _get_fundamentals
    self._info['regularMarketPrice'] = self._info['regularMarketOpen']
KeyError: 'regularMarketOpen'

Using yahoo finance directly, I noticed that it had the code with an additional "L" (for London I guess), so I tried that.

>>> brwm = yf.Ticker("BRWM.L")
>>> brwm.info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\ticker.py", line 138, in info
    return self.get_info()
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\base.py", line 446, in get_info
    self._get_fundamentals(proxy)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\yfinance\base.py", line 285, in _get_fundamentals
    holders = _pd.read_html(url)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\html.py", line 1101, in read_html
    displayed_only=displayed_only,
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\html.py", line 917, in _parse
    raise retained
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\html.py", line 898, in _parse
    tables = p.parse_tables()
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\html.py", line 217, in parse_tables
    tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\html.py", line 547, in _parse_tables
    raise ValueError("No tables found")
ValueError: No tables found

So should I use just the LSE code or with an additional "L". Of course neither works so what am I doing wrong?

Regards,

norricorp commented 3 years ago

An update. So using BRWM (rather than BRWM.L) the problem seems to be "regularMarketOpen". Does yahoo finance uk give a different json structure to yahoo finance US? And that regularMarketOpen is not an attribute in the data?

AdamHodgson commented 3 years ago

Experiencing the same issue.

@norricorp were you able to find out any more about the JSON structure? Would be happy to pick this issue up at the weekend

norricorp commented 3 years ago

Actually I moved to rapidapi (https://rapidapi.com/apidojo/api/yahoo-finance1). You do have to sign up but there is a 500 calls/month free plan. And it works really well. You have to end each stock symbol with .L (eg BARC.L) and some symbols are not the same as in the FT but just search for the stock name on the yahoo finance page and use their symbol.

pauljsymonds commented 3 years ago

If you don't put the .L for London some stocks will return US Equities instead of London such as WPP for example.

I have some code that scrapes the .L data formats, if I get some time I'll see if I can pull and fix this but it won't be quick as I am mega busy.

silvavn commented 3 years ago

It does work, code:

brwm = yf.Ticker("BRWM.L")
brwm.info

close if solved.

norricorp commented 3 years ago

Closed