PriceForecast/测试环境获取市场信息平台数据项.ipynb
2024-12-27 14:15:20 +08:00

321 lines
14 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "31c0e11d-c87a-4e95-92a0-d1d09625e255",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"数据库连接成功 192.168.101.27 jingbo_test root\n"
]
}
],
"source": [
"from config_jingbo import *\n",
"import requests\n",
"import json\n",
"import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "83c81b9e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'http://192.168.100.53:8080/jingbo-dev/api/server/login'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"login_pushreport_url\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a058f507",
"metadata": {},
"outputs": [],
"source": [
"login_pushreport_url = \"http://10.200.32.39/jingbo-api/api/server/login\"\n",
"upload_url = \"http://10.200.32.39/jingbo-api/api/analysis/reportInfo/researchUploadReportSave\"\n",
"upload_warning_url = \"http://10.200.32.39/jingbo-api/api/basicBuiness/crudeOilWarning/save\"\n",
"query_data_list_item_nos_url = \"http://10.200.32.39/jingbo-api/api/warehouse/dwDataItem/queryDataListItemNos\"\n",
"\n",
"login_data = {\n",
" \"data\": {\n",
" \"account\": \"api_dev\",\n",
" \"password\": \"ZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2U=\",\n",
" \"tenantHashCode\": \"8a4577dbd919675758d57999a1e891fe\",\n",
" \"terminal\": \"API\"\n",
" },\n",
" \"funcModule\": \"API\",\n",
" \"funcOperation\": \"获取token\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8af0311d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'http://10.200.32.39/jingbo-api/api/server/login'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"login_pushreport_url"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2b330ee3-c006-4ab1-8558-59c51ac8d86f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'data': {'account': 'api_dev',\n",
" 'password': 'ZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2U=',\n",
" 'tenantHashCode': '8a4577dbd919675758d57999a1e891fe',\n",
" 'terminal': 'API'},\n",
" 'funcModule': 'API',\n",
" 'funcOperation': '获取token'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"login_data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dcb6100a-ed2b-4077-a1a9-361c6cb565f9",
"metadata": {},
"outputs": [],
"source": [
"\n",
"def get_head_auth_report():\n",
" login_res = requests.post(url=login_pushreport_url, json=login_data, timeout=(3, 5))\n",
" text = json.loads(login_res.text)\n",
" print(text)\n",
" if text[\"status\"]:\n",
" token = text[\"data\"][\"accessToken\"]\n",
" return token\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "22c0c7c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'confirmFlg': False, 'data': {'accessToken': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcGlfZGV2IiwidGgiOiI4YTQ1NzdkYmQ5MTk2NzU3NThkNTc5OTlhMWU4OTFmZSIsImx0IjoiYXBpIiwiaXNzIjoiIiwidG0iOiJQQyIsImV4cCI6MTczNTMxNTg5NywianRpIjoiNDk1NGEyNDYyYThkNDc1YjkwMTMwMDRjNGNlZWY4NTcifQ.ABXdVR-X4ZOtzPJN2slCa1HwqQgmwGjDxOX4aKqaBWU', 'md5Token': 'd870019072ff52333add0545d26398a8'}, 'status': True}\n"
]
}
],
"source": [
"token = get_head_auth_report()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "12077ead",
"metadata": {},
"outputs": [],
"source": [
"# token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcGlfdGVzdCIsInRoIjoiOGE0NTc3ZGJkOTE5Njc1NzU4ZDU3OTk5YTFlODkxZmUiLCJsdCI6ImFwaSIsImlzcyI6IiIsInRtIjoiUEMiLCJleHAiOjE3MzE5NTkzNjUsImp0aSI6IjRiMjcwNTgzN2YyZDQxOWM4MzQ3NjI2NDQwZDlhZGQzIn0.PPgnoiJt412dJiceqVW8w7qkJFY4s-VqU9z6ZIkpqho'"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a7ae21d1",
"metadata": {},
"outputs": [],
"source": [
"# def upload_warning_data(warning_data):\n",
"# token = get_head_auth_report()\n",
"# warning_data = warning_data\n",
"# headers = {\"Authorization\": token}\n",
"# logger.info(\"预警上传中...\")\n",
"# logger.info(f\"token:{token}\")\n",
"# logger.info(f\"warning_data:{warning_data}\" )\n",
"# upload_res = requests.post(url=upload_warning_url, headers=headers, json=warning_data, timeout=(3, 15))\n",
"# if upload_res:\n",
"# return upload_res\n",
"# else:\n",
"# logger.info(\"预警上传失败\")\n",
"# return None\n",
"\n",
"\n",
"# logger.info(f'上传预警信息')\n",
"# try:\n",
"# warning_date = datetime.datetime.now().strftime('%Y-%m-%d')\n",
"# content = f'{warning_date}有2887个停更'\n",
"# warning_data['data']['WARNING_DATE'] = warning_date\n",
"# warning_data['data']['WARNING_CONTENT'] = content\n",
"# upload_warning_data(warning_data)\n",
"# logger.info(f'上传预警信息成功')\n",
"# except Exception as e:\n",
"# logger.error(f'上传预警信息失败:{e}')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "54942e1a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n",
"<html>\n",
"<head><title>404 Not Found</title></head>\n",
"<body bgcolor=\"white\">\n",
"<h1>404 Not Found</h1>\n",
"<p>The requested URL was not found on this server. Sorry for the inconvenience.<br/>\n",
"Please report this message and include the following information to us.<br/>\n",
"Thank you very much!</p>\n",
"<table>\n",
"<tr>\n",
"<td>URL:</td>\n",
"<td>http://10.200.32.39/jingbo-dev/api/warehouse/dwDataItem/queryDataListItemNos</td>\n",
"</tr>\n",
"<tr>\n",
"<td>Server:</td>\n",
"<td>iz2zeb0rw4fi7a0b3dh8jfz</td>\n",
"</tr>\n",
"<tr>\n",
"<td>Date:</td>\n",
"<td>2024/12/27 14:11:26</td>\n",
"</tr>\n",
"</table>\n",
"<hr/>Powered by Tengine</body>\n",
"</html>\n",
"\n"
]
}
],
"source": [
"query_data_list_item_nos_data = {\n",
" \"funcModule\": \"数据项\",\n",
" \"funcOperation\": \"查询\",\n",
" \"data\": {\n",
" \"dateStart\":\"20200101\",\n",
" \"dateEnd\":\"20241231\",\n",
" \"dataItemNoList\":[\"Brentzdj\",\"Brentzgj\"]\n",
" }\n",
"}\n",
"\n",
"headers = {\"Authorization\": token}\n",
"items_res = requests.post(url=query_data_list_item_nos_url, headers=headers, json=query_data_list_item_nos_data, timeout=(3, 35))\n",
"print(items_res.text)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5272c84b",
"metadata": {},
"outputs": [
{
"ename": "JSONDecodeError",
"evalue": "Expecting value: line 1 column 1 (char 0)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mJSONDecodeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[11], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mjson\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpd\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m json_data \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(items_res\u001b[38;5;241m.\u001b[39mtext)\n\u001b[0;32m 5\u001b[0m df \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(json_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# 按照dataItemNo 分组 得到多个dataframe 最后根据dataDate merge 成一个dataframe\u001b[39;00m\n",
"File \u001b[1;32md:\\ProgramData\\anaconda3\\Lib\\json\\__init__.py:346\u001b[0m, in \u001b[0;36mloads\u001b[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m 341\u001b[0m s \u001b[38;5;241m=\u001b[39m s\u001b[38;5;241m.\u001b[39mdecode(detect_encoding(s), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msurrogatepass\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 343\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[0;32m 344\u001b[0m parse_int \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m parse_float \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[0;32m 345\u001b[0m parse_constant \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_pairs_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kw):\n\u001b[1;32m--> 346\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _default_decoder\u001b[38;5;241m.\u001b[39mdecode(s)\n\u001b[0;32m 347\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 348\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m JSONDecoder\n",
"File \u001b[1;32md:\\ProgramData\\anaconda3\\Lib\\json\\decoder.py:337\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m 332\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecode\u001b[39m(\u001b[38;5;28mself\u001b[39m, s, _w\u001b[38;5;241m=\u001b[39mWHITESPACE\u001b[38;5;241m.\u001b[39mmatch):\n\u001b[0;32m 333\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001b[39;00m\n\u001b[0;32m 334\u001b[0m \u001b[38;5;124;03m containing a JSON document).\u001b[39;00m\n\u001b[0;32m 335\u001b[0m \n\u001b[0;32m 336\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 337\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mraw_decode(s, idx\u001b[38;5;241m=\u001b[39m_w(s, \u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m.\u001b[39mend())\n\u001b[0;32m 338\u001b[0m end \u001b[38;5;241m=\u001b[39m _w(s, end)\u001b[38;5;241m.\u001b[39mend()\n\u001b[0;32m 339\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m end \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(s):\n",
"File \u001b[1;32md:\\ProgramData\\anaconda3\\Lib\\json\\decoder.py:355\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[1;34m(self, s, idx)\u001b[0m\n\u001b[0;32m 353\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mscan_once(s, idx)\n\u001b[0;32m 354\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m--> 355\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m JSONDecodeError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExpecting value\u001b[39m\u001b[38;5;124m\"\u001b[39m, s, err\u001b[38;5;241m.\u001b[39mvalue) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 356\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m obj, end\n",
"\u001b[1;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
]
}
],
"source": [
"# 遍历items.text 获取data ,整理成 ds itemno value 的形式\n",
"import json\n",
"import pandas as pd\n",
"json_data = json.loads(items_res.text)\n",
"df = pd.DataFrame(json_data['data'])\n",
"# 按照dataItemNo 分组 得到多个dataframe 最后根据dataDate merge 成一个dataframe\n",
"df2 = pd.DataFrame()\n",
"for i in df['dataItemNo'].unique():\n",
" df1 = df[df['dataItemNo'] == i]\n",
" df1 = df1[['dataDate', 'dataValue']]\n",
" df1 = df1.rename(columns={'dataValue': i})\n",
" df2 = pd.concat([df2, df1], axis=0)\n",
"print(df2)\n",
"\n",
"\n",
"\n",
"df2.to_csv('data.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b1de00f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}