[英]Setting a proxy for pandas_datareader
System: Anaconda Python 3.6.4 x64 on Win 7 x64系统:Win 7 x64 上的 Anaconda Python 3.6.4 x64
I am trying to use pandas_datareader.data behind a proxy without being able to modify my Windows PATH.我试图在代理后面使用 pandas_datareader.data 而无法修改我的 Windows PATH。
As far as I understand, pandas_datareader uses urllib.requests to connect to the respective service providers.据我了解,pandas_datareader 使用 urllib.requests 连接到各自的服务提供商。 By default, urllib uses an opener which tries to detect the PATH-proxysettings: https://docs.python.org/3.5/howto/urllib2.html#proxies
默认情况下,urllib 使用一个开启器来尝试检测 PATH-proxysettings: https ://docs.python.org/3.5/howto/urllib2.html#proxies
I can replace the opener to force it to use my specified proxy settings, and as far as I understand , this modifies the behavior of the urllib on a global level, not just for a given instance.我可以替换 opener 以强制它使用我指定的代理设置,据我所知,这会在全局级别上修改 urllib 的行为,而不仅仅是针对给定的实例。
My assumption is that using install_opener({...}) should modify the behavior of pandas_datareader.我的假设是使用 install_opener({...}) 应该修改 pandas_datareader 的行为。 However, I don't see a change.
但是,我没有看到任何变化。
Example:例子:
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import urllib
style.use('ggplot')
start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
# this fails, since no proxy is set
df = web.DataReader("TSLA", 'morningstar', start, end)
# install proxies to opener
proxies = {'http' : 'http://...',
'https': 'https://...'}
proxy_support = urllib.request.ProxyHandler(proxies)
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
# this also fails, why?
df = web.DataReader("TSLA", 'morningstar', start, end)
Could anyone please point me to my mistake?有人可以指出我的错误吗?
This is how i did it这就是我做到的
proxies = {'http': 'http:your proxy:8080'}
headers = { "Accept":"application/json",
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
"Accept-Encoding":"none",
"Accept-Language":"en-US,en;q = 0.8",
"Connection":"keep-alive",
"Referer":"https://cssspritegenerator.com",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML,
like Gecko) Chrome/23.0.1271.64 Safari/537.11"
}
with requests.Session() as s:
s.headers = headers
s.proxies.update(proxies)
gspc = web.DataReader('^GSPC', 'yahoo', start, end, session=s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.