Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would definitely suggest trying to use the API if possible, but this works for me (not for your example post, which has been deleted, but for any active one):</p> <pre><code>#!/usr/bin/env python import mechanize import cookielib import urllib import logging import sys def main(): br = mechanize.Browser() cj = cookielib.LWPCookieJar() br.set_cookiejar(cj) br.set_handle_equiv(True) br.set_handle_gzip(True) br.set_handle_redirect(True) br.set_handle_referer(True) br.set_handle_robots(False) br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) r= br.open('http://www.reddit.com') # Select the second (index one) form br.select_form(nr=1) # User credentials br.form['user'] = 'user' br.form['passwd'] = 'passwd' # Login br.submit() # Open up comment page posting = 'http://www.reddit.com/r/PoopSandwiches/comments/f47f8/testing/' rval = 'PoopSandwiches' # you can get the rval in other ways, but this will work for testing r = br.open(posting) # You need the 'uh' value from the first form br.select_form(nr=0) uh = br.form['uh'] br.select_form(nr=7) thing_id = br.form['thing_id'] id = '#' + br.form.attrs['id'] # The id that gets posted is the form id with a '#' prepended. data = {'uh':uh, 'thing_id':thing_id, 'id':id, 'renderstyle':'html', 'r':rval, 'text':"Your text here!"} new_data_dict = dict((k, urllib.quote(v).replace('%20', '+')) for k, v in data.iteritems()) # not sure if the replace needs to happen, I did it anyway new_data = 'thing_id=%(thing_id)s&amp;text=%(text)s&amp;id=%(id)s&amp;r=%(r)s&amp;uh=%(uh)s&amp;renderstyle=%(renderstyle)s' %(new_data_dict) # not sure which of these headers are really needed, but it works with all # of them, so why not just include them. req = mechanize.Request('http://www.reddit.com/api/comment', new_data) req.add_header('Referer', posting) req.add_header('Accept', ' application/json, text/javascript, */*') req.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') req.add_header('X-Requested-With', 'XMLHttpRequest') cj.add_cookie_header(req) res = mechanize.urlopen(req) main() </code></pre> <p>It would be interesting to turn javascript off and see how the reddit comments are handled then. Right now there is a bunch of <code>magic</code> that happens in an onsubmit function called when making your post. This is where the <code>uh</code> and <code>id</code> value get added.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload