提交 22b065ed 作者: LiuLiYuan

纳斯达克财务数据 10/07

上级 afe226ba
...@@ -11,7 +11,6 @@ from bs4 import BeautifulSoup ...@@ -11,7 +11,6 @@ from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from requests.packages import urllib3 from requests.packages import urllib3
from retry import retry from retry import retry
from base import BaseCore from base import BaseCore
urllib3.disable_warnings() urllib3.disable_warnings()
...@@ -20,6 +19,7 @@ log = baseCore.getLogger() ...@@ -20,6 +19,7 @@ log = baseCore.getLogger()
cnx = pymysql.connect(host='114.115.159.144', user='caiji', password='zzsn9988', db='caiji', cnx = pymysql.connect(host='114.115.159.144', user='caiji', password='zzsn9988', db='caiji',
charset='utf8mb4') charset='utf8mb4')
cursor = cnx.cursor() cursor = cnx.cursor()
r = baseCore.r
URL = 'https://www.nasdaq.com/' URL = 'https://www.nasdaq.com/'
session = requests.session() session = requests.session()
session.mount('https://', HTTPAdapter(pool_connections=20, pool_maxsize=100)) session.mount('https://', HTTPAdapter(pool_connections=20, pool_maxsize=100))
...@@ -65,6 +65,7 @@ def add_date(com_code, date_list): ...@@ -65,6 +65,7 @@ def add_date(com_code, date_list):
# 数据发送端口 # 数据发送端口
def sendData(start_time, social_code, gpdm, dic_info): def sendData(start_time, social_code, gpdm, dic_info):
data = json.dumps(dic_info) data = json.dumps(dic_info)
# print(data)
url_baocun = 'http://114.115.236.206:8088/sync/finance/nsdk' url_baocun = 'http://114.115.236.206:8088/sync/finance/nsdk'
for nnn in range(0, 3): for nnn in range(0, 3):
try: try:
...@@ -86,7 +87,7 @@ def getUnit(gpdm): ...@@ -86,7 +87,7 @@ def getUnit(gpdm):
req.encoding = req.apparent_encoding req.encoding = req.apparent_encoding
soup = BeautifulSoup(req.text, 'lxml') soup = BeautifulSoup(req.text, 'lxml')
unit = soup.find('div', class_='financials__note').text.split(' ')[1].lstrip().strip() unit = soup.find('div', class_='financials__note').text.split(' ')[1].lstrip().strip()
unit = f'(千){unit}' unit = f'{unit}(千)'
req.close() req.close()
return unit return unit
...@@ -104,9 +105,11 @@ def getlist(table, tableName): ...@@ -104,9 +105,11 @@ def getlist(table, tableName):
value = re.sub(r"[^\d+-]", "", value) value = re.sub(r"[^\d+-]", "", value)
else: else:
value = '-' value = '-'
date = years[f'value{i}'].split('/')[2] + '-' + years[f'value{i}'].split('/')[0] + '-' + \ date_ = years[f'value{i}']
years[f'value{i}'].split('/')[1] if date_ :
list.append({f'{tableName}': name, 'value': value, 'date': date, }) date = date_.split('/')[2] + '-' + date_.split('/')[0] + '-' + \
date_.split('/')[1]
list.append({f'{tableName}': name, 'value': value, 'date': date, })
return list return list
...@@ -162,6 +165,7 @@ def getYear(start_time, session, social_code, gpdm): ...@@ -162,6 +165,7 @@ def getYear(start_time, session, social_code, gpdm):
# 判断该报告期是否已采过 # 判断该报告期是否已采过
panduan = check_date(social_code, date + '-year') panduan = check_date(social_code, date + '-year')
if panduan: if panduan:
log.info(f'{social_code}=={gpdm}=={date}年度数据采集过')
continue continue
xjll_list_f = reviseData([item for item in final_list if 'xjll' in item], unit, 'xjll') xjll_list_f = reviseData([item for item in final_list if 'xjll' in item], unit, 'xjll')
zcfz_list_f = reviseData([item for item in final_list if 'zcfz' in item], unit, 'zcfz') zcfz_list_f = reviseData([item for item in final_list if 'zcfz' in item], unit, 'zcfz')
...@@ -177,6 +181,7 @@ def getYear(start_time, session, social_code, gpdm): ...@@ -177,6 +181,7 @@ def getYear(start_time, session, social_code, gpdm):
"ynFirst": ynFirst, "ynFirst": ynFirst,
} }
sendData(start_time, social_code, gpdm, dic_info) sendData(start_time, social_code, gpdm, dic_info)
log.info(f'{social_code}=={gpdm}=={date}年度财务数据采集成功')
date_list.append(date + '-year') date_list.append(date + '-year')
else: else:
log.error(f'找不到{social_code}=={gpdm}年度财务数据') log.error(f'找不到{social_code}=={gpdm}年度财务数据')
...@@ -184,6 +189,7 @@ def getYear(start_time, session, social_code, gpdm): ...@@ -184,6 +189,7 @@ def getYear(start_time, session, social_code, gpdm):
takeTime = baseCore.getTimeCost(start_time, time.time()) takeTime = baseCore.getTimeCost(start_time, time.time())
baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===无年度财务数据') baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===无年度财务数据')
except: except:
log.error(f'{social_code}===年度财务数据访问失败')
state = 0 state = 0
takeTime = baseCore.getTimeCost(start_time, time.time()) takeTime = baseCore.getTimeCost(start_time, time.time())
baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===年度财务数据访问失败') baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===年度财务数据访问失败')
...@@ -217,6 +223,7 @@ def getQuarter(start_time, session, social_code, gpdm): ...@@ -217,6 +223,7 @@ def getQuarter(start_time, session, social_code, gpdm):
# 判断该报告期是否已采过 # 判断该报告期是否已采过
panduan = check_date(social_code, date + '-quarter') panduan = check_date(social_code, date + '-quarter')
if panduan: if panduan:
log.info(f'{social_code}=={gpdm}=={date}季度数据采集过')
continue continue
xjll_list_f = reviseData([item for item in final_list if 'xjll' in item], unit, 'xjll') xjll_list_f = reviseData([item for item in final_list if 'xjll' in item], unit, 'xjll')
zcfz_list_f = reviseData([item for item in final_list if 'zcfz' in item], unit, 'zcfz') zcfz_list_f = reviseData([item for item in final_list if 'zcfz' in item], unit, 'zcfz')
...@@ -236,13 +243,15 @@ def getQuarter(start_time, session, social_code, gpdm): ...@@ -236,13 +243,15 @@ def getQuarter(start_time, session, social_code, gpdm):
if panduan_flag: if panduan_flag:
dic_info['dateFlag'] = 'year' dic_info['dateFlag'] = 'year'
sendData(start_time, social_code, gpdm, dic_info) sendData(start_time, social_code, gpdm, dic_info)
log.info(f'{social_code}=={gpdm}=={date}季度财务数据采集成功')
date_list.append(date + '-quarter') date_list.append(date + '-quarter')
else: else:
log.error(f'{social_code}=={gpdm}无季度财务数据')
state = 0 state = 0
takeTime = baseCore.getTimeCost(start_time, time.time()) takeTime = baseCore.getTimeCost(start_time, time.time())
baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===无季度财务数据') baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===无季度财务数据')
except: except:
log.error(f'{social_code}===季度财务数据访问失败')
state = 0 state = 0
takeTime = baseCore.getTimeCost(start_time, time.time()) takeTime = baseCore.getTimeCost(start_time, time.time())
baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===季度财务数据访问失败') baseCore.recordLog(social_code, taskType, state, takeTime, url, f'{social_code}===季度财务数据访问失败')
...@@ -250,36 +259,52 @@ def getQuarter(start_time, session, social_code, gpdm): ...@@ -250,36 +259,52 @@ def getQuarter(start_time, session, social_code, gpdm):
return date_list return date_list
def FinanceFromNasdaq():
sql = "select xydm from mgzqyjwyh_list where state=2 and exchange='Nasdaq';"
cursor.execute(sql)
finance = cursor.fetchall()
finance_list = [item[0] for item in finance]
for item in finance_list:
r.rpush('FinanceFromNasdaq:nasdaqfinance_socialCode', item)
print('redis放入成功')
def getInfomation(social_code):
sql = f"select * from mgzqyjwyh_list where state=2 and xydm='{social_code}';"
cursor.execute(sql)
data = cursor.fetchone()
return data
def doJob(): def doJob():
# while True:
# social_code = baseCore.redicPullData('')
# datas_enterprise = baseCore.getInfomation(social_code)
session.get(URL, headers=headers) session.get(URL, headers=headers)
# sql = "select * from mgzqyjwyh_list where state=2 and exchange='Nasdaq';" while True:
# cursor.execute(sql) social_code = baseCore.redicPullData('FinanceFromNasdaq:nasdaqfinance_socialCode')
# datas_enterprise = cursor.fetchall() if not social_code or social_code == None:
# for data_enterprise in datas_enterprise: log.info('============已没有数据============等待===============')
start_time = time.time() time.sleep(600)
# gpdm = data_enterprise[3] continue
# social_code = data_enterprise[6] data_enterprise = getInfomation(social_code)
social_code = 'ZD0CN0012309000172' start_time = time.time()
gpdm = 'NTES' gpdm = data_enterprise[3]
# 采集年度数据 social_code = data_enterprise[6]
date_list_year = getYear(start_time, session, social_code, gpdm) # print(gpdm,social_code)
# 保存年度数据到redis # 采集年度数据
add_date(social_code, date_list_year) date_list_year = getYear(start_time, session, social_code, gpdm)
# 采集季度数据 # 保存年度数据到redis
date_list_quarter = getQuarter(start_time, session, social_code, gpdm) add_date(social_code, date_list_year)
# 保存季度数据到redis # 采集季度数据
add_date(social_code, date_list_quarter) date_list_quarter = getQuarter(start_time, session, social_code, gpdm)
timeCost = baseCore.getTimeCost(start_time, time.time()) # 保存季度数据到redis
state = 1 add_date(social_code, date_list_quarter)
baseCore.recordLog(social_code, taskType, state, timeCost, '', '') timeCost = baseCore.getTimeCost(start_time, time.time())
log.info(f'{social_code}=={gpdm}==耗时{timeCost}') state = 1
# break baseCore.recordLog(social_code, taskType, state, timeCost, '', '')
cursor.close() log.info(f'{social_code}=={gpdm}==耗时{timeCost}')
cnx.close()
if __name__ == '__main__': if __name__ == '__main__':
# 财务数据采集
doJob() doJob()
# 企业股票代码放入redis
# FinanceFromNasdaq()
cursor.close()
cnx.close()
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论