简体   繁体   中英

TypeError: object of type 'datetime.timedelta' has no len()

i´m having a problem with a code. I need to subtract a day in a datetime.now to webscrapping a workbook from the Anbima site (a brazilian financial data site). Please, someone know how can i solve this?

Code:

today = datetime.now(timezone('America/Sao_Paulo')).strftime("%d/%m/%Y") 
today = timedelta(days=-1) #today = '29/11/2022'
wd.find_element(By.NAME, "Dt_Ref").clear()
wd.find_element(By.NAME, "Dt_Ref").send_keys(today)

and the error:

today = datetime.now(timezone('America/Sao_Paulo')).strftime("%d/%m/%Y") 
today = timedelta(days=-1) #today = '29/11/2022' 
wd.find_element(By.NAME, "Dt_Ref").clear()
wd.find_element(By.NAME, "Dt_Ref").send_keys(today)

I tried to do the timedelta but its not working. If there is another way to subtract a day i accept please.

This line:

today = timedelta(days=-1) #today = '29/11/2022'

is overwriting the today variable. You should instead use an augmented assignment so you can subtract 1 day from today :

today = datetime.now(timezone('America/Sao_Paulo'))
today -= timedelta(days=1)
wd.find_element(By.NAME, "Dt_Ref").clear()
wd.find_element(By.NAME, "Dt_Ref").send_keys(today.strftime("%d/%m/%Y"))

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