python 3 Login form on webpage with urllib and cookiejar

删除回忆录丶 提交于 2019-12-21 21:25:10

问题


I've been trying to make a python script login to my reddit account but it doesnt seem to work, could anybody tell me whats wrong with my code? It runs fine it just doesnt login.¨

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:


The name attribute of the field is sent, not the id:

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


来源:https://stackoverflow.com/questions/18949516/python-3-login-form-on-webpage-with-urllib-and-cookiejar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!