Python Forum

Full Version: [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

The following code works in other scripts that read an RSS feed, but for some reason, this one doesn't work with another site.

As shown, I tried .split() in case it had a carriage return, but it makes no difference:

URL="https://feeds.acme.com/blah.xml"
resp = requests.get(URL)
soup = BeautifulSoup(resp.text,'xml')
for item in soup("item"):
	title=item.title.string
	link=item.link.string

	#<pubDate>Mon, 28 Oct 2024 07:44:18 GMT</pubDate>
	#ValueError: time data 'Mon, 28 Oct 2024 07:44:18 GMT' does not match format '%a, %d %b %Y %H:%M:%S %z'
	date_obj = dt.datetime.strptime(item.pubDate.string, "%a, %d %b %Y %H:%M:%S %z")

	#NO DIFF date = item.pubDate.string.strip()
	#date_obj = dt.datetime.strptime(date, "%a, %d %b %Y %H:%M:%S %z")

	pubDate = "{} {} {}".format(date_obj.day,date_obj.strftime("%b"),date_obj.year)
Any idea what could be wrong?

Thank you.

--
Edit: Found it here: "don't confuse %Z (timezone abbr) and %z (numeric offset)"

This works:
date_obj = dt.datetime.strptime(item.pubDate.string, "%a, %d %b %Y %H:%M:%S %Z")
It seems like the format of the pubDate string might be slightly different from what you expect. The error indicates that the time string 'Mon, 28 Oct 2024 07:44:18 GMT' doesn't match the strptime format "%a, %d %b %Y %H:%M:%S %z", possibly because of the GMT part not being recognized as a timezone offset. Try updating the format string to handle GMT or removing it before parsing:

date_str = item.pubDate.string.replace('GMT', '').strip()
date_obj = dt.datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S")

This should parse the date correctly.