Skip to content

Instantly share code, notes, and snippets.

@eliask
Created October 16, 2018 14:11
Show Gist options
  • Save eliask/702fd23f69394f54c781df0d6dc1b900 to your computer and use it in GitHub Desktop.
Save eliask/702fd23f69394f54c781df0d6dc1b900 to your computer and use it in GitHub Desktop.
verkkokauppa.com sales 2018-10
import requests
headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'fi',
'User-Agent': 'Mozilla/5.0',
'Accept': '*/*',
'Referer': 'https://www.verkkokauppa.com/fi/syyssiivous',
'Connection': 'keep-alive',
}
#/fastsearch response
'''
{
"currentPage": 2,
"totalItems": 870,
"resultSetSize": 60,
"brands": [...],
"links": {
"prev": "\/api\/v1\/search\/?query=%23syyssiivous&onlyprimarycategories=true&minprice=0&maxprice=0&page=1&sort=popularity&marketing=true&inactive=0&latest=0&resultsetsize=60&eol=0",
"next": "\/api\/v1\/search\/?query=%23syyssiivous&onlyprimarycategories=true&minprice=0&maxprice=0&page=3&sort=popularity&marketing=true&inactive=0&latest=0&resultsetsize=60&eol=0"
},
"matches": [
],
"categories": {
...
"10480c": 1,
"10685c": 3,
"10684c": 1
},
"results": [
...
429278,
438803,
363304,
430997
]
}
'''
#/pids response
'''
[
{
"active": true,
...
"name": "Braun Series 5 5147S Wet&Dry -parranajokone + nahkainen matkakotelo",
"package": {
"width": 200,
"height": 80,
"depth": 260,
"volume": 4160000,
"weight": 1080
},
"pid": 433166,
"price": {
"current": 129,
"currentTaxless": 104.03,
"currentFormatted": "129,00",
"original": 199.9,
"originalTaxless": 161.21,
"originalFormatted": "199,90",
"discountAmount": 70.9,
"discountPercentage": 35,
"taxRate": 24,
"discount": {
"id": 61710,
"beginAt": "2018-09-27T06:00:00.000Z",
"endAt": "2018-10-31T21:59:00.000Z",
"name": "Huippuhalpa poistotuote!",
"discountType": 2
},
"deposit": null,
"unit": null
},
"productId": "56688",
"rating": {
"reviewCount": 1,
"averageOverallRating": 4,
"recommendedCount": 1
},
...
"vak": false,
"visible": 1,
"warranty": "24",
"updatedAt": "2018-10-16T05:00:58.679Z",
"verifiedAt": "2018-10-16T13:12:58.970Z",
"updateCount": 9268,
"updateStartTime": "2018-10-16T13:12:58.925Z"
},
...
]
'''
def get_product_ids():
params = (
('marketing', 'true'),
('onlyprimarycategories', 'true'),
('page', '1'),
('query', '#syyssiivous'),
('sort', 'popularity'),
)
with requests.Session() as session:
for page in range(1,100):
params = dict(params)
params['page'] = page
response = session.get('https://www.verkkokauppa.com/api/v1/fastsearch', headers=headers, params=params)
res = response.json()
num_pages = res['totalItems'] / res['resultSetSize']
print(page, res['totalItems'], res['resultSetSize'], num_pages)
if page >= num_pages:
break
yield from res['results']
from itertools import islice
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
def get_product_pages():
params = (
('pids', '181749,183645,197042,229888,237,255557,265099,285821,293678,295040,332387,332651,332858,33772,339533,346130,357703,360991,369875,405041,407366,413090,416402,416411,416447,427217,427496,427589,427673,427682,429278,429302,430280,430502,430976,433163,440915,443390,446342,447674,457905,458121,488268,492123,6943'),
('fresh', 'false'),
)
params = dict(params)
with requests.Session() as session:
for pids in chunk(get_product_ids(), 50):
params['pids'] = ','.join(str(pid) for pid in pids)
response = session.get('https://www.verkkokauppa.com/resp-api/product', headers=headers, params=params)
res = response.json()
yield from res
def get_products():
for p in get_product_pages():
yield dict(
name=p['name'],
current_price=p['price']['current'],
original_price=p['price']['original'],
product=p,
)
ps=[]
for p in get_products():
print(p['name'], round(100*p['current_price']/p['original_price']), p['current_price'], p['original_price'])
ps.append(p)
print('\n'*5)
for p in sorted(ps, key=lambda x: x['current_price']/(1+x['original_price'])):
print(f"{p['name']} SALE {round(100-100*p['current_price']/p['original_price'])}% PRICE: {p['current_price']} ORIG PRICE: {p['original_price']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment