You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.5 KiB
66 lines
2.5 KiB
5 months ago
|
import os
|
||
|
|
||
|
import django
|
||
|
|
||
|
# 设置DJANGO_SETTINGS_MODULE环境变量
|
||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_Bootstrap.settings') # 将'test_Bootstrap'替换为你的项目名称
|
||
|
|
||
|
# 初始化Django设置
|
||
|
django.setup()
|
||
|
from app_test.user_agents_pool import agent_list
|
||
|
# 使用绝对导入来导入模型
|
||
|
from app_test.models import StockData
|
||
|
import pandas as pd
|
||
|
import requests
|
||
|
import time
|
||
|
import random
|
||
|
import re
|
||
|
from datetime import date
|
||
|
# from .models import StockData
|
||
|
|
||
|
class GetYaoqiGupiao(object):
|
||
|
user_Agent = random.choice(agent_list)
|
||
|
def __init__(self):
|
||
|
self.headers = {
|
||
|
'user-agent': self.user_Agent
|
||
|
}
|
||
|
def get_content(self,id,start_time,end_time):
|
||
|
url = f'https://q.stock.sohu.com/hisHq?code=cn_{id}&start={start_time}&end={end_time}&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp&r=0.44942133747295787&0.3001773339487177'
|
||
|
data_all = []
|
||
|
# 爬取医药股票历史数据
|
||
|
res = requests.get(url, headers=self.headers)
|
||
|
# 设置3秒休眠时间
|
||
|
time.sleep(3)
|
||
|
html = res.content.decode("gb2312")
|
||
|
html = html.replace('historySearchHandler', '')
|
||
|
info_list = re.findall(r'\["202\d+.*?"\]', html, re.S)
|
||
|
datas = []
|
||
|
for info in info_list:
|
||
|
number_list = re.findall(r'"(.*?\d+.*?)"',info,re.S)
|
||
|
date = number_list[0]
|
||
|
shoupan = number_list[1]
|
||
|
datas.append([date, float(shoupan)])
|
||
|
df = pd.DataFrame(datas, columns=['date', '收盘'])
|
||
|
return df
|
||
|
def get_stock_data():
|
||
|
# 选用两只和布洛芬相关的股票数据
|
||
|
id_list = {'301075':'多瑞医药','301211':'亨迪药业'}
|
||
|
today = str(date.today()).replace('-', '')
|
||
|
getyaoqigupiao = GetYaoqiGupiao()
|
||
|
df1 = getyaoqigupiao.get_content('301075','20230201',today)
|
||
|
df2 = getyaoqigupiao.get_content('301211','20230201',today)
|
||
|
merged_df = df1.merge(df2,on = 'date',how = 'inner')#取交集
|
||
|
merged_df['stock_data'] = (merged_df['收盘_x']+merged_df['收盘_y'])/2
|
||
|
data = merged_df[['date','stock_data']]
|
||
|
data_list = data.values.tolist()
|
||
|
data_list = list(reversed(data_list))
|
||
|
print(data_list)
|
||
|
for data in data_list:
|
||
|
obj, created =StockData.objects.get_or_create(date=data[0], defaults={'shoupan': data[1]})
|
||
|
if created:
|
||
|
print(f"Added new record for date {data[0]} with infections {data[1]}")
|
||
|
else:
|
||
|
print(f"Record for date {data[0]} already exists.")
|
||
|
|
||
|
# get_stock_data()
|