Python Forum
[SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar'
#1
Question 
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")
Reply
#2
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.
buran write Jan-02-2025, 04:13 AM:
Spam link removed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does datetime.strptime() not work in this situation Monomial 2 242 Apr-08-2025, 06:36 PM
Last Post: snippsat
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,012 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  Python date format changes to date & time 1418 4 2,532 Jan-20-2024, 04:45 AM
Last Post: 1418
  Export data from PDF as tabular format zinho 5 2,328 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  String formatting (strptime) issues Henrio 2 1,781 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 3,729 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 4,910 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  What time format is this? wrybread 3 3,838 Jun-15-2022, 02:46 PM
Last Post: snippsat
  Problem with datetime [SOLVED] AlphaInc 3 8,830 Apr-22-2022, 01:33 PM
Last Post: snippsat
  How to get evenly-spaced datetime tick labels regardless of x-values of data points? Mark17 4 8,895 Apr-04-2022, 07:10 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020