# -*- coding:utf-8 -*-
import urllib
import urllib2
import re
import hashlib
import cookielib
import webbrowser
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
loginurl = 'https://passport.csdn.net/account/login'
loginheaders = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
#'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'zh-CN,zh;q=0.8',
'Host':'my.csdn.net',
'Proxy-Connection':'keep-alive',
'Referer':'http://passport.csdn.net/account/login?from=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn',
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36',
}
result = urllib2.urlopen(url = loginurl)
content = result.read().encode('UTF-8')
print content
pattern = re.compile('name="lt" value="(.*?)"')
match = re.search(pattern,content)
lt_code = match.group(1)
print lt_code
pattern = re.compile('name="execution" value="(.*?)"')
match = re.search(pattern,content)
execution_code = match.group(1)
print execution_code
postdata = urllib.urlencode({
'username':'这里是我的账号~',
'password':'这里是我的密码~',
'rememberMe':'true',
'lt':lt_code,
'execution':execution_code,
'eventId':'submit',
#'l':'登 录',
})
req = urllib2.Request(url = 'https://passport.csdn.net/account/login',data = postdata,headers = loginheaders)
result = opener.open(req)
content = result.read().encode('UTF-8')
print content
运行显示出错如下,求大神解答一下,困扰很久了,多谢了
LT-44346-WUopvHYSgK0LvIjP3SuwayBKWaisla
e1s1
Traceback (most recent call last):
File "D:\Program Files (x86)\eclipse-java-luna-SR1a-win32-x86_64\eclipse\workspace\csdn\src\csdn.py", line 56, in
result = opener.open(req)
File "D:\python2.7\lib\urllib2.py", line 437, in open
response = meth(req, response)
File "D:\python2.7\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python2.7\lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File "D:\python2.7\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "D:\python2.7\lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
特别希望有大神指出我程序问题所在,因为在学习python路上,在这个简单的地方被困扰太久了,有极大的挫败感Orz