【发布时间】:2019-11-17 14:36:21
【问题描述】:
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)
这给了我以下错误:
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'
我想知道如何正确编写此内容,因为我需要使用以下格式的最终日期(在负 1、2 或 3 天之后):YYYYMMDD。
谢谢
【问题讨论】:
-
你是什么意思“语法”?这是逻辑,你不需要任何特定的语法。你尝试过什么,它到底有什么问题?
-
str(time.strftime("%m/%d/%Y")), #应该是负3天。我不知道如何制作负 3 天并保持相同的格式
-
您尝试过任何研究吗?以stackoverflow.com/questions/441147/… 为例。
-
我已经看到并尝试过了,但是我遇到了一些问题。由于我在代码的开头已经有“import time import datetime from datetime import date”,我可以简单地在“from datetime import date”“, datetime, timedelta”旁边添加吗?因为那是我尝试过的,但没有奏效..我再次尝试确定。我是 Python 初学者
-
所以给那个具体问题的minimal reproducible example。
标签: python python-3.x python-datetime