-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
171 lines (149 loc) · 5.6 KB
/
crawler.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
import pandas as pd
import requests
import openpyxl
import json
from bs4 import BeautifulSoup
from tqdm import tqdm
import dotenv
import os
# *******************************
# Variable Needs to be changed
# *******************************
input_filename = "에스쁘아_0718" # input 파일명
brand_name = "에스쁘아" # 브랜드명 (brandData에 있는 브랜드명)
# *******************************
dictBrand = [
{"name_kr": "이니스프리", "api_id": "7"},
{"name_kr": "설화수", "api_id": "8"},
{"name_kr": "헤라", "api_id": "3"},
{"name_kr": "에뛰드", "api_id": "9"},
{"name_kr": "미샤", "api_id": "10"},
{"name_kr": "아비브", "api_id": "11"},
{"name_kr": "에스트라", "api_id": "12"},
{"name_kr": "베네피트", "api_id": "13"},
{"name_kr": "숨37도", "api_id": "14"},
{"name_kr": "오휘", "api_id": "15"},
{"name_kr": "fmgt", "api_id": "16"},
{"name_kr": "프레시안", "api_id": "1"},
{"name_kr": "네이밍", "api_id": "17"},
{"name_kr": "키스미", "api_id": "18"},
{"name_kr": "힌스", "api_id": "19"},
{"name_kr": "멜릭서", "api_id": "5"},
{"name_kr": "데이지크", "api_id": "20"},
{"name_kr": "애프터블로우", "api_id": "21"},
{"name_kr": "려", "api_id": "6"},
{"name_kr": "더바디샵", "api_id": "22"},
{"name_kr": "롱테이크", "api_id": "23"},
{"name_kr": "피지오겔", "api_id": "4"},
{"name_kr": "어뮤즈", "api_id": "24"},
{"name_kr": "에스쁘아", "api_id": "27"},
{"name_kr": "롬앤", "api_id": "2"},
{"name_kr": "논픽션", "api_id": "26"},
{"name_kr": "탬버린즈", "api_id": "25"},
{"name_kr": "스킨푸드", "api_id": "28"},
]
class CopyCrawler(object):
# 초기화
def __init__(self, input_filename, brand_name):
self.input_filename = input_filename + ".xlsx"
self.brand_name = brand_name
self.excel_sheet_name = input_filename
self.output_filename = input_filename + "_parsed.xlsx"
self.body = {}
self.max_data = 10
dotenv.load_dotenv()
self.serverUrl = os.getenv("SERVER_URL")
# html requests
def get_text(self, idx, url):
response = requests.get(url)
if response.status_code == 200:
html = response.text
bs = BeautifulSoup(html, "html.parser")
try:
target_text = bs.select_one("title").get_text().lstrip(
'\n').replace('\r', '\n')
target_json = json.loads(
bs.select_one(
'script[type="application/ld+json"]').get_text()
)
return '\"'.join(target_text.split('"')[1:-1]), target_json[0]["dateCreated"][:-6]
except:
print('-----exception handling needed-----')
print(idx, url)
return '\"'.join(target_text.split('"')[1:-1]), '-----fix-----'
else:
print(response.status_code)
def post_request(self):
headers = {"Content-type": "application/json"}
response = requests.post(
self.serverUrl, json.dumps(self.body), headers=headers)
# 상태 코드
print(response.status_code)
print(response.json())
def find_dict(self, **kwargs):
return next(
(
obj["api_id"]
for obj in dictBrand
if len(set(obj.keys()).intersection(kwargs.keys())) > 0
and all([obj.get(k) == v for k, v in kwargs.items()])
),
# 기본값
None,
)
def get_data(self):
df = pd.read_excel(self.input_filename, engine="openpyxl")
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
res_text = []
res_date = []
res_url = []
self.max_data = len(df["Url"])
for idx, url in tqdm(enumerate(df["Url"]), total=self.max_data):
T, D = self.get_text(idx, url)
res_text.append(T)
res_date.append(D)
res_url.append(url)
excel = []
excel.append(res_text)
excel.append(res_date)
excel.append(res_url)
data = []
for i in range(self.max_data):
copy = {
"text": res_text[i],
"createdAt": res_date[i],
"brandId": int(self.find_dict(name_kr=self.brand_name)),
"url": res_url[i]
}
data.append(copy)
self.body["data"] = data
print(json.dumps(self.body, ensure_ascii=False))
# self.write_excel(excel)
def write_excel(self, excel):
print("body", self.body)
pd.DataFrame(excel).T.to_excel(excel_writer=self.output_filename)
def test_data(self):
url = input()
response = requests.get(url)
if response.status_code == 200:
html = response.text
bs = BeautifulSoup(html, "html.parser")
target_text = bs.select_one("title").get_text()
tmp = 'MISSHA 미샤 on Instagram: "'
# print(target_text.split('"')[1])
# print('-------------------------')
# target_text = target_text.lstrip(tmp).rstrip('"')
print(target_text)
print('-------------------------------')
print('\"'.join(target_text.split('"')[1:-1]))
else:
print(response.status_code)
def run(self):
self.get_data()
# self.post_request()
def test(self):
self.test_data()
copycrawler = CopyCrawler(input_filename, brand_name)
copycrawler.run()
# copycrawler.test()