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
| import re from urllib import request
class Spider(): url = "https://www.panda.tv/cate/lol" root_pattern = '<div class="video-info">([\s\S]*?)</div>' name_pattern = '</i>([\s\S]*?)</span>' number_pattern = '<span class="video-number">([\s\S]*?)</span>'
def __fetch_content(self): r = request.urlopen(Spider.url) htmls = r.read() return str(htmls, encoding='utf-8')
def __analysis(self, htmls): root_html = re.findall(Spider.root_pattern, htmls) anchors = [] for html in root_html: name = re.findall(Spider.name_pattern, html) number = re.findall(Spider.number_pattern, html) anchor = { 'name': name, 'number': number } anchors.append(anchor) return anchors
def __refine(self, anchors): l = lambda anchor: { 'name': anchor['name'][0].strip(), 'number': anchor['number'][0] } return map(l, anchors)
def __sort(self, anchors): anchors = sorted(anchors, key=self.__sort_seed, reverse=True) return anchors
def __show(self, anchors): for rank in range(0, len(anchors)): print('rank ' + str(rank + 1) + ':' + anchors[rank]['name'] + ':' + anchors[rank]['number'])
def __sort_seed(self, anchor): r = re.findall('\d*', anchor['number']) number = float(r[0]) if '万' in anchor['number']: number *= 10000 return number
def go(self): htmls = self.__fetch_content() anchors = self.__analysis(htmls) anchors = list(self.__refine(anchors)) anchors = self.__sort(anchors) anchors = self.__show(anchors)
spider = Spider() spider.go()
|