带有urllib和cookiejar的网页上的python 3登录表单 [英] python 3 Login form on webpage with urllib and cookiejar

查看:120
本文介绍了带有urllib和cookiejar的网页上的python 3登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使python脚本登录到我的reddit帐户,但是它似乎不起作用,有人可以告诉我我的代码有什么问题吗?它运行正常,只是没有登录.¨

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()

推荐答案

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

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

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

这篇关于带有urllib和cookiejar的网页上的python 3登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆