Python(十一) 原生爬虫

一、分析抓取目的确定抓取页面
 
#爬取主播人气排行
二、整理爬虫常规思路
 
爬虫前奏

明确目的
找到数据对应的网页
分析网页的结构找到数据所在的标签位置

模拟 HTTP 请求, 向服务器发送这个请求, 获取到服务器返回给我们的HTML
用正则表达式提取我们要的数据(名字,人数)

 

三、 VSCode中调试代码
 
F5 启动 和vs 调试一样
 BeautifulSoup , Scrapy
爬虫、 反爬虫、反反爬虫
ip 封
代理 ip库

 

五、数据提取层级分析及原则三、正则分析HTML、正则分析获取名字和人数
from urllib import request
import re
#断点调试 有坑 7i
class  Spider():
    url = 'https://www.panda.tv/cate/lol'
    root_pattern ='<div class="video-info">([\s\S]*?)</div>'
    name_pattern = '</li>([\s\S]*?)</span>' 
    number_pattern = '<span class="video-number">([\s\S]*?)</span>'

    def __fetch_content(self):
        r = request.urlopen(Spider.url)
        #bytes
        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(anchors[0])
        return anchors

    def __refine(self, anchors):
        l = lambda anchor:{
            'name':anchor['name'][0].strip(),
            'number':anchor['number'][0]
            }
        return map(l,anchors)

    def go(self):
        htmls = self.__fetch_content()
        anchors = self.__analysis(htmls)
        anchors =list(self.__refine(anchors))
        print(anchors[0])

s = Spider()
s.go()

结果:
{'name': 'LOL丶摇摆哥', 'number': '26.8万'}

 

八、 数据精炼、 sorted 排序
 
from urllib import request
import re
#断点调试 坑 7i
class  Spider():
    url = 'https://www.panda.tv/cate/lol'
    root_pattern ='<div class="video-info">([\s\S]*?)</div>'
    name_pattern = '</li>([\s\S]*?)</span>' 
    number_pattern = '<span class="video-number">([\s\S]*?)</span>'

    # 获取数据的页面
    def __fetch_content(self):
        r = request.urlopen(Spider.url)
        #bytes
        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(anchors[0])
        return anchors

    # 数据取杂质(空格换行)strip() 字符串去空格换行
    def __refine(self, anchors):
        l = lambda anchor:{
            'name':anchor['name'][0].strip(),
            'number':anchor['number'][0]
            }
        return map(l,anchors) #map类  对字典每一个序列进行l这个函数

    # 对抓取的数据进行排序 reverse=True 倒序
    def __sort(self, anchors):
        anchors = sorted(anchors, key=self.__sort_seed, reverse=True)
        return anchors

    # 给 key 写的函数 说明用那个进行排序
    def __sort_seed(self, anchors):
        r = re.findall('\d*', anchors['number'])
        number = float(r[0])
        if '' in anchors['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()
        anchors = self.__analysis(htmls)
        anchors =list(self.__refine(anchors))
        print(anchors[0])
        anchors= self.__sort(anchors)
        self.__show(anchors[:20])

s = Spider()
s.go()

结果:
{'name': 'LOL丶摇摆哥', 'number': '20.2万'}
rank 1   : 贾克虎丶虎神    96.9万人
rank 2   : LOL丶摇摆哥    20.2万人
rank 3   : LPL熊猫官方直播    12.1万人
rank 4   : WUCG官方直播平台    8.4万人
rank 5   : 温州丶黄小明    5.1万人
rank 6   : 暴君aa    4.6万人
rank 7   : 顺顺套路王    3.1万人
rank 8   : 火苗OB解说    2.5万人
rank 9   : 兰晨丶    1.1万人
rank 10   : 海洋OvO    1.9万人
rank 11   : 小马哥玩盖伦    1.6万人
rank 12   : 牛老师丶    1.5万人
rank 13   : Riot国际赛事直播间    1.5万人
rank 14   : 小白Mini    7361人
rank 15   : 一个很C的稻草人    7223人
rank 16   : 抗寒使者    4976人
rank 17   : 小麦子鲜奶油    4902人
rank 18   : 祝允儿    4574人
rank 19   : 请叫我大腿岩丶    4201人
rank 20   : 李小青ZJ    3838人

 

posted @ 2018-08-20 16:33  革凡  阅读(1251)  评论(0编辑  收藏  举报