Solution 1 :

The issue is with parsing of the date = we want '18.03.2020', while it’s looking for '18.03.20'. So it’s coming up with an empty row and then you’re trying to split None and take the 0 index.

So just change where you see '%d.%m.%y' to '%d.%m.%Y'

def loadFromWebsite(company,ISIN):

    # counter to load past 4 weeks
    count = 28

    # generating URL to website
    URL = "https://www.boerse.de/historische-kurse/{0}-Aktie/{1}".format (company, ISIN)

    shareValues = []

    # getting onto website
    response = requests.get(URL)

    # get current date
    date_object = datetime.datetime.now()

    # website lists values starting from previous day
    start = date_object + datetime.timedelta(days = -1)

    previous = start

    # counting 4 weeks
    while count > 0:

        # Market is closed on saturdays (5) and sundays (6)
        if previous.weekday() < 5:

            # storing content of page
            src = response.content

            # create BeatifulSoup Object based on src
            soup = BeautifulSoup(src, 'html.parser')

            tables = soup.find_all("table")

            for table in tables:
                if start.strftime('%d.%m.%Y') in table.text:
                    df = pd.read_html(str(table))[0]

                    # get row of the requested date
                    row = df[df['Datum']== previous.strftime('%d.%m.%Y')].reset_index()

                    # add value of share (beginning of day) to values array
                    value = (row.loc[0,'Erster Schluss'].split()[0]).replace(',','.')
                    print(value)

                    # add date of the current value to date array
                    date = (previous.strftime('%d.%m.%Y'))
                    test = []
                    test.append(value)
                    test.append(date)

                    # save value and date into list
                    shareValues.append(test)
        count = count - 1

        previous = previous + datetime.timedelta(days = -1)

loadFromWebsite("TecDax","DE0007203275")

Problem :

My Python program gets values of shares from a website, but doesn’t work when a value is above 99. I don’t understand the error message. The website is an HTML where I get values of a table.

def loadFromWebsite(company,ISIN):

    # counter to load past 4 weeks
    count = 28

    # generating URL to website
    URL = "https://www.boerse.de/historische-kurse/{0}-Aktie/{1}".format (company, ISIN)

    shareValues = []

    # getting onto website
    response = requests.get(URL)

    # get current date
    date_object = datetime.datetime.now()

    # website lists values starting from previous day
    start = date_object + datetime.timedelta(days = -1)

    previous = start

    # counting 4 weeks
    while count > 0:

        # Market is closed on saturdays (5) and sundays (6)
        if previous.weekday() < 5:

            # storing content of page
            src = response.content

            # create BeatifulSoup Object based on src
            soup = BeautifulSoup(src, 'html.parser')

            tables = soup.find_all("table")

            for table in tables:
                if start.strftime('%d.%m.%y') in table.text:
                    df = pd.read_html(str(table))[0]

                    # get row of the requested date
                    row = df[df['Datum']== previous.strftime('%d.%m.%y')].reset_index()

                    # add value of share (beginning of day) to values array
                    value = (row.loc[0,'Erster Schluss'].split()[0]).replace(',','.')
                    print(value)

                    # add date of the current value to date array
                    date = (previous.strftime('%d.%m.%y'))
                    test = []
                    test.append(value)
                    test.append(date)

                    # save value and date into list
                    shareValues.append(test)
        count = count - 1

        previous = previous + datetime.timedelta(days = -1)

loadFromWebsite("TecDax","DE0007203275")

Error Message:

  Traceback (most recent call last):
      File "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexesrange.py", line 350, 
    in get_loc
        return self._range.index(new_key)
    ValueError: 0 is not in range

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "c:/Users/hendr/Documents/Python_Projects/loadShareValues.py", line 99, in <module>
        loadFromWebsite("TecDax","DE0007203275")
      File "c:/Users/hendr/Documents/Python_Projects/loadShareValues.py", line 82, in loadFromWebsite
        value = (row.loc[0,'Erster Schluss'].split()[0]).replace(',','.')
      File "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexing.py", line 1762, in __getitem__
        return self._getitem_tuple(key)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexing.py", line 1272, in _getitem_tuple
        return self._getitem_lowerdim(tup)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexing.py", line 1389, in _getitem_lowerdim
        section = self._getitem_axis(key, axis=i)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexing.py", line 1965, in _getitem_axis
        return self._get_label(key, axis=axis)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexing.py", line 625, in _get_label
        return self.obj._xs(label, axis=axis)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoregeneric.py", line 3537, in xs
        loc = self.index.get_loc(key)
      File 
    "C:UsershendrAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagespandascoreindexesrange.py", line 352, 
    in get_loc
        raise KeyError(key)
    KeyError: 0
  row:    
  index     Datum Erster Schluss  Hoch Tief Tief Schluss Volumen
  0      0  18.03.20      4,95 5,10  5,45 4,91  491     510    2.197.259

Comments

Comment posted by It_is_Chris

The error is thrown because of this line:

Comment posted by Hendrik Niemax

Do you know how to solve it? I am new to python

Comment posted by It_is_Chris

What does the

Comment posted by Hendrik Niemax

Changed it, so it is way shorter than the real numbers (2.277,16)

Comment posted by Hendrik Niemax

the issue with that is, if i search in the html for a year with 4 digits i wont find any values

Comment posted by chitown88

then have the search in the html search for either 2 or 4 digits for the year

Comment posted by chitown88

I know, i tested it before posting. Kindly accept the solution here (and the solution from yesterday as well).

By