Невозможно отправить сообщение с PRAW API - PullRequest
0 голосов
/ 26 марта 2019

У меня есть этот код:

import praw

print('starting')
reddit = praw.Reddit(client_id='****',
                     client_secret='********',
                     user_agent='****',
                     username = '****',
                     password = '****')

r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})

print("finishing")

Но возвращается с ошибкой:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-19-7e66ffa81635> in <module>
      9                      password = '*****')
     10 
---> 11 r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})
     12 
     13 print("finishing")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\reddit.py in post(self, path, data, files, params)
    481         data = self.request('POST', path, data=data or {}, files=files,
    482                             params=params)
--> 483         return self._objector.objectify(data)
    484 
    485     def put(self, path, data=None):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\objector.py in objectify(self, data)
    148             if len(errors) == 1:
    149                 raise APIException(*errors[0])
--> 150             assert not errors
    151 
    152         elif isinstance(data, dict):

AssertionError: 

и в некоторых случаях возвращается один и тот же код:

---------------------------------------------------------------------------
APIException                              Traceback (most recent call last)
<ipython-input-27-b62f9f5f585d> in <module>
      9                      password = '****')
     10 
---> 11 r = reddit.post("/api/submit",data={'title':'my firts title','text':'the text of my post','sr':'r/test'})
     12 
     13 print("finishing")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\reddit.py in post(self, path, data, files, params)
    481         data = self.request('POST', path, data=data or {}, files=files,
    482                             params=params)
--> 483         return self._objector.objectify(data)
    484 
    485     def put(self, path, data=None):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\praw\objector.py in objectify(self, data)
    147             errors = data['json']['errors']
    148             if len(errors) == 1:
--> 149                 raise APIException(*errors[0])
    150             assert not errors
    151 

APIException: INVALID_OPTION: 'opci\xf3n inv\xe1lida' on field 'sr'

Если честно, я не знаю, что я делаю неправильно. Я полагаю, что есть лучший способ отправить сообщение в Reddit, но документация не очень полезна

1 Ответ

2 голосов
/ 26 марта 2019

Вы должны сделать:

my_post = reddit.subreddit('subreddit').submit('My Title', selftext='Stuff you want to put in the textbox')

Обратите внимание, что subreddit не должно включать r / .

согласно: https://praw.readthedocs.io/en/latest/code_overview/models/subreddit.html#praw.models.Subreddit.submit

...