Python3简单的爬虫项目 爬取虎牙主播名字 人气

代码

from urllib import request
import re

class Spider():

    url='https://www.huya.com/g/wzry'
    #正则
    root_pattern='([\s\S]*?)
' name_pattern='([\s\S]*?)' number_pattern='([\s\S]*?)' #获取要爬取的html 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[0],'number':number} anchors.append(anchor) print(anchors[0]) return anchors #过滤空格以及换行符 def __refine(self,anchors): #lambda表达式 简化函数 l=lambda anchor:{ 'name':anchor['name'][0], 'number':anchor['number'][0] } #映射函数,依次作用 return map(l,anchors) def __sort(self,anchors): #filter,对列表进行迭代排序,指定number进行排序 anchors=sorted(anchors,key=self.__sort_seed) 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()

爬取的HTML参考

Python3简单的爬虫项目 爬取虎牙主播名字 人气_第1张图片

结果

Python3简单的爬虫项目 爬取虎牙主播名字 人气_第2张图片

你可能感兴趣的:(Python)