-
Notifications
You must be signed in to change notification settings - Fork 1
/
tienda_1.py
219 lines (121 loc) · 3.94 KB
/
tienda_1.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from bs4 import BeautifulSoup
import pandas as pd
import requests
from functools import reduce
# In[2]:
with open("data/tienda1.txt", encoding="utf-8") as file:
tienda1 = [l.rstrip("\n") for l in file]
# In[3]:
tienda1
# In[4]:
# url = input("Enter a website to extract the URL's from: ")
headers = {
"user-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
# r = requests.get("https://" +url)
r = requests.get(tienda1[0], headers)
data = r.text
soup = BeautifulSoup(data, features="html5lib")
url_data = []
for link in soup.find_all("a"):
text = link.get("href")
text = str(text).replace("'", "")
url_data.append(text)
# In[5]:
# initializing start Prefix
start_letter = "https://"
data = [x for x in url_data if x.startswith(start_letter)]
# In[6]:
# data
# In[7]:
# initializing start Prefix
start_letter = tienda1[0]
data_list = [x for x in data if x.startswith(start_letter)]
end_letter = ".html"
data_list1 = [x for x in data_list if x.endswith(end_letter)]
# data_list1
# In[8]:
total_url = []
for i in data_list1:
r = requests.get(i)
data = r.text
soup = BeautifulSoup(data, features="html5lib")
urls = []
for link in soup.find_all("a"):
text = link.get("href")
text = str(text).replace("'", "")
urls.append(text)
total_url.append(urls)
# In[9]:
# total_url
# In[10]:
# agrupar listas nested en una
single_list = reduce(lambda x, y: x + y, total_url)
# In[11]:
# initializing start Prefix
start_letter = tienda1[0]
data_total_url = [x for x in single_list if x.startswith(start_letter)]
# In[12]:
# In[13]:
len(data_total_url)
# In[14]:
# using naive method to remove duplicated from list
res_data_total_url = []
for i in data_total_url:
if i not in res_data_total_url:
res_data_total_url.append(i)
# In[15]:
len(res_data_total_url)
# In[18]:
total_url = []
headers = {
"user-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}
for i in res_data_total_url:
r = requests.get(i, headers=headers)
soup = BeautifulSoup(r.content, "html5lib")
total = []
resulta = soup.findAll("div", attrs={"class": "product-info-main"})
for link in resulta:
# desc = link.find('div', attrs = {'class':'url'}).get_text()
base = link.find("span", attrs={"class": "base"})
if base is not None:
base = base.get_text()
title = link.find("span", attrs={"class": "section-title"})
if title is not None:
title = title.get_text()
description = link.find("span", attrs={"itemprop": "description"})
if description is not None:
description = description.get_text()
sku = link.find("span", attrs={"itemprop": "sku"})
if sku is not None:
sku = sku.get_text()
color_value = link.find("span", attrs={"id": "color-swatch-value"})
if color_value is not None:
color_value = color_value.get_text()
price = link.find("span", attrs={"class": "price"})
if price is not None:
price = price.get_text()
color = link.find("img", attrs={"class": "color-value"})
if color is not None:
color = color.get_text()
one = {}
one["url"] = i
one["base"] = base
one["title"] = title
one["description"] = description
one["sku"] = sku
one["color_valuecolor_value"] = color_value
one["price"] = price
one["color"] = color
total.append(one)
total_url.append(total)
# In[19]:
single_list = reduce(lambda x, y: x + y, total_url)
df = pd.DataFrame.from_records(single_list)
df["url"] = df["url"].str.replace(tienda1[0], "tienda1", regex=True)
# In[20]:
df.to_csv("data/tienda_1.csv")