繁体   English   中英

为 pandas_datareader 设置代理

[英]Setting a proxy for pandas_datareader

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

我试图在代理后面使用 pandas_datareader.data 而无法修改我的 Windows PATH。

据我了解,pandas_datareader 使用 urllib.requests 连接到各自的服务提供商。 默认情况下,urllib 使用一个开启器来尝试检测 PATH-proxysettings: https ://docs.python.org/3.5/howto/urllib2.html#proxies

我可以替换 opener 以强制它使用我指定的代理设置,据我所知,这会在全局级别上修改 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)

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

这就是我做到的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM