-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_work.py
More file actions
194 lines (166 loc) · 8.28 KB
/
scrape_work.py
File metadata and controls
194 lines (166 loc) · 8.28 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
182
183
184
185
186
187
188
189
190
191
192
193
194
from requests import get
from bs4 import BeautifulSoup
import sys
import os
from urllib.parse import urljoin
# to import html file already saved
import codecs
import pandas as pd
import time
import re
def multireplace(string, replacements):
"""
Given a string and a replacement map, it returns the replaced string.
:param str string: string to execute replacements on
:param dict replacements: replacement dictionary {value to find: value to replace}
:rtype: str
"""
# Place longer ones first to keep shorter substrings from matching where the longer ones should take place
# For instance given the replacements {'ab': 'AB', 'abc': 'ABC'} against the string 'hey abc', it should produce
# 'hey ABC' and not 'hey ABc'
substrs = sorted(replacements, key=len, reverse=True)
# Create a big OR regex that matches any of the substrings to replace
regexp = re.compile('|'.join(map(re.escape, substrs)))
# For each match, look up the new string in the replacements
return regexp.sub(lambda match: replacements[match.group(0)], string)
def grower_dict():
'''
Returns a dictionary to find and replace keywords in text
'''
find_replace = {'8TH & PENN' : '8P',
'ABATIN' : 'AW',
'ABATIN WELLNES' : 'AW',
'ABATIN WELLNESS' : 'AW',
'ABATIN WELLNESS CENTER' : 'AW',
'ABATIN WELLNESS DC' : 'AW',
'ABATIN WELNESS' : 'AW',
'ALTERNATIVE SOLUTIOINS' : 'AS',
'ALTERNATIVE SOLUTIONS' : 'AS',
'APELLES' : 'AC',
'APELLES CULTIVATION' : 'AC',
'CAPITAL CITY CARE' : 'CC',
'CAPITAL CITY CULTIVATION' : 'CC',
'DISTRICT GROWERS' : 'DG',
'DISTRICT GROWERS !' : 'DG',
'DISTRICT GROWERS!' : 'DG',
'HOLISTIC REMDIES' : 'HR',
'HOLISTIC REMEDIES' : 'HR',
'HOLISTIC REMEIDES' : 'HR',
'LIBERTY' : 'LB',
'ORGANIC WELLNESS' : 'OW',
'PHYTO' : 'PM',
'PHYTO MANAGEMENT' : 'PM',
'TAKOMA WELLNESS' : 'TK',
'TAKOMA WELLNESS CENTER' : 'TK'}
return find_replace
pull_date = time.strftime("%Y%m%d")
main_page = "https://www.stickyguide.com"
DC_page = urljoin(main_page, "/washington-dc/dispensary-finder")
response = get(DC_page)
DC_soup = BeautifulSoup(response.text, 'html.parser')
print(type(DC_soup))
disp_details = DC_soup.find_all('div', class_='details')
disp_locations = DC_soup.find_all('div', class_='location')
print(len(disp_details))
disp_count = 0
columns = ["pull_date", "disp_name", "disp_address", "prod_name", "prod_link", "prod_grower",
"prod_summary", "prod_category", "prod_subtype",
"prod_type", "prod_price", "prod_size"]
prod_df = pd.DataFrame(columns=columns)
for disp_details in disp_details:
# dispensary name
disp_name = disp_details.h3.a.text.strip()
print(disp_name)
# dispensary page
disp_link = urljoin(main_page, disp_details.h3.a['href'])
print(disp_link)
disp_address = disp_locations[disp_count].text.strip()
print(disp_address)
product_url_list = ["Flowers", "Pre_Rolls", "Concentrates",
"Edibles", "Topicals"]
for product_url in product_url_list:
# import html already scraped from site
disp_folder = disp_name.replace(" ", "_")
product_html = os.path.join(disp_folder, product_url + ".html")
print(product_html)
f = codecs.open(product_html, 'r', encoding="utf8")
response = f.read()
# parse the html
html_soup = BeautifulSoup(response, 'html.parser')
print(type(html_soup))
prod_containers = html_soup.find_all('div', class_='flower-details')
print(type(prod_containers))
print(len(prod_containers))
# loop through each container and pull out relevant information
for prod_container in prod_containers:
# product name
prod_name = prod_container.h5.a.text.strip()
print(prod_name)
# product link
prod_link = urljoin(main_page, prod_container.h5.a['href'])
'''prod_response = get(prod_link)
prod_html = BeautifulSoup(prod_response.text, 'html.parser')'''
# import html already scraped from site
trantab = str.maketrans("/':-,&", "______")
html_name = prod_name.translate(trantab) + ".html"
html_path = os.path.join(disp_folder, product_url, html_name)
f = codecs.open(html_path, 'r', encoding="utf8")
prod_html = BeautifulSoup(f.read(), 'html.parser')
try:
# pull description from individual page
prod_sum_container = prod_html.find('div', class_='product-bio')
prod_summary = prod_sum_container.p.text.strip()
# pull type from individual page
prod_type_containers = prod_html.find_all('a')
for prod_type_container in prod_type_containers:
if "product_category" in prod_type_container['href']:
if product_url in ["Concentrates", "Edibles", "Topicals"]:
prod_subtype = prod_type_container.text
else:
prod_subtype = ""
elif "type_name" in prod_type_container['href']:
prod_type = prod_type_container.text
# pull the prod_category from div class stat id prod_type
try:
prod_cat_container = prod_html.find(
'div', attrs={"class": "stat", "id": "product_type"})
prod_cat_subcontainer = prod_cat_container.find(
'div', class_="value")
prod_category = prod_cat_subcontainer.text.strip().replace(
"H", "Hybrid").replace("S", "Sativa").replace("I", "Indica")
except:
prod_category = "Unknown"
# grower, find and replace to standardize names
find_replace = grower_dict()
prod_grower = prod_container.div.text.strip()
prod_grower = multireplace(prod_grower, find_replace)
# navigate to product page and parse HTML
# loop through each div value container and pull price and size
prod_price_div = prod_container.find(
'div', class_='parent_price_box')
prod_price_values = prod_price_div.find_all('div', class_='value')
prod_price_sizes = prod_price_div.find_all(
'div', class_='attribute')
# loop through length of values list to get value and sizes
for i in range(len(prod_price_values)):
prod_price_value = prod_price_values[i]
# remove denomination span
for match in prod_price_value.find_all('span'):
match.extract()
prod_price = int(prod_price_value.text)
if product_url == "Pre_Rolls":
prod_size = "Each"
else:
prod_size = prod_price_sizes[i].text
# append row to output dataframe
prod_df = prod_df.append(pd.Series([pull_date, disp_name, disp_address, prod_name, prod_link, prod_grower, prod_summary,
prod_category, prod_subtype, prod_type, prod_price, prod_size], index=columns), ignore_index=True)
print("prod_name: {} prod_link: {} prod_grower: {} prod_summary: {} prod_category: {} prod_subtype: {} prod_type: {} prod_price: {} prod_size: {}"
.format(prod_name, prod_link, prod_grower, prod_summary, prod_category, prod_subtype, prod_type, prod_price, prod_size))
except:
print("Warning issue with {} for {}".
format(prod_html, disp_name))
disp_count+=1
# output dataframe to CSV
outfile = "Daily_Pull_" + pull_date + ".csv"
prod_df.to_csv(outfile, index=False)