You are trying to replace the times
result set (with s
), not the single time
tag (no s
).
However, you probably don't want to replace the tag; you want to replace the text in the tag:
for time in times:
timestring = time.text[8:27]
newdate = (datetime.datetime.strptime(timestring, "%Y-%m-%d %H:%M:%S") +
datetime.timedelta(hours= h, minutes = m, seconds = s))
newstring = soup.new_string(time.text[:8] +
newdate.strftime("%Y-%m-%d %H:%M:%S") + time.text[27:])
time.string.replaceWith(newstring)
This creates a new NavigableString
object with soup.new_string()
, and replaces the original string child node on time
.