user1613263 user1613263 - 8 months ago 84
Python Question

why I can't log in this web with python

This is my python codeļ¼ŽI want to log in this website with python, but it can't come true.

import sys, time, os, re
import urllib, urllib2, cookielib

loginurl = 'https://auth.dxy.cn/login?null'
#loginurl = 'https://www.douban.com/accounts/login'

cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)

params = {
'username' : 'XXXX',
'password' : 'XXXX',
'lt' : '_c573C07C4-AD13-CD25-85E9-CC917AF0433F_k42B35CAA-C2E7-7B2C-A7A1-9FD4E6CBE30F',
'_eventId' : 'submit'
}

req = urllib2.Request(loginurl,urllib.urlencode(params))
res = urllib2.urlopen(req).read()
m = re.search(r'\s+(.+?)\s+',str(cookie))
jsession=re.sub(r'\s+', '', m.group(0))
#print jsession

header={'Cookie':jsession}
#response=opener.open(req).read()
req1 = urllib2.Request(loginurl,urllib.urlencode(params),header)
res1 = urllib2.urlopen(req1)
print res1.geturl()

response1=opener.open(res1.geturl())
print response1.read()

Answer Source

Ive had similar problems before because of user-agent issues. Try log in normally (via your browser of choice) and take a look at whatever header info is sent. Try mimic that in your script.

If you don't know how to set the headers check this out: How do I add a header to urllib2 opener?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download