1
我知道类似的问题已经被问到,我已经阅读过。我还阅读了我能找到的大多数其他相关文章。我已经尝试httplib2,标题修改,以及任何我可以找到或想到的,但我无法获得此登录脚本工作。Python登录脚本
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()
首先,这不是我的脚本,但我修改了一些。其次,我认为这与cookie的处理方式有关,但我无法弄清楚。我也替换了用户名密码组合。
“我不能让这个登录脚本的工作”。你也许应该定义**正在做什么以及为什么你不喜欢那样做。它是否有语法错误?它是否从Web服务器返回401错误?你看到了什么结果? – 2010-12-01 17:53:42