开发者

Programatically posting a comment in Reddit using Python

I'm using the httplib2 library located here

So far my code is this:

    http = httplib2.Http()
    url= 'http://www.reddit.com/api/login'
    body= {'user' : 'DUMMYUSERNAME', 'passwd': 'DUMMYPASSWORD'}
    headers = {'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    response, content= http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))
    headers = {'Cookie': response['set-cookie']}

I could be mistaken, but I believe the above code successfully logs me into reddit.com (how would I verify this?)

The next step is sending the comment. I took a look at the network tab in Firebug and it appears the form requires several variables:

id  #form-t3_f3oj23a4
r   learnprogramming
renderstyle html
text    testing send data
thing_id    t3_f3oj2
uh  6qsck0zhpa2585dac6a0ab49250cbceca76开发者_StackOverflowfb36df883320896

But I'm not sure

  1. How to programatically find the requisite post data for each different page. (How do I find id, thing_id, and uh for each page?)
  2. Send that data to reddit.

Would it be something like this?

data= dict(thing_id="t3_f3oj2", text="testing", id="#form-t3_f3oj2ikj", r="learnprogramming", uh="sz4n7idqmc22bdeec21d7f2ca3e9408b102160646e2fcbbff4", renderstyle="html")
resp, content = h.request("http://www.reddit.com/r/blahblahsubreddit/comments/f3oj2/blahblahpost/", "POST", urllib.urlencode(data))

Am I using the appropriate libraries? Thanks.


I'd suggest taking at look at the Reddit API:

http://code.reddit.com/wiki/API

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜