pr.ipynb
5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from bs4 import BeautifulSoup\n",
"import urllib\n",
"from urllib import request\n",
"import re\n",
"import json\n",
"from datetime import datetime\n",
"import os\n",
"import boto3\n",
"import time\n",
"import sys\n",
"import plotly.offline as py\n",
"import plotly.graph_objs as go\n",
"import plotly.tools as tls\n",
"import matplotlib\n",
"from random import shuffle\n",
"\n",
"py.init_notebook_mode(connected=True)\n",
"\n",
"client_id = \"sYcWggwUdtmXwGqUrzzN\"\n",
"client_secret = \"oxUqDSa22I\"\n",
"\n",
"link = \"https://openapi.naver.com/v1/datalab/search\"\n",
"requested = request.Request(link)\n",
"requested.add_header(\"X-Naver-Client-Id\",client_id)\n",
"requested.add_header(\"X-Naver-Client-Secret\",client_secret)\n",
"requested.add_header(\"Content-Type\",\"application/json\")\n",
"\n",
"df = pd.read_excel(\"index.xls\")\n",
"names = df.회사명.values\n",
"\n",
"now = datetime.now().strftime(\"%Y-%m-%d\")\n",
"body_dict = {\"startDate\":\"2017-01-01\", \n",
" \"endDate\":\"2020-05-07\",\n",
" \"timeUnit\":\"date\"}\n",
"v_list = [{\"groupName\" : i, \"keywords\" : [i]} for i in names]\n",
"df[\"대표자명\"] = df[\"대표자명\"].apply(lambda x: re.sub(r'\\(.*\\)', '', x))\n",
"df[\"대표자명\"] = df[\"대표자명\"].apply(lambda x: [re.compile('[^ㄱ-ㅣ가-힣]+').sub(\"\",x)] if len(re.compile('[^ㄱ-ㅣ가-힣]+').sub(\"\",x)) < 5 else re.findall(r\"[\\w']+\", x))\n",
"\n",
"for i in range(0, df.shape[0]):\n",
" for j in df[\"대표자명\"].values[i]:\n",
" if \"대표\" not in j and j!= \"\":\n",
" v_list[i][\"keywords\"].append(j) \n",
" \n",
"shuffle(v_list)\n",
"standard = v_list[0]\n",
"standard_keyword = standard[\"groupName\"]\n",
"list_use = v_list[1:]\n",
"\n",
"split_list = [list_use[i:i+4] for i in range(0, len(list_use), 4)]\n",
"\n",
"for i in split_list:\n",
" i.append(standard)\n",
"\n",
"sample_body = body_dict\n",
"sample_body[\"keywordGroups\"] = split_list[0]\n",
"sample_body = json.dumps(sample_body, ensure_ascii=False)\n",
"sample_response = request.urlopen(requested, data=sample_body.encode(\"utf-8\"))\n",
"\n",
"code = sample_response.getcode() \n",
"if code == 200: \n",
" sample_response_body = sample_response.read()\n",
" sample_scraped = sample_response_body.decode(\"utf-8\")\n",
"else: \n",
" print (\"Error Code:\", code)\n",
"\n",
"sample_result = json.loads(sample_scraped)\n",
"\n",
"for i in sample_result[\"results\"] :\n",
" if i[\"title\"] == standard_keyword:\n",
" sample_standard = i[\"data\"]\n",
"scale = sample_standard[0][\"ratio\"]\n",
"\n",
"df = {}\n",
"\n",
"df[standard_keyword] = np.array([i[\"ratio\"] for i in sample_standard])\n",
"length = len(df[standard_keyword])\n",
"date = np.array([i[\"period\"] for i in sample_standard])\n",
"for i in split_list:\n",
"\n",
" body_dict[\"keywordGroups\"] = i\n",
" body = json.dumps(body_dict, ensure_ascii=False)\n",
" \n",
" print(i)\n",
" response = request.urlopen(requested, data=body.encode(\"utf-8\"))\n",
" \n",
" code = response.getcode() \n",
" if code == 200: \n",
" response_body = response.read()\n",
" scraped = response_body.decode(\"utf-8\")\n",
" else: \n",
" print (\"Error Code:\", code)\n",
" \n",
" \n",
" \n",
" result = json.loads(scraped)\n",
" \n",
" for i in result[\"results\"]:\n",
" if i[\"title\"] == standard_keyword:\n",
" compare = i[\"data\"]\n",
" compare = compare[0][\"ratio\"]\n",
" \n",
" scaling = scale/compare\n",
" \n",
" for i in result[\"results\"]:\n",
" if i[\"title\"]!=standard_keyword:\n",
" value = [j[\"ratio\"]*scaling for j in i[\"data\"]]\n",
" if len(value)!=length:\n",
" value+=np.abs(length-len(value)) * [value[-1]]\n",
" df[i[\"title\"]] = np.array(value)\n",
"\n",
"df = pd.DataFrame(df)\n",
"df[\"date\"] = date\n",
"df = df.set_index(\"date\")\n",
"df.to_csv(\"trend.xls\", encoding = \"utf-8\")\n",
"\n",
"df.plot(title = \"Naver Trend - Stock-Daily\", figsize = (20, 10), legend = False)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}