Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3625038
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2019-02-25 11:01:54

访问熊猫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 /]#


阅读(4155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~