2

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
jrbedard
  • 3,662
  • 5
  • 30
  • 34
bin
  • 1,625
  • 3
  • 12
  • 10

3 Answers3

5

Actually, it should be capital S:

params += "&since=" + SINCE.strftime("%S")
                                           ^
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • when modify to capital S:it errors:File "C:\Python\PyCharmProject\FaceBookCrawl\group_download.py", line 31, in getFeed params += "&since=" + SINCE.strftime("%S") UnicodeEncodeError: 'locale' codec can't encode character '\uff33' in position 1: Illegal byte sequence – bin Jan 12 '17 at 08:34
  • You have a unicode issue, what Python version are you using? and can you post what's in `SINCE` variable – Iron Fist Jan 12 '17 at 08:36
  • Can you post what's in `param` and `SINCE` ? – Iron Fist Jan 12 '17 at 08:42
  • DAYS = 14 SINCE = datetime.datetime.now() - datetime.timedelta(DAYS) when print(SINCE),the result is 2016-12-29 00:37:29.296710 – bin Jan 12 '17 at 08:47
  • You probably have in `param` some unicode char. whose encoding isn't 'utf-8' because error message says at position 1, can u post what's in `param`? – Iron Fist Jan 12 '17 at 08:50
  • what do you mean, what is the "post" in your comment mean – bin Jan 12 '17 at 08:55
  • could you please help me, I have no idea – bin Jan 12 '17 at 10:16
  • Can you post more code?, because like this it is hard for me to keep guessing your issue – Iron Fist Jan 12 '17 at 12:05
1

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

0

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.

Community
  • 1
  • 1
Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63