# -*- coding: utf-8 -*-
"""
Created on Tue Nov 14 16:39:25 2017
@author: masserd
"""
from atexit import register
from re import compile
from threading import Thread
from time import ctime
from urllib.request import urlopen,Request
REGEX = compile('#([\d,]+) in Books')
AMZN = 'https://www.amazon.com/dp/'
ISBNs = {'0132269937':'Core Python Programming',
'0132356139':'Python Web Development with Django',
'0137143419':'Python Fundamentals',}
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36'
headers = { 'User-Agent' : user_agent }
def getRanking(isbn):
url = '%s%s' % (AMZN,isbn)
req = Request(url)
req.add_header("User-Agent",user_agent)
req.add_header("GET",url)
req.add_header("Host","www.amazon.com")
req.add_header("Referer","https://www.amazon.com/dp/0132269937")
page = urlopen(req)
data = page.read()
page.close()
return REGEX.findall(data)[0]
def showRanking(isbn):
print('- %r ranked %s' %(ISBNs[isbn],getRanking(isbn)))
def main():
print('At',ctime(),'on Amazon...')
threads = []
for isbn in ISBNs:
t = Thread(target=showRanking,args=(isbn,))
threads.append(t)
for i in range(len(ISBNs)):
threads[i].start()
for i in range(len(ISBNs)):
threads[i].join()
print('all DONE at:',ctime())
@register
def atexit():
print('all DONE at:',ctime())
if __name__ == '__main__':
main()# -*- coding: utf-8 -*-
"""
Created on Tue Nov