用python写了一个csdn的模拟登录程序,运行失败了

dystopiaz 2015-09-09 11:21:53
# -*- coding:utf-8 -*-
import sys
from fileinput import filename
reload(sys)
sys.setdefaultencoding('utf-8')
# -*- 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

显示在req这里出了问题,求大神解答一下,多谢了
引用
LT-2250024-Rsx6xFCktUTcidVsWEilzjawsdbpbX
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 <module>
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
...全文
113 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧