1

我正在编写 python 程序以编程方式登录网站。现在我在处理 cookie 时遇到了问题。以下是我的代码:

import mechanize
cj = mechanize.CookieJar()
browser = mechanize.Browser()
browser.set_cookiejar(cj)
opener = mechanize.build_opener(*browser.handlers)

它给了我这样的错误:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-  packages/django/core/handlers/base.py", line 111, in get_response

response = callback(request, *callback_args, **callback_kwargs)
File "/home/test/testEnv/apps/arachni/views.py", line 983, in handle_request
content = opener.open(req, data).read()
File "build/bdist.linux-i686/egg/mechanize/_opener.py", line 188, in open
req = meth(req)
File "build/bdist.linux-i686/egg/mechanize/_http.py", line 316, in http_request
self.rfp.read()
File "build/bdist.linux-i686/egg/mechanize/_http.py", line 242, in read
f = self._opener.open(req)
File "build/bdist.linux-i686/egg/mechanize/_opener.py", line 204, in open
response = meth(req, response)
File "build/bdist.linux-i686/egg/mechanize/_urllib2_fork.py", line 457, in http_response
'http', request, response, code, msg, hdrs)
File "build/bdist.linux-i686/egg/mechanize/_opener.py", line 221, in error
result = apply(self._call_chain, args)
File "build/bdist.linux-i686/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
result = func(*args)
File "build/bdist.linux-i686/egg/mechanize/_urllib2_fork.py", line 571, in http_error_302
return self.parent.open(new)
File "build/bdist.linux-i686/egg/mechanize/_opener.py", line 188, in open
req = meth(req)
File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 71, in http_request
request = self.parent._add_referer_header(
AttributeError: OpenerDirector instance has no attribute '_add_referer_header'

好像我没有正确处理“推荐人”。任何想法?

4

2 回答 2

3
# Browser
br = mechanize.Browser()
br.set_handle_referer(False)    # allow everything to be written to
br.set_handle_robots(False)   # no robots
br.set_handle_refresh(True)  # can sometimes hang without this
br.set_handle_redirect(True)
br.addheaders = [('User-Agent', 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile myTouch 3G Slide Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'),
                    ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
                    ('Accept-Language', 'en-gb,en;q=0.5'),
                    ('Accept-Encoding', 'gzip,deflate'),
                    ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
                    ('Keep-Alive', '115'),
                    ('Connection', 'keep-alive'),
                    ('Cache-Control', 'max-age=0'),
                    ('Referer', 'http://yahoo.com')]
于 2012-10-24T00:01:38.587 回答
1

您可以尝试创建这样的开瓶器吗?

opener = mechanize.build_opener(
    mechanize.HTTPCookieProcessor(cj),
    mechanize.HTTPRefererProcessor,
    mechanize.HTTPEquivProcessor,
    mechanize.HTTPRefreshProcessor)
于 2012-10-23T22:40:54.713 回答