-
Notifications
You must be signed in to change notification settings - Fork 2
/
collect_data.py
160 lines (114 loc) ยท 6.21 KB
/
collect_data.py
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
from urllib.request import Request, urlopen
import requests
from bs4 import BeautifulSoup
import csv
import re
file_name = 'data'
# spell = ''
director_urls=[]
director_sns_urls=[]
director = ''
upload_date = ''
artist = ''
title = ''
production = ''
video_link = ''
# csv ํ์ผ ์ด๊ธฐ
f = open(f'{file_name}.csv','w', encoding="UTF-8", newline='')
wr = csv.writer(f, lineterminator='\n')
# CSV ํค๋
wr.writerow(['๊ฐ๋
์ด๋ฆ', '๊ฐ๋
์ด๋ฏธ์ง ์ฃผ์', '๊ฐ๋
SNS ๋งํฌ', 'ํ๋ก๋์
์ด๋ฆ', 'ํ๋ก๋์
์ด๋ฏธ์ง ์ฃผ์', '๋ฎค๋น ์ ๋ชฉ', '๋ฎค๋น ์ฃผ์',
'์ํฐ์คํธ', '๋ฎค๋น ์
๋ก๋ ๋ ์ง', '๊ฐ๋
์ด ๋ฐ๋๋ ์ง์ '])
# ๋ฎคํ์ด๋ธ URL์ ์ ๊ทผํด์ HTML์ ํ์ฑํ๋ค
url = 'http://muhive.net/'
req = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
main_html = req.text
soup = BeautifulSoup(main_html, 'html.parser')
# ์ด์ฑ ์์๊ณผ ๊ฐ๋
์ด๋ฆ์ ๋ฝ์๋ด์
# URL ์์ ๊ทธ๋ ๊ฒ ์ ๊ทผํด์ผ ๊ฐ๋
๋ณ ๋ฎค๋น ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ ์ ์์ ex) http://muhive.net/k-pop-music-video-directors/ใฑ/๊ณ ์ ์ /
#directors_with_html = soup.find_all("a", {"class" : "fusion-bar-highlight"})
directors_with_html = soup.select('.sub-menu > li > a')
#ใฑ,ใด,ใท ์ด๋ฐ๊ฑฐ ๊ฐ์ ธ์ค๋ ค๊ณ
# spell_pattern = re.compile('<span class="menu-text">.*</span>')
#๊ฐ๋
์ด๋ฆ ๊ฐ์ ธ์ค๋ ค๊ณ
director_pattern = re.compile('<span>.*</span>')
#๊ฐ๋
๋ณ ํ์ด์ง ๊ฐ์ ธ์ค๋ ค๊ณ
for director_with_html in directors_with_html:
director_url = director_with_html['href']
director_urls.append(director_url)
# spell_with_span_tag = spell_pattern.findall(str(director_with_html))
# if (len(spell_with_span_tag) != 0) :
# if ('Home' not in spell_with_span_tag[0] and 'About Us' not in spell_with_span_tag[0]) :
# #spell์๋ ใฑ,ใด,ใท,~ ์ด๋ฐ๊ฑฐ ๋ค์ด๊ฐ๋
# spell = spell_with_span_tag[0][24:len(spell_with_span_tag) - 8]
# director_with_span_tag = director_pattern.findall(str(director_with_html))
# if (len(director_with_span_tag) != 0) :
# director = director_with_span_tag[0][6:len(director_with_span_tag) - 8]
# else :
# director = ''
# if (spell != '' and director != ''):
# director_urls.append(f'http://muhive.net/k-pop-music-video-directors/{spell}/{director}/')
#๊ฐ๋
๋ณ ํ์ด์ง ๋ค์ด๊ฐ์ ๋
ธ๋๋ณ ํ์ด์ง ๋ค์ด๊ฐ๊ณ ๊ทธ ์์์ mv ๊ด๋ จ ๋ฐ์ดํฐ ๋ฝ์๋ด๊ธฐ
for director_url in director_urls:
req = requests.get(director_url, headers={'User-Agent': 'Mozilla/5.0'})
html = req.text
soup = BeautifulSoup(html, 'html.parser')
#๊ฐ๋
๊ฐ์ธํ์ด์ง์์ sns๋งํฌ ๋ฝ์๋ด๊ธฐ
director_sns_htmls = soup.select('.fusion-social-links > .fusion-social-networks > .fusion-social-networks-wrapper > a')
#๊ฐ๋
๊ฐ์ธ ํ์ด์ง์ ์๋ ๊ณก ๋ชฉ๋ก์ค์ ํ๋์ฉ ๋งํฌ ๋ฝ์๋ด๊ธฐ
mv_list_page_html = soup.select('.lcp_catlist > li > a')
del director_sns_urls[:]
for director_sns_html in director_sns_htmls:
#๊ฐ๋
์ ๊ฐ์ธ sns ์ข
๋ฅ๋ ๊ตณ์ด ๋ฐ๋ก ๋ฝ์๋ด์ง ๋ง๊ณ ๊ทธ๋ฅ ๋๋ฉ์ธ์ผ๋ก ๋์ค์ ์ฒ๋ฆฌํ์ฅ
#director_sns_name = director_sns_html['data-title']
director_sns_urls.append(director_sns_html['href'])
for mv_page_html in mv_list_page_html:
#mv_url์๋ ๊ณก ํ๋๋น ๋งํฌ๊ฐ ๋ค์ด๊ฐ
mv_url = mv_page_html['href']
req = requests.get(mv_url, headers={'User-Agent': 'Mozilla/5.0'})
html = req.text
soup = BeautifulSoup(html, 'html.parser')
#๋ฎค๋น ๋งํฌ ๋ฝ์๋ด๊ธฐ
mv_link_html = soup.select('.post-content > .video-shortcode > iframe')
mv_link_pattern = re.compile('https://www.youtube.com/embed/.*?feature=oembed')
mv_link = mv_link_pattern.findall(str(mv_link_html))
#mv_link_pattern์ ์๋ง๋ ๋ฎค๋น๋ ๊ทธ๋ฅ ๋งํฌ ์๋ค๊ณ ์ทจ๊ธ
#ex)๊ณ ํ๋ฏผ ๊ฐ๋
๋์ ๋ฐ์ฌ์ -๋๋จ์
if(len(mv_link) == 0) :
video_link = ''
else :
video_link = mv_link[0][30:-15]
#๊ณก๋ณ ๋งํฌ ํ๊ณ ๋ค์ด๊ฐ์ ๊ฐ๋
, ์ํฐ์คํธ, ๋ฐ๋งค์ผ ๋ฑ ์ ๋ณด ๋ฝ์๋ด๊ธฐ
mv_info_html = soup.select('.post-content > ul')
mv_info_pattern = re.compile('<li>.*')
mv_infos = mv_info_pattern.findall(str(mv_info_html))
for mv_info in mv_infos:
if('Date' in mv_info):
#๋ฎคํ์ด๋ธ์ ๋ฎค๋น ์
๋ก๋ ๋ ์ง ํ์์ด ๊ฐ๊ฐ ๋ฌ๋ผ์ ํ์ ๋ง์ถฐ์ฃผ๊ธฐ ์ํ ์ฝ๋
#ex)2020.01.12 โ 2020-01-12
upload_date = mv_info[11:-5].strip().replace('.','-')
#ex)2020-1-12 โ 2020-01-12
#if(len(upload_date) != 10):
#upload_date = upload_date[0:5]+'0'+upload_date[5:]
elif('Artist' in mv_info):
artist = mv_info[12:-5].strip()
elif('Song Title' in mv_info):
title = mv_info[16:-5].strip()
elif('Director' in mv_info):
director = mv_info[14:-5].strip()
elif('Production' in mv_info):
if('</li>' not in mv_info):
production = mv_info[16:].strip()
else:
production = mv_info[16:-5].strip()
#print(director, '', ','.join(director_sns_urls), production, '', title, video_link, artist, upload_date)
wr.writerow([director, '', ','.join(director_sns_urls), production, '', title, video_link, artist, upload_date])
director = ''
production = ''
########################################################################################################################
# ์ดํ ์์
์ ์์์ ์งํํ ๊ฒ์ฒ๋ผ, ์ถ์ถํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์๋ URL์ ์ ๊ทผํด์, ๊ด๋ จ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ท์น(ํ๊ทธ๋ช
, ์ ๊ทํํ์ ๋ฑ)์ ์ฐพ์๋ด์ผ ํฉ๋๋ค #
########################################################################################################################
################
# ์ต์ข
๋ฐ์ดํฐ ์์ #
################
f.close()