bin
bin

Reputation: 1925

python the code strftime("%s") errors

THE python program is as follows, it errors:

File "C:\Python\PyCharmProject\FaceBookCrawl\group_download.py", line 31, in getFeed params += "&since=" + SINCE.strftime("%s")
ValueError: Invalid format string

the program it seems SINCE.strftime("%s") is wrong, how to solve it?

SINCE = datetime.datetime.now() - datetime.timedelta(DAYS)

params = "?fields=permalink_url,from,story,type,message,link,created_time,updated_time,likes.limit(0).summary(total_count),comments.limit(0).summary(total_count)"

#Default paging limit
params += "&amp&limit=" + DEFAULT_LIMIT

#Time-based limit
params += "&since=" + SINCE.strftime("%s")
graph_url = GRAPH_URL_PREFIX + group + "/feed" + params

Upvotes: 3

Views: 5495

Answers (3)

user16543787
user16543787

Reputation: 80

For anybody coming here when using ("%s") to generate an Epoch timestamp. Note that the usage of strftime("%s") is platform dependent and doesnt work on windows while it works on Linux with you Local Timezone. You can just use timestamp():

int(datetime.datetime.utcnow().timestamp())

You can read more here Convert python datetime to epoch with strftime

Upvotes: 5

Nabeel Ahmed
Nabeel Ahmed

Reputation: 19242

ValueError: Invalid format string

You're using the wrong formatter i.e. it has to be upper case 'S' - here's datetime's strftime reference.


UnicodeEncodeError: 'locale' codec can't encode character '\uff33' in position 1: Illegal byte sequence

the \uff33 is basically the the full width latin letter 'S' - the one you edited to get rid of previous ValueError.

Solution/way-outs:

1.Use raw string i.e. prefix your string with an 'r'

params = r"?fields=permalink_url,from,story,type,message,link,created_time,updated_time,likes.limit(0).summary(total_count),comments.limit(0).summary(total_count)"

2.If you're using str() to convert from unicode to encoded text / bytes - instead use .encode() to encode the string. A helpful SO thread.

Upvotes: 0

Iron Fist
Iron Fist

Reputation: 10951

Actually, it should be capital S:

params += "&since=" + SINCE.strftime("%S")
                                           ^

Upvotes: 5

Related Questions