-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsephora_scraper.py
More file actions
63 lines (49 loc) · 1.49 KB
/
Copy pathsephora_scraper.py
File metadata and controls
63 lines (49 loc) · 1.49 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
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import *
import requests
import json
import re
import pickle
url = 'http://www.sephora.com/eye-cream-dark-circles?pageSize=-1'
class Render(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self._loadFinished)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def _loadFinished(self, result):
self.frame = self.mainFrame()
self.app.quit()
r = Render(url)
html = r.frame.toHtml()
products = unicode(re.search(r"(?<=products).*?(?=meta)", html).group(0)).replace('}}],"', '}}').replace('":[{', '{')
p = products.split('},')
prods = []
for product in p[:-1]:
prods.append(product + '}')
prods.append(p[-1])
products = {}
for prod in prods:
product = json.loads(prod)
key = product['product_url']
products[key] = {
'rating': product['rating'],
'brand': product['brand_name'],
'id': product['id'],
'name': product['display_name']
}
url_base = 'http://www.sephora.com'
for product in products.keys():
url = url_base + product
response = requests.get(url)
page = response.text
try:
reviews = int(unicode(re.search(r"(?<=\n).*?(?= review)", page).group(0)).strip())
except ValueError:
reviews = 0
products[product]['num_reviews'] = reviews
with open('sephora_data.pkl', 'w') as picklefile:
pickle.dump(products, picklefile)