-
Notifications
You must be signed in to change notification settings - Fork 0
/
livingsocial_scraper.py
207 lines (185 loc) · 6.03 KB
/
livingsocial_scraper.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
#!/usr/bin/python3
from bs4 import BeautifulSoup
from lxml import html
from datetime import datetime
from datetime import timedelta
import sys
import re
import requests
from selenium import webdriver
from sql_miner import sql_miner
city = ""
class livingsocial_scraper:
def __init__(self, html, city_name, tracker):
#print("scraping the groupon deal.")
global city
city = city_name
soup = BeautifulSoup(html, "html.parser")
self.parser(soup, tracker)
def parser(self, soup, tracker):
deal_data = {}
deal_data['expired'] = 1
deal_data['alive'] = 1
dealoveralert = soup.find("div", {"id":"deal-over-alert"})
if dealoveralert != None:
deal_data['alive'] = 0
deal_data['expired'] = 0
# check if title can be scraped.
href = soup.find("meta",{"property":"og:url"})
if href != None:
href = href["content"]
name = soup.find("meta",{"property":"og:title"})
if name != None:
name = name["content"]
description = soup.find("div", itemprop="description")
if description != None:
try:
description = description.text
except Exception as e:
print(e)
pass
short_description = soup.find("meta", {"name":"description"})
if short_description != None:
try:
short_description = short_description["content"]
except Exception as e:
print(e)
pass
urgency_price = soup.find("p", {"id":"urgency-price"})
try:
if urgency_price != None:
urgency_price = urgency_price.text
except Exception as e:
print(e)
pass
#sale price, original price
sale_price, orig_price = 0, 0
dealbox = soup.find("ul", {"class":"unstyled ls-price_info ls-large_value"})
if dealbox != None:
multiprice = dealbox.find_all("li", recursive = False)
if multiprice != None:
option = multiprice[0].text
prices = self.digit_sum(option)
sale_price = prices[0]
if len(prices) > 1:
if ',' in prices[1]:
prices[1] = self.convert_numb(prices[1])
if '$' in prices[1]:
prices[1] = prices[1].split("$")[1]
try:
orig_price = float(prices[0].split("$")[1]) + float(prices[1])
except Exception as e:
print(e)
pass
else:
singleprice = dealbox.find("li", class_="ls-original_price")
#print("single price?", singleprice)
else:
dealbox = soup.find("div", class_="price-info")
if dealbox != None:
retail = dealbox.find("p", class_="retail-price")
if retail != None:
orig_price = retail.text.split()[0]
sale_price = soup.find("b", {"itemprop":"lowprice"})
if sale_price != None:
sale_price = sale_price.text
#savings = soup.find("p", class_="savings")
#if savings != None:
# savings = savings.text.split()[1]
#print(savings)
#number sold
bought_count = soup.find("div", class_="purchased")
if bought_count != None:
tmp_bcount = bought_count.find("span", class_="value")
if tmp_bcount != None:
bought_count = tmp_bcount.text
else:
bc_list = soup.find("ul", {"id":"stats_deal_list"})
bc_list = bc_list.find("li", {"id":"deal-purchase-count"})
bought_count = bc_list.text.split()[0]
#fine print
fine_print = soup.find("div", class_="fine-print")
if fine_print != None:
fine_print = fine_print.text
else:
fine_print = soup.find("section", class_="fine-print")
if fine_print != None:
fine_print = fine_print.text
#social networks
social_networks = soup.find("ul",{"class":"unstyled share-links"})
facebook_count, twitter_count = "" , ""
if social_networks != None:
sn_list = social_networks.find_all("li", recursive = False)
if len(sn_list) == 3:
facebook_count = sn_list[0].find("span", class_="share-count")
if facebook_count != None:
facebook_count = facebook_count.text
twitter_count = sn_list[1].find("span", class_="share-count")
if twitter_count != None:
twitter_count = twitter_count.text
else:
social_networks = soup.find("ul", {"class":"share-links"})
if social_networks != None:
sn_list = social_networks.find_all("li", recursive = False)
if len(sn_list) == 3:
facebook_count = sn_list[0].find("span", class_="share-count")
if facebook_count != None:
facebook_count = facebook_count.text
twitter_count = sn_list[1].find("span", class_="share-count")
if twitter_count != None:
twitter_count = twitter_count.text
#rating
rating = soup.find("div", class_="recommend")
if rating != None:
rating = rating.text
#address
address = soup.find("span", itemprop="address")
if address != None:
address = self.address_handler(address)
else:
t = soup.find("address", class_="vcard")
if t != None:
address = self.address_handler(t)
else:
address = ""
deal_data['name'] = name
deal_data['exp_date'] = ""
deal_data['orig_price'] = orig_price
deal_data['sale_price'] = sale_price
deal_data['description'] = description
deal_data['short_description'] = short_description
deal_data['fine_print'] = fine_print
deal_data['address'] = address
deal_data['city'], deal_data['href'] = city, href
deal_data['yelp_info'] = ""
deal_data['opt_count'], deal_data['opt_number'] = 0, 0
deal_data['parent_ID'] = 0
deal_data['bought_count'] = bought_count
deal_data['temp_price'] = urgency_price
deal_data['groupon_rating'] = rating
deal_data['facebook_count'] = facebook_count
deal_data['twitter_count'] = twitter_count
deal_data['sold_out'] = 0
# SQL insertion.
if (tracker == 0):
SQL_worker = sql_miner(deal_data)
SQL_worker.insert_single()
SQL_worker = sql_miner(deal_data)
SQL_worker.insert_single_price()
def digit_sum(self, str_price):
return [i for i in str_price.split() if "$" in i]
def convert_numb(self, str_price):
t = ""
temp = str_price.split(".") #check if there are any cents unit.
for d in temp[0]:
if d.isdigit():
t+=d
if len(temp) > 1:
t+=temp[1]
return t
def address_handler(self, address):
ta = ""
p = address.find_all("meta", recursive = False)
for i in p:
ta += i["content"] + " "
return ta