Python爬取上交所一年大盘数据

  • 2019 年 12 月 17 日
  • 筆記

这次我们用requests、json来爬取上交所A股大盘的数据,并以收盘价为坐标画出2018-2019年的走势。要爬取的界面如下:

具体爬虫代码如下:

import json  import requests  import matplotlib.pyplot as plt  #爬取程序  def load_sse():   url='http://yunhq.sse.com.cn:32041/v1/sh1/dayk/000001?callback=jQuery111208282462776376776_1569062885488&select=date%2Copen%2Chigh%2Clow%2Cclose%2Cvolume&begin=-300&end=-1&_=1569062885522'   response=requests.get(url,headers={'Referer': 'http://www.sse.com.cn/market/price/trends/'})   json_str=response.text[42:-1]   data=json.loads(json_str)   return data['kline']  #画图  def draw(data):   date=[x[0] for x in data]   close=[x[4] for x in data]   #对x轴进行处理   index=range(len(date))   limit=90   plt.xticks(index[::limit],date[::limit])   #画图   plt.plot(index,close,'red')  if __name__=='__main__':   kline=load_sse()   draw(kline)

画图如下:

好了,See you!这只是用收盘价格画出来的折线