python - 如何使用 python mechanize 设置 Referer?

标签 python mechanize referer

我正在编写 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'

似乎我没有正确处理“推荐人”。有什么想法吗?

最佳答案

# 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')]

关于python - 如何使用 python mechanize 设置 Referer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13040341/

相关文章:

ruby - 通过代理错误使用 SSL 进行 Mechanize

php - 如何仅允许白名单网站嵌入 iframe?

node.js - 使用 Node.js http GET 请求发送浏览器、referer 和 cookie

python - 如何使用 OpenCV 检测和跟踪人员?

python - 无法从 python 子进程 : permission denied 执行 shell 脚本

python - ALLOWED_HOSTS 和 Django

用于抓取 html 输出的 Python 脚本有时有效,有时无效

python - 使用 Python 无浏览器访问 LinkedIn

jquery - 使用ajax请求设置referer url

python - 如何在不维护 Python 中的目录结构的情况下从 zip 中提取文件?