阅读背景:

python中‘can't use a string pattern on a bytes-like object’错误

来源:互联网 
# -*- 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 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: