访问熊猫TV查看LOL主播及关注人数并按顺序打印:
[root@localhost /]# cat pachong.py
#!/bin/python
from urllib import request
import re
class Spider():
url=''
root_pattern = '
([\s\S]*?)
'
name_pattern = '([\s\S]*?)'
number_pattern = '
([\s\S]*?)'
def __fetch_content(self):
r=request.urlopen(Spider.url)
htmls=r.read()
htmls=str(htmls,encoding='utf-8')
return htmls
def __analysis(self,htmls)
root_htmls=re.findall(Spider.root_pattern,htmls)
anchors=[]
for html in root_htmls:
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 __sort_seed(self,anchor):
return anchor['number']
def __show(self,anchors):
for anchor in anchors:
print(anchor['name']+'---------'+anchor['number'])
def go(self):
htmls=self.__fetch_content()
anchors=self.__analysis(htmls)
anchors=list(self.__refine(anchors))
anchors=self.__sort(anchors))
self.__show(anchors)
spider=Spider()
spider.go()
[root@localhost /]#
阅读(4236) | 评论(0) | 转发(0) |