为pandas_datareader设置代理

时间:2018-12-27 13:43:40

标签: python-3.x proxy urllib pandas-datareader

系统:Win 7 x64上的Anaconda Python 3.6.4 x64

我正在尝试在代理后面使用pandas_datareader.data而不能够修改我的Windows PATH。

据我了解,pandas_datareader使用urllib.requests连接到各个服务提供商。默认情况下,urllib使用一个打开程序来尝试检测PATH代理设置: https://docs.python.org/3.5/howto/urllib2.html#proxies

我可以替换打开器以强制其使用我指定的代理设置,并且据我了解 ,这可以在全局级别上修改urllib的行为,而不仅限于给定实例

我的假设是,使用install_opener({...})应该会修改pandas_datareader的行为。但是,我看不到变化。

示例:

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)

有人可以指出我的错误吗?

1 个答案:

答案 0 :(得分:0)

这就是我的做法

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)
相关问题