获取带接口的股票数据
Posted tan2810
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取带接口的股票数据相关的知识,希望对你有一定的参考价值。
mport
urllib.request
import
re
#获取股票代码
def
codeTolist():
codeList
=
[]
html
=
urllib.request.urlopen(codeUrl).read()
html
=
html.decode(
‘utf-8‘
)
s
=
‘<a href="https://hq.gucheng.com/(SZ|SH)(.*?)/">‘
p
=
re.
compile
(s)
data
=
p.findall(html)
for
code
in
data:
codeList.append(code[
1
])
return
codeList
allCodeList
=
codeTolist()
#下载股票数据
def
allDown():
t
=
len
(allCodeList)
for
code
in
allCodeList:
print
(f
‘正在获取{code}股票数据‘
)
if
code[
0
]
=
=
‘6‘
:
Url
=
‘http://quotes.money.163.com/service/chddata.html?code=0‘
+
code
+
‘&start=19981218&end=20191213&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP‘
else
:
Url
=
‘http://quotes.money.163.com/service/chddata.html?code=1‘
+
code
+
‘&start=19981218&end=20191213&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP‘
urllib.request.urlretrieve(Url,
‘d:\gupiao\‘+code+‘
.csv‘)
t
=
t
-
1
if
t
=
=
0
:
print
(
‘所有股票下载完毕!‘
)
allDown()
以上是关于获取带接口的股票数据的主要内容,如果未能解决你的问题,请参考以下文章