Here are the examples of the python api urllib3.Request taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
3 Examples
0
View Complete Implementation : DyStockDataGateway.py
Copyright MIT License
Author : moyuanz
Copyright MIT License
Author : moyuanz
def _getDaysFrom163(self, code, startDate, endDate, retry_count=3, pause=0.001):
"""
从网易获取个股日线数据,指数和基金(ETF)除外
@code: DevilYuan Code
"""
symbol = ('0' + code[:6]) if code[-2:] == 'SH' else ('1' + code[:6])
for _ in range(retry_count):
sleep(pause)
try:
url = 'http://quotes.money.163.com/service/chddata.html?code={}&start={}&end={}&fields=TCLOSE;HIGH;LOW;TOPEN;TURNOVER;VOTURNOVER;VATURNOVER'
url = url.format(symbol, startDate.replace('-', ''), endDate.replace('-', ''))
re = Request(url)
lines = urlopen(re, timeout=10).read()
lines = lines.decode('GBK')
df = pd.read_table(StringIO(lines),
sep=',',
names=['date', 'code', 'name', 'close', 'high', 'low', 'open', 'turnover', 'volume', 'amount'],
skiprows=[0])
except Exception as e:
print(e)
ex = e
else:
df = df[['date', 'open', 'high', 'close', 'low', 'volume', 'amount', 'turnover']] # return columns
df = df.set_index('date')
df = df.sort_index(ascending=False)
return df
raise ex
0
View Complete Implementation : DyStockDataTicksGateway.py
Copyright MIT License
Author : moyuanz
Copyright MIT License
Author : moyuanz
def _getTickDataFromTencent(code=None, date=None, retry=3, pause=0.001):
"""
从腾讯获取分笔数据
接口和返回的DF,保持跟tushare一致
Parameters
------
code:string
股票代码 e.g. 600848.SH
date:string
日期 format:YYYY-MM-DD
retry : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame 当日所有股票交易数据(DataFrame)
属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
"""
print('从腾讯获取[{}, {}]的分笔数据...'.format(code, date))
if code is None or len(code)!=9 or date is None:
return None
code = code[:-3]
symbol = DyStockDataTicksGateway._codeToTencentSymbol(code)
yyyy, mm, dd = date.split('-')
for _ in range(retry):
sleep(pause)
try:
re = Request('http://stock.gtimg.cn/data/index.php?appn=detail&action=download&c={0}&d={1}'.format(symbol, yyyy+mm+dd))
lines = urlopen(re, timeout=10).read()
lines = lines.decode('GBK')
df = pd.read_table(StringIO(lines), names=['time', 'price', 'change', 'volume', 'amount', 'type'],
skiprows=[0])
except Exception as e:
print(e)
ex = e
else:
return df
raise ex
0
View Complete Implementation : DyStockDataTicksGateway.py
Copyright MIT License
Author : moyuanz
Copyright MIT License
Author : moyuanz
def _getTickDataFromSina(code=None, date=None, retry=3, pause=0.001):
"""
获取分笔数据
Parameters
------
code:string
股票代码 e.g. 600848
date:string
日期 format:YYYY-MM-DD
retry : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame 当日所有股票交易数据(DataFrame)
属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
"""
if code is None or len(code)!=9 or date is None:
return None
code = code[:-3]
symbol = DyStockDataTicksGateway._codeToSinaSymbol(code)
for _ in range(retry):
sleep(pause)
try:
re = Request('http://market.finance.sina.com.cn/downxls.php?date={}&symbol={}'.format(date, symbol))
lines = urlopen(re, timeout=10).read()
lines = lines.decode('GBK')
df = pd.read_table(StringIO(lines), names=['time', 'price', 'change', 'volume', 'amount', 'type'],
skiprows=[0])
except Exception as e:
print(e)
ex = e
else:
return df
raise ex