2011-03-28 34 views
0

这是我的代码登录网站使用python:为什么我不能在POST方法使用python登录这个网站

import urllib2, cookielib 
cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar()) 
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) 
urllib2.install_opener(opener) 
content = urllib2.urlopen('http://192.168.1.200/order/index.php?op=Login&ac=login&userName=%E8%B5%B5%E6%B1%9F%E6%98%8E&userPwd=123').read() 

print content 

它显示:

{"title":"login error","body":"username or password error","data":{"status":1}} 

,但用户名和密码是正确的,我可以登录使用Firefox的这个网站,

所以我能做些什么,

感谢

回答

3

你正在一个GET请求。为了使POST请求,使用方法:如果数据(第二个参数)被传递给它

content = urllib2.urlopen(
    'http://192.168.1.200/order/index.php", 
    'op=Login&ac=login&userName=%E8%B5%B5%E6%B1%9F%E6%98%8E&userPwd=123').read() 

urlopen方法发送POST请求。

0

尝试使用密码管理器:

here

# create a password manager 
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 

# Add the username and password. 
# If we knew the realm, we could use it instead of ``None``. 
top_level_url = "http://example.com/foo/" 
password_mgr.add_password(None, top_level_url, username, password) 

handler = urllib2.HTTPBasicAuthHandler(password_mgr) 

# create "opener" (OpenerDirector instance) 
opener = urllib2.build_opener(handler) 

# use the opener to fetch a URL 
opener.open(a_url) 

# Install the opener. 
# Now all calls to urllib2.urlopen use our opener. 
urllib2.install_opener(opener) 
相关问题