0
    import pandas as pd
    import requests
    from bs4 import BeautifulSoup
    import time
    import datetime
    from datetime import date, timedelta

    # MM/DD/YYYY : time.strftime("%m/%d/%Y")
    # YYYYMMDD : time.strftime('%Y%m%d')

if datetime.datetime.now().isoweekday() == 1 : # If today is a Monday -> some dates must be minus 3 days to take the previous Friday's data

    url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&productGroupId=13394&productId=34642&cp=&month=&year=&busDate=' + str(time.strftime('%Y%m%d') - timedelta(days=3))   #Should be minus 3 days
    df = pd.read_html(url_DAX)[0]
    df.to_csv('results_DAX_' + str(time.strftime('%Y%m%d')) + '.csv') 
    print(df)

This give me the following error :

 url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&productGroupId=13394&productId=34642&cp=&month=&year=&busDate=' + str(time.strftime('%Y%m%d') - timedelta(days=2))  # Should be minus 2 days
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

I'd like to know how to write this correctly as I need to have the final date (after the minus 1, 2 or 3 days) in the following format : YYYYMMDD.

Thanks

manny-
  • 129
  • 2
  • 10
  • What do you mean *"syntax"*? This is *logic*, you don't need any specific syntax. What have you tried and what exactly is the problem with it? – jonrsharpe Nov 17 '19 at 14:40
  • str(time.strftime("%m/%d/%Y")), #Should be minus 3 days. I don't know how to make minus 3 days and keeping the same format – manny- Nov 17 '19 at 14:46
  • 1
    Did you try any research? https://stackoverflow.com/questions/441147/how-to-subtract-a-day-from-a-date, for example. – jonrsharpe Nov 17 '19 at 14:47
  • I've seen and tried it, however I had some issues. As I have already " import time import datetime from datetime import date" at the beginning of my code, can I simply add, next to "from datetime import date" ", datetime, timedelta" ? Because that's what I tried and it didn't work .. I try it again to be sure. I'm a beginner in Python – manny- Nov 17 '19 at 14:51
  • So give a [mcve] of that specific issue. – jonrsharpe Nov 17 '19 at 14:57
  • So I wrote : import pandas as pd import requests from bs4 import BeautifulSoup import time import datetime from datetime import date, datetime, timedelta and in the url_DAX I wrote : url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&productGroupId=13394&productId=34642&cp=&month=&year=&busDate=' + str(time.strftime('%Y%m%d') - timedelta(days=3)) . I have the following error : AttributeError: type object 'datetime.datetime' has no attribute 'datetime' – manny- Nov 17 '19 at 15:01
  • If you `from datetime import datetime` then the thing in your current scope named `datetime` is the *class*, `datetime.datetime`, not the module, `datetime`. But I meant [edit] the question; code is hard to read in comments and what you've included in the question isn't a MRE. – jonrsharpe Nov 17 '19 at 15:03
  • Then I should delete "from datetime import date, datetime, timedelta". And just keep "import datetime" ? – manny- Nov 17 '19 at 15:06
  • It depends how you want to access the members of that module, but **you must be *consistent***. – jonrsharpe Nov 17 '19 at 15:08
  • I have a date in the following format : YYYYDDMM and if today's date is a Monday for example, I want the date in the URL to be (today's date - 3 days) and in the following format YYYYDDMM :) – manny- Nov 17 '19 at 15:10
  • Then we return to the question: what *specific problem* do you have with your implementation of that? The linked post shows how to do the subtraction, and evidently you know how to format a date to a string because it's in what you've posted. – jonrsharpe Nov 17 '19 at 15:13
  • I've edited the main question to try to be more consistent in what I want. I hope it's clear enough .. As I said, I'm beginning with Python – manny- Nov 17 '19 at 15:30
  • 1. That's *not* a [mcve]. 2. `time.strftime('%Y%m%d') - timedelta(days=2)`, as the error message makes clear, is trying to subtract *after* you've converted the value to a string. Note this also makes `str(time.strftime('%Y%m%d'))` redundant - `strftime` **returns a string**, that's the point of it. – jonrsharpe Nov 17 '19 at 15:32
  • So how should I write it ? At the beginning the URL looked like that : url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&productGroupId=13394&productId=34642&cp=&month=&year=&busDate=' + str(time.strftime('%Y%m%d')) . And it worked. Then I realize that I needed to make it minus x days depending what day we are. And since, I can't find how to write that correctly ... – manny- Nov 17 '19 at 15:42

2 Answers2

1

Do the subtraction in a separate statement:

from datetime import date, timedelta
dt = date.today() - timedelta(days=2)

Then convert it to a string in your format

busdate =  dt.strftime('%Y%m%d')
url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&............&busDate=' + busdate
wwii
  • 23,232
  • 7
  • 37
  • 77
0

You can use datetime module. Make datetime instance and then add timedelta you want to:

dtm += datetime.timedelta(hours=2)
stardust
  • 593
  • 7
  • 9
  • Simply this way : str(time.strftime('%Y%m%d') - timedelta(days=3)) ? I need to keep the following format in the URL (YYYYMMDD) – manny- Nov 17 '19 at 15:02