原生爬虫(爬取熊猫直播人气主播排名)

Posted ksyoon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生爬虫(爬取熊猫直播人气主播排名)相关的知识,希望对你有一定的参考价值。

‘‘‘‘
    This is a module
‘‘‘

import re

from urllib import request
# 断点调试

class Spider():
    ‘‘‘
        This is a class
    ‘‘‘
    # 私有方法
    # 匹配所有字符 [sS]*? 非贪婪
    url=https://www.panda.tv/all?pdt=1.27.psbar-menu.0.1oj9bbkfjbh
    root_pattern = <div class="video-info">([wW]*?)</div>
    name_pattern = </i>([wW]*?)</span>
    number_pattern = <span class="video-number">([wW]*?)</span>
    def __fetch_content(self):

        # This is a HTTP request
        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[0])
        # print(anchors[0])
        # print(anchors)
        return anchors
        
    def __refine(self, anchors):
        
        # 匿名函数lambda
        l = lambda anchor: {name:anchor[name][0].strip(),number:anchor[number][0]}
        # r = map(l, anchors)
        # print(r)
        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 *= 10000

        return number

    def __show(self, anchors):
        for rank in range(0, len(anchors)):
            print(rank+str(rank+1)+:+anchors[rank][name]+ +anchors[rank][number])

    def go(self):
        htmls = self.__fetch_content()
        # self.__analysis(htmls)
        anchors = self.__analysis(htmls) 
        # anchors = self.__refine(anchors)
        anchors = list(self.__refine(anchors))
        # print(anchors)
        # anchors = list(self.__refine(anchors))
        anchors = self.__sort(anchors)
        self.__show(anchors)
        # print(anchors)

spider = Spider()
spider.go()

 

以上是关于原生爬虫(爬取熊猫直播人气主播排名)的主要内容,如果未能解决你的问题,请参考以下文章

十原生爬虫实战

2019最火的直播平台有哪些?2019直播前十名排名

python实战之原生爬虫(爬取熊猫主播排行榜)

Python 原生爬虫

Python项目实战:带领你爬取某主播的人气指数和昵称

爬取斗鱼LOL主播人气数据,并显示排行榜 [网络爬虫] [应用案例][请求头][模块]