如何使用代理与Robobrowser
我正在使用 http://robobrowser.readthedocs.org/en/latest /readme.html ,(一个基于美丽的汤和请求库的新的python库)在django内。我的django应用包含:
I'm working with http://robobrowser.readthedocs.org/en/latest/readme.html, (a new python library based on the beautiful soup and request libraries) within django. My django app contains :
def index(request):
p=str(request.POST.get('p', False)) # p='https://www.yahoo.com/'
pr="http://10.10.1.10:3128/"
setProxy(pr)
browser = RoboBrowser(history=True)
postedmessage = browser.open(p)
return HttpResponse(postedmessage)
我想为代码添加一个代理,但是在文档中找不到参考如何做到这一点。是否可以这样做?
I would like to add a proxy to my code but can't find a reference in the docs on how to do this. Is it possible to do this?
编辑:
按照您的建议,我将代码更改为
following your recommendation I've changed the code to
pr="http://10.10.1.10:3128/"
setProxy(pr)
browser = RoboBrowser(history=True)
with:
def setProxy(pr):
import os
os.environ['HTTP_PROXY'] = pr
return
我现在得到:
Django Version: 1.6.4
Exception Type: LocationParseError
Exception Value:
Failed to parse: Failed to parse: 10.10.1.10:3128
下一步做什么想法?我找不到这个错误的引用
Any ideas on what to do next? I can't find a reference to this error
由于RoboBrowser使用请求库,您可以尝试将代理设置为在请求文档中通过设置环境变量 HTTP_PROXY
和 HTTPS_PROXY
。
Since RoboBrowser uses the request library, you can try to set the proxies as mentioned in the request docs by setting the environment variables HTTP_PROXY
and HTTPS_PROXY
.