Python登录脚本

Python登录脚本,python,login,Python,Login,我知道有人问过类似的问题,我读过。我还阅读了我能找到的大多数其他相关文章。我已经尝试过httplib2、头修改,以及我能找到或想到的任何东西,但是我无法让这个登录脚本工作 import cookielib import urllib import urllib2 cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) resp = opener.open('http://

我知道有人问过类似的问题,我读过。我还阅读了我能找到的大多数其他相关文章。我已经尝试过httplib2、头修改,以及我能找到或想到的任何东西,但是我无法让这个登录脚本工作

import cookielib
import urllib
import urllib2

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open('http://www.biocidecity.com')

theurl = 'http://www.biocidecity.com/index.php'
body={'username':'someusername','password':'somepassword', 'login' : '1'}
txdata = urllib.urlencode(body) txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}


try:
    req = urllib2.Request(theurl, txdata, txheaders)
    handle = opener.open(req) 
    HTMLSource = handle.read()
    f = file('test.html', 'w')
    f.write(HTMLSource)
    f.close()

except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    elif hasattr(e, 'reason'):
        print "The error object has the following 'reason' attribute :", e.reason
        print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
        sys.exit()

else:
    print 'Here are the headers of the page :'
    print handle.info() 

首先,这不是我的脚本,但我已经修改了一些。第二,我认为这与处理饼干的方式有关,但我不明白。我还替换了用户名密码组合。

我想这也是服务条款,但您应该试试

import cookielib
import urllib
import urllib2

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open('http://www.biocidecity.com')

theurl = 'http://www.biocidecity.com/index.php'
body={'username':'someusername','password':'somepassword', 'login' : '1'}
txdata = urllib.urlencode(body) txheaders =  {'Referer': 'http://www.biocidecity.com/index.php'}


try:
    req = urllib2.Request(theurl, txdata, txheaders)
    handle = opener.open(req) 
    HTMLSource = handle.read()
    f = file('test.html', 'w')
    f.write(HTMLSource)
    f.close()

except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    elif hasattr(e, 'reason'):
        print "The error object has the following 'reason' attribute :", e.reason
        print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
        sys.exit()

else:
    print 'Here are the headers of the page :'
    print handle.info() 

“我无法使此登录脚本正常工作”。你也许应该定义它在做什么以及为什么你不喜欢它。它有语法错误吗?它是否从web服务器返回401错误?你看到的结果是什么?我的错误,当我在Wireshark中查看交互时,服务器只是返回我发布到的同一个登录页面(即,我没有被重定向)。除此之外,没有错误,什么都没有。我能找到的唯一区别是,当我在Chrome中提交表单时,302响应包在http头中包含cookie信息,而当我使用脚本时,它不包含cookie信息。。。嗯,也许我应该改写我的问题。“也许我应该改写我的问题”。绝对地请更新问题,使其非常清楚您遇到了什么问题。