我正在为python 2.6中的Hulu开发一个简单的HTML刮板,并且登录到我的帐户时遇到问题。这里是我到目前为止的代码:Python中的Cookie问题
import urllib
import urllib2
from cookielib import CookieJar
#make a cookie and redirect handlers
cookies = CookieJar()
cookie_handler= urllib2.HTTPCookieProcessor(cookies)
redirect_handler= urllib2.HTTPRedirectHandler()
opener = urllib2.build_opener(redirect_handler,cookie_handler)#make opener w/ handlers
#build the url
login_info = {'username':USER,'password':PASS}#USER and PASS are defined
data = urllib.urlencode(login_info)
req = urllib2.Request("http://www.hulu.com/account/authenticate",data)#make the request
test = opener.open(req) #open the page
print test.read() #print html results
代码编译和运行,但所有的打印是:
Login.onError("Please \074a href=\"/support/login_faq#cant_login\"\076enable cookies\074/a\076 and try again.");
我假设有在我如何处理Cookie一些错误,但就是”似乎发现它。我听说Mechanize对于这种类型的程序来说是一个非常有用的模块,但由于这似乎是唯一的减速带,所以我希望找到我的错误。