-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
177 lines (123 loc) · 4.06 KB
/
test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 수정 중인 코드 주석 처리하였음
from django.shortcuts import render
from bs4 import BeautifulSoup
import requests
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "osori.settings")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
import django
django.setup()
from outfit.models import mixxo_model
from outfit.models import musinsa_model
from outfit.models import spao_model
musinsa = "https://www.musinsa.com/mz/brandsnap?p=1"
mixxo = "https://www.mixxo.com"
spao = "https://www.spao.com"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36'}
def get_request(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
return soup
# musinsa
# 이미지 링크 스크래핑 함수
def get_musinsa_images():
soup = get_request(musinsa)
list_items = soup.select('div.articleImg>a>img')
list_images = [i.attrs['src'] for i in list_items[:3]]
return list_images
# 링크 스크래핑 함수
def get_musinsa_links():
soup = get_request(musinsa)
list_items = soup.select('div.articleImg>a')
list_links = ["https://www.musinsa.com" + i.attrs['href']
for i in list_items[:3]]
return list_links
# 무신사 크롤링 함수
def get_musinsa_data():
result = []
i = 0
list_links = get_musinsa_links()
list_images = get_musinsa_images()
for link, image in zip(list_links, list_images):
i += 1
musinsa_obj = {
'musinsa_image': image,
'musinsa_link': link,
'musinsa_key': i
}
result.append(musinsa_obj)
return result
# mixxo
def get_mixxo_images():
# 상품 이미지 링크
soup = get_request(mixxo)
list_items = soup.select('div.prdImg>a>img')
list_images = ["https:" + i.attrs['src'] for i in list_items[:3]]
return list_images
def get_mixxo_links():
# 상품 url
soup = get_request(mixxo)
list_items = soup.select('div.prdImg>a')
list_links = ["https:" + i.attrs['href'] for i in list_items[:3]]
return list_links
# 미쏘 크롤링 함수
def get_mixxo_data():
result = []
i = 0
list_links = get_mixxo_links()
list_images = get_mixxo_images()
for link, image in zip(list_links, list_images):
i += 1
mixxo_obj = {
'mixxo_image': image,
'mixxo_link': link,
'mixxo_key': i
}
result.append(mixxo_obj)
return result
# spao
def get_spao_images():
# 상품 이미지 링크
soup = get_request(spao)
list_items = soup.select('div.prdImg>a>img')
list_images = ["https:" + i.attrs['src'] for i in list_items[:3]]
return list_images
def get_spao_links():
# 상품 url
soup = get_request(spao)
list_items = soup.select('div.prdImg>a')
list_links = ["https:" + i.attrs['href'] for i in list_items[:3]]
return list_links
# 스파오 크롤링 함수
def get_spao_data():
result = []
i = 0
list_links = get_spao_links()
list_images = get_spao_images()
for link, image in zip(list_links, list_images):
i += 1
spao_obj = {
'spao_image': image,
'spao_link': link,
'spao_key': i
}
result.append(spao_obj)
return result
# insert data into sqlite
if __name__ == '__main__':
musinsa_data = get_musinsa_data()
mixxo_data = get_mixxo_data()
spao_data = get_spao_data()
# musinsa object
for item in musinsa_data:
musinsa_model(musinsa_image=item['musinsa_image'],
musinsa_link=item['musinsa_link'], musinsa_key=item['musinsa_key']).save()
# for item in mixxo_data:
for item in mixxo_data:
mixxo_model(mixxo_image=item['mixxo_image'],
mixxo_link=item['mixxo_link'], mixxo_key=item['mixxo_key']).save()
# for item in spao_data:
for item in spao_data:
spao_model(spao_image=item['spao_image'],
spao_link=item['spao_link'], spao_key=item['spao_key']).save()