2012-10-23 22 views
1

我正在編寫python程序以編程方式登錄到網站。現在我在處理cookie方面遇到問題。下面是我的代碼:如何使用python機械化設置Referer?

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' 

看起來像我不跟「引薦」處理正確。任何想法?

回答

1

你可以嘗試創建一個像這樣的揭幕戰?

opener = mechanize.build_opener(
    mechanize.HTTPCookieProcessor(cj), 
    mechanize.HTTPRefererProcessor, 
    mechanize.HTTPEquivProcessor, 
    mechanize.HTTPRefreshProcessor) 
2
# 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')] 
+0

的文檔,但是,說這個'mechanize.Browser'自動執行此操作。 – 0xC0000022L