import requests
from lxml.etree import HTML
import os
import re
import json
import redis
import time
now = int(time.time())
timeArray = time.localtime(now)
db = redis.Redis(host='127.0.0.1',port=6379,decode_responses=False)
headers = {
'user - agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36",
}
def reques(url,code):
res = requests.get(url,headers=headers).text
res = json.loads(res)
content = json.loads(res['content'])
content_list = content['vSecQuote']
dic={}
for li in content_list:
dic['fopen'] = round(li['fOpen'],2) # 今天开盘价
dic['year_fclose'] = round(li['fClose'],2) # 昨天收盘价
dic['tady_fnow'] = round(li['fNow'],2) # 今天收盘价
dic['fvolumeratio'] = round(li['fVolumeRatio'],2) # 量比
dic['fFhsl'] = round((li['fFhsl']*100),2) # 换手率
dic['lvolume'] = li['lVolume'] # 成交量
dic['famout'] = li['fAmout'] # 成交额
dic['fmax'] = li['fMax'] # 最高价
dic['fmin'] = li['fMin'] # 最低价
dic['fminlimit'] = li['fMinLimit'] # 跌停价
# dic['otherStyleTime'] = '2021-06-04'
dic['otherStyleTime'] = time.strftime("%Y-%m-%d", timeArray) # 时间
item = json.dumps(dic)
print(item)
db.sadd(code,item)
if __name__ == '__main__':
with open('./所有股票的列表与代码.txt','r',encoding='utf8') as f:
content_code = f.readlines()
for i in content_code:
code = str(i.split('\t')[-1]).strip()[2:]
url = f'https://sec.gushi.com/getSecInfo?action=quote&seccode=0101{code}'
reques(url,code)