在python中,获取免费股市行情的方法有很多,其中用request库是最简单的一种。示例代码如下:
import requests
stock_code="301307"
if (stock_code[:1] == '6'): # 上交所
exchange = "sh"
elif (stock_code[:1] == '3'): # 深交所
exchange = "sz"
elif (stock_code[:1] == '0'): # 深交所
exchange = "sz"
else: # reserved for 北交所
exchange = ""
# endif
url = f"https://qt.gtimg.cn/q=" + exchange + stock_code
data = requests.get(url)
if (data.status_code==200):
print(data.text)
#endif
exit(0)
输出结果如下:
v_sz301307="51~美利信~301307~22.39~22.46~22.50~47539~23012~24527~22.38~62~22.37~39~22.36~15~22.35~35~22.33~3~22.39~127~22.40~156~22.41~120~22.42~34~22.43~40~~20241220161457~-0.07~-0.31~22.64~22.24~22.39/47539/106570651~47539~10657~4.36~-90.02~~22.64~22.24~1.78~24.40~47.15~1.48~26.95~17.97~1.16~-323~22.42~-73.78~34.70~~~2.17~10657.0651~0.0000~0~ AR~GP-A-CYB~-35.23~-4.52~0.54~-1.64~-0.79~35.05~15.91~-7.78~-4.93~27.07~108991640~210600000~-51.19~-26.20~108991640~~~-34.19~0.09~~CNY~0~~22.45~-102";
这里只是一个举例,stock_code你随便换成你要的即可。