1 """
2 this module is used to spider data!
3 """
4
5 from urllib import request
6 import re
7 # 代替print的斷點調試方法,特別重要!!!
8
9
10 class Spider:
11 """
12 this class is used to spider data!
13 """
14 url = 'https://www.panda.tv/cate/hearthstone'
15 root_pattern = '<div class="video-info">([\s\S]*?)</div>' # 非貪婪模式
16 name_pattern = '</i>([\s\S]*?)</span>'
17 number_pattern = '<span class="video-number">([\s\S]*?)</span>'
18
19 def __fetch_content(self):
20 """
21 this class is used to spider data!
22 """
23
24 r = request.urlopen(self.url) # 提取到html
25 html_s = r.read()
26 html = str(html_s, encoding='utf-8')
27
28 return html
29
30 def __analysis(self, html):
31 root_html = re.findall(self.root_pattern, html) # list
32 # print(root_html[0]) # 第一次匹配的結果
33
34 anchors =[]
35 for html in root_html:
36 name = re.findall(self.name_pattern, html)
37 number = re.findall(self.number_pattern, html)
38 anchor = {'name': name, 'number': number}
39 anchors.append(anchor)
40 # print(anchors[0])
41
42 return anchors
43
44 @staticmethod
45 def __refine(anchors):
46 i = lambda anchor: {'name': anchor['name'][0].strip(), # 列表後面只有一個元素
47 'number': anchor['number'][0].strip()
48 }
49 return map(i, anchors)
50
51 def __sort(self, anchors): # 業務處理
52 anchors = sorted(anchors, key=self.__sort_seek, reverse=True)
53 return anchors
54
55 @staticmethod
56 def __sort_seek(anchors):
57 r = re.findall('\d*', anchors['number'])
58 number = float(r[0])
59 if '萬' in anchors['number']:
60 number *= 10000
61
62 return number
63
64 @staticmethod
65 def __show(anchors):
66 # for anchor in anchors:
67 # print(anchor['name'] + '-----' + anchor['number'])
68 for rank in range(0, len(anchors)):
69 print('rank' + str(rank + 1)
70 + ' : ' + anchors[rank]['name']
71 + ' ' + anchors[rank]['number'])
72
73 def go(self): # 主方法(平級的函數)
74 html = self.__fetch_content() # 獲取到文本
75 anchors = self.__analysis(html) # 分析數據
76 anchors = self.__refine(anchors) # 精煉數據
77 # print(list(anchors))
78 anchor = self.__sort(anchors)
79 self.__show(anchor)
80
81
82 spider = Spider()
83 spider.go()