python 3使用urllib和cookiejar在网页上登录表单

时间:2013-09-22 22:41:00

标签: python login request urllib cookiejar

我一直试图让python脚本登录到我的reddit帐户,但它似乎没有用,有人能告诉我我的代码有什么问题吗?它运行正常,只是没有登录。

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
authentication_url = 'https://ssl.reddit.com/post/login'
payload = {
    'op': 'login',
    'user_name': 'username',
    'user_pass': 'password'
}
data = urllib.parse.urlencode(payload)
binary_data = data.encode('UTF-8')
req = urllib.request.Request(authentication_url, binary_data)
resp = urllib.request.urlopen(req)
contents = resp.read()

1 个答案:

答案 0 :(得分:1)

发送字段的name属性,而不是id

payload = {
    'op': 'login',
    'user': 'username',
    'passwd': 'password'
}