诚心求教这段python登录网站的代码哪里错误了?

刘地 发布于 2012/09/22 15:57
阅读 860
收藏 0
www.zhuike.net是一个盗贴网站,因为界面很简洁,所以我使用它尝试用python登录并且打开书架,但是每次执行都失败,返回告诉我“进入无限循环”

代码如下:

def main():
    import urllib2,cookielib,sys
    cookie_support=urllib2.HTTPCookieProcessor(cookielib.CookieJar())
    opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
    #???header?????
    header={}
    header['User-Agent']='Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)'
    header['Accept-Language']='zh-CN,zh;q=0.8'
    header['Accept-Charset']='utf-8;q=0.7,*;q=0.7'
    header['connection']='keep-alive'
    try:
        #?????
        header['Accept']='*/*'
        header['Host']='www.zhuike.net'
        req=urllib2.Request('http://www.zhuike.net/',headers=header)
        opener.open(req)
        #??????
        header['Referer']='http://www.zhuike.net/'
        header['Accept']='image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*'
        req=urllib2.Request('http://www.zhuike.net/login.html',headers=header)
        opener.open(req)
        #????
        header['Referer']='http://www.zhuike.net/login.html'
        namekey=urllib.urlencode({'un':'snake117','auto':'1','p':'47B32797DCFDF1BFD2882E8E121C17B3'})
        req=urllib2.Request('http://www.zhuike.net/u/login.html',data=namekey,headers=header)
        opener.open(req)
        #?????
        header['Accept']='*/*'
        req=urllib2.Request('http://book.zhuike.net/u/',headers=header)
        data=opener.open(req).read()
        print data
    except urllib2.HTTPError,e:
        print 'First Error : %s'%(e)
        sys.exit()

if __name__ == '__main__':
    main()

加载中
0
Jooooooker
Jooooooker
能否把错误信息一起贴上来呢?
0
刘地
刘地

引用来自“Jooooooker”的答案

能否把错误信息一起贴上来呢?
> "C:\Program Files\Python\pythonw.exe" -u "E:\Program\zhuike.py" 
First Error : HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Permanently

用的 Ulipad 谢谢
0
Jooooooker
Jooooooker

这看起来应该是服务器的问题吧 

The HTTP server returned a redirect error that would lead to an infinite loop.

把自己重定向到自己 就会产生无限循环了

而且我看了一下它的登录 是往

http://book.zhuike.net/u/login.html

而不是

http://www.zhuike.net/u/login.html

你先改这个再看看吧

返回顶部
顶部