上海证券交易所日数据概况爬虫

# from urllib import request
# # resp=request.urlopen("http://www.hkexnews.hk/sdw/search/mutualmarket_c.aspx?t=sh")
# # print(resp.read(10))
import requests
from lxml import etree
import json
import time
import datetime
headers={"Referer": "http://www.sse.com.cn/market/stockdata/overview/day/","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "sseMenuSpecial=8465; yfx_c_g_u_id_10000042=_ck18102916221510722963893155845; yfx_f_l_v_t_10000042=f_t_1540801335062__r_t_1540801335062__v_t_1540801335062__r_c_0; VISITED_MENU=%5B%228466%22%5D",
"Host": "www.sse.com.cn",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3551.3 Mobile Safari/537.36"
}
params={"jsonCallBack": "jsonpCallback18631",
"searchDate": "2018-10-24",
"prodType": "gp",
"_":"1540803198930"}
def down_trade(startime,etime):
stime = datetime.datetime.strptime(startime, "%Y-%m-%d")
etime = datetime.datetime.strptime(etime, "%Y-%m-%d")
while stime<=etime:
stime+=datetime.timedelta(days=1)
riqi=stime.strftime("%Y-%m-%d")
print("开始处理日期:"+riqi)
shanhaicodeavg(riqi)

def shanhaicodeavg(day):
url="http://query.sse.com.cn/marketdata/tradedata/queryTradingByProdTypeData.do?jsonCallBack=jsonpCallback44691&searchDate="+day+"&prodType=gp&_=1540803198932"
response=requests.get(url,headers=headers)
response.encoding="utf-8"
text=response.text
#print(text)
jsontext1=text.strip('jsonpCallback44691')#最终结果json格式
jsontext=jsontext1.strip('(').strip(')')
fh = open(day+".txt", 'w', encoding='utf-8')
fh.write(jsontext)
fh.close()
if __name__ == "__main__":
down_trade("2000-01-01","2018-10-29")
print('ok')

转载于:https://www.cnblogs.com/guoshanyun/p/9883190.html

你可能感兴趣的:(上海证券交易所日数据概况爬虫)