-
Notifications
You must be signed in to change notification settings - Fork 0
/
barcode.py
55 lines (47 loc) · 2.03 KB
/
barcode.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
import os
import time
import csv
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import sqlite3
from dotenv import load_dotenv
load_dotenv()
print("Scraping Barcodes...")
options = Options()
options.headless = True
d = webdriver.Firefox(options=options)
d.get('https://www.booker.co.uk/home')
d.find_element_by_id('CustomerNumber').send_keys(
os.getenv('BOOKER_ACCOUNT'), Keys.RETURN)
time.sleep(2)
d.find_element_by_id('Email').send_keys(os.getenv('BOOKER_EMAIL'))
d.find_element_by_id('Password').send_keys(
os.getenv('BOOKER_PASSWORD'), Keys.RETURN)
time.sleep(3)
with open('barcode.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['barcode', 'code', 'sub_cat_code'])
writer.writeheader()
for row in sqlite3.connect('stores.db').execute("SELECT * FROM sitemap").fetchall():
# for row in [["CS13_201630", "Pet Food", "Cats"], ["CS13_201640", "Pet Food", "Dogs"]]:
# for row in [["CS13_200020", "Beer and the rest", "Cider"]]:
d.get(
f'https://www.booker.co.uk/products/product-list?categoryName={row[1]}')
page_count = len(d.find_elements_by_css_selector(
'.page-link[rel=next]'))
for page_index in (range(page_count) if page_count > 0 else range(1)):
d.get(
f'https://www.booker.co.uk/products/product-list?categoryName={row[1]}&pageIndex={page_index}')
d.get(
f'https://www.booker.co.uk/products/print-product-list-ungroup?printType=ProductList')
for tr in d.find_elements_by_css_selector('.table-desktop tbody tr'):
try:
barcode = tr.find_element_by_tag_name(
'svg').get_attribute('jsbarcode-value')
except:
barcode = ""
writer.writerow({
'barcode': barcode,
'code': tr.find_element_by_css_selector('td:nth-of-type(2)').text
})
d.quit()