mlink-checkpoint.ipynb
5.06 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import urllib.request\n",
"from bs4 import BeautifulSoup\n",
"import requests\n",
"\n",
"url = \"http://www.playdb.co.kr/playdb/playdblist.asp?Page=40&sReqMainCategory=000001&sReqSubCategory=&sReqDistrict=&sReqTab=2&sPlayType=1&sStartYear=2019&sSelectType=1\"\n",
"req = urllib.request.urlopen(url)\n",
"res = req.read()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"soup = BeautifulSoup(res,'html.parser')\n",
"for link in soup.find_all('a'):\n",
" #a= link.get('href')\n",
" if 'PlaydbDetail' in link.get('href'):\n",
" paramcode = link.get('href')\n",
" print(link.get('href'))\n",
" print(paramcode)\n",
"\n",
" \n",
"##keywords = soup.find_all('span',class_='ah_k')\n",
"#get_text() == 데이터에서 문자열만 추출\n",
"#strip() == 데이터의 양옆 공백제거\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"웹 크롤링 후 csv 파일로 저장.\n",
"뒷페이지로 갈 수록 링크 사라지는 문제 해결 요청."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import time\n",
"time.sleep(5)\n",
"\n",
"toturl = []\n",
"whole_source = \"\"\n",
"#171페이지까지 있음\n",
"for pageNo in range(1,172):\n",
" url = \"http://www.playdb.co.kr/playdb/playdblist.asp?Page=\" + str(pageNo) +\"&sReqMainCategory=000001&sReqSubCategory=&sReqDistrict=&sReqTab=2&sPlayType=1&sStartYear=2019&sSelectType=1\"\n",
" response = requests.get(url)\n",
" whole_source = whole_source + response.text\n",
" \n",
"f = open(\"/Users/김서영/Desktop/temp/data/playdblink.csv\", \"w\")\n",
"soup = BeautifulSoup(whole_source, 'html.parser')\n",
"for link in soup.find_all('a'):\n",
" if 'PlaydbDetail' in link.get('href'):\n",
" paramcode = link.get('href')\n",
" print(link.get('href'))\n",
" f.write(paramcode+\",\")\n",
" #toturl.append(link.get('href'))\n",
" #paramcode = soup.find('a')\n",
" #for link in soup.find_all('a'):\n",
" \n",
" \n",
"#print(toturl)\n",
" \n",
"##keywords = soup.find_all('span',class_='ah_k')\n",
"#get_text() == 데이터에서 문자열만 추출\n",
"#strip() == 데이터의 양옆 공백제거\n",
"#중복되는 url지우기!!!!!\n",
" \n",
" \n",
"# 데이터의 헤더부분을 입력한다.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"f.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이 아래로는 연습 해둔 것들. 실행 x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"toturl = []\n",
"whole_source = \"\"\n",
"#171페이지까지 있음\n",
"for pageNo in range(1,172):\n",
" url = \"http://www.playdb.co.kr/playdb/playdblist.asp?Page=\" + str(pageNo) +\"&sReqMainCategory=000001&sReqSubCategory=&sReqDistrict=&sReqTab=2&sPlayType=1&sStartYear=2019&sSelectType=1\"\n",
" response = requests.get(url)\n",
" whole_source = whole_source + response.text\n",
"\n",
"soup = BeautifulSoup(whole_source, 'html.parser')\n",
" #soup = BeautifulSoup(res,'html.parser')\n",
"for link in soup.find_all('a'):\n",
" if 'PlaydbDetail' in link.get('href'):\n",
" paramcode = link.get('href')\n",
" #print(link.get('href'))\n",
" toturl.append(link.get('href'))\n",
" #paramcode = soup.find('a')\n",
" #for link in soup.find_all('a'):\n",
"print(toturl)\n",
" \n",
"##keywords = soup.find_all('span',class_='ah_k')\n",
"#get_text() == 데이터에서 문자열만 추출\n",
"#strip() == 데이터의 양옆 공백제거\n",
"#중복되는 url지우기!!!!!\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"dataframe = pd.DataFrame(toturl)\n",
"dataframe.to_csv(\"/Users/김서영/Desktop/temp/mUrl.csv\", header = False, index = False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}