• 主要是体现代码的规范性

    from urllib import request
    import re 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()
    htmls = str(htmls, encoding='utf-8')
    return htmls 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)
    # print(root_html[1])
    # print(anchors[1])
    return anchors def __refine(self, anchors):
    L = lambda anchor: {"name": anchor['name'][0].strip(), 'number': anchor['name'][1]}
    return map(L, anchors) def __sort(self, anchors):
    anchors = sorted(anchors, key=self.__sort_seed, reverse=True)
    return anchors def __sort_seed(self, anchor):
    r = re.findall("\d*", anchor["number"])
    number = float(r[0])
    if '万' in anchor['number']:
    number = number * 10000
    return number def __show(self, anchors):
    for rank in range(0, len(anchors)):
    print("排名:"+str(rank+1)+" 主播:" + anchors[rank]['name'] +
    "--------" + "观看人数:" +
    anchors[rank]['number']) def go(self):
    htmls = self.__fetch_content()
    anchors = self.__analysis(htmls)
    anchors = list(self.__refine(anchors))
    anchors = self.__sort(anchors)
    self.__show(anchors)
    print(len(anchors))
    # print(anchors) spider = Spider()
    spider.go()

      

使用Python自带的库和正则表达式爬取熊猫直播主播观看人气的更多相关文章

  1. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  2. python 3.6 urllib库实现天气爬取、邮件定时给妹子发送天气

    #由于每天早上要和妹子说早安,于是做个定时任务,每天早上自动爬取天气,发送天气问好邮件##涉及模块:#(1)定时任务:windows的定时任务#             配置教程链接:http://b ...

  3. 爬虫基本库request使用—爬取猫眼电影信息

    使用request库和正则表达式爬取猫眼电影信息. 1.爬取目标 猫眼电影TOP100的电影名称,时间,评分,等信息,将结果以文件存储. 2.准备工作 安装request库. 3.代码实现 impor ...

  4. [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】

    [python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...

  5. 初识python 之 爬虫:使用正则表达式爬取“糗事百科 - 文字版”网页数据

    初识python 之 爬虫:使用正则表达式爬取"古诗文"网页数据 的兄弟篇. 详细代码如下: #!/user/bin env python # author:Simple-Sir ...

  6. 初识python 之 爬虫:使用正则表达式爬取“古诗文”网页数据

    通过requests.re(正则表达式) 爬取"古诗文"网页数据. 详细代码如下: #!/user/bin env python # author:Simple-Sir # tim ...

  7. [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息

    [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2018-07-21 23:53:02 larger5 阅读数 4123更多 分类专栏: 网络爬虫   版权声明: ...

  8. 14-Requests+正则表达式爬取猫眼电影

    '''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析 ...

  9. 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...

随机推荐

  1. [笔记] Ubuntu 18.04源码编译安装OpenCV 4.0流程

    标准常规安装方法安装的OpenCV版本比较低,想尝鲜使用4.0版本,只好源码安装. 安装环境 OS:Ubuntu 18.04 64 bit 显卡:NVidia GTX 1080 CUDA:10.0 c ...

  2. PKU 1201 Intervals(差分约束系统+Spfa)

    题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...

  3. 实战DVWA!

    DVWA漏洞训练系统,来个大图^-^ 1.首先试了下DVWA的命令执行漏洞command execution     这是我在Low级别上测试的,另外附上low级别代码: <?php if( i ...

  4. bind函数的作用

    面向连接的网络应用程序分为客户端和服务器端.服务器端的执行流程一般为4步,客户端程序相对简单,一般需要两个步骤. 服务器端执行流程4步如下: (1)调用socket函数,建立一个套接字,该套接字用于接 ...

  5. JS正则表达式从入门到入土(10)—— 字符串对象方法

    字符串对象方法 search方法 String.prototype.search(reg) search方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,方法返回第一个匹配结果的 ...

  6. 利用python获取nginx服务的ip以及流量统计信息

    #!/usr/bin/python #coding=utf8 log_file = "/usr/local/nginx/logs/access.log" with open(log ...

  7. (转)国内yum源的安装(163,阿里云,epel)

    国内yum源的安装(163,阿里云,epel) ----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...

  8. Python学习笔记(十二)—Python3中pip包管理工具的安装【转】

    本文转载自:https://blog.csdn.net/sinat_14849739/article/details/79101529 版权声明:本文为博主原创文章,未经博主允许不得转载. https ...

  9. 树梅派配置ad-hoc网络

    树梅派配置ad-hoc网络 更新与安装 1.更改源/etc/apt/source.list: http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian ...

  10. linux下如何制作ext4文件系统镜像

    1.生成一个空的2MiB文件 dd if=/dev/zero of=rootfs.ext4 bs=1024 count=2048 (指定每一块大小为1024字节,一共又2048块,那么就是2048 * ...