博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
原生爬虫(爬取熊猫直播人气主播排名)
阅读量:6876 次
发布时间:2019-06-26

本文共 2207 字,大约阅读时间需要 7 分钟。

此代码未采用任何反爬虫策略 ''''    This is a module'''import refrom urllib import request# 断点调试class Spider():    '''        This is a class    '''    # 私有方法    # 匹配所有字符 [\s\S]*? 非贪婪    url='https://www.panda.tv/all?pdt=1.27.psbar-menu.0.1oj9bbkfjbh'    root_pattern = '
([\w\W]*?)
' name_pattern = '([\w\W]*?)' number_pattern = '([\w\W]*?)' 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()

运行该.py文件,终端显示部分结果如下:

 

 

转载于:https://www.cnblogs.com/KSYoon/p/9662812.html

你可能感兴趣的文章
mysql复制一个新的数据库
查看>>
matlab 控制(helpqq)
查看>>
复杂系统是如何崩溃的 - 翻译
查看>>
Linux基础笔记vi
查看>>
10-利用思维导图梳理JavaSE-Java 集合
查看>>
在线图片翻转、旋转工具
查看>>
jquery利用sort方法对json数据排序
查看>>
要复习内容
查看>>
【Qt笔记】使用流处理 XML
查看>>
指针的使用
查看>>
5-pandas基础运算
查看>>
php判断IP跳转区域二级域名
查看>>
百度webupload--上传图片功能---插件使用
查看>>
Java深、浅克隆(clone)
查看>>
如何在View上不用UIImageView重新绘制一张图片?
查看>>
实现控制器(Controller)
查看>>
Intent传值
查看>>
好多年前写的一个C++事件回调工具
查看>>
python3使用logging日志记录
查看>>
servlet3中jar的web资源携带
查看>>