前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python爬虫基础

python爬虫基础

作者头像
py3study
发布2020-01-19 11:18:32
3690
发布2020-01-19 11:18:32
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
Note:
一:简单爬虫的基本步骤

1.爬虫的前奏:
    (1)明确目的
    (2)找到数据对应的网页
    (3)分析网页的结构,找到数据的位置

2.爬虫第二步:__fetch_content方法
    模拟HTTP请求,向服务器发送这个请求,获取服务器返回给我们的Html
    用正则表达式提取我们要的数据

3.爬虫第三步:__analysis
    (1)找到一个定位标签或者是标识符,利用正则表达式找到需要的内容:
    它的选择原则是:
    唯一原则、就近原则、选择父级闭合标签
    (2)再找到的内容中进一步提取需要的数据,可能多次提取

4.精炼提取到的数据
    利用lambda表达式替换for循环

5.处理精炼后的数据

5.显示处理后的数据

二:程序规范
    1.注释
    2.空行的利用
    3.函数大小10-20行
    4.写平级方法并用主方法调用,避免多级嵌套方法!

四:补充
    beautiful Soup, scrapy爬虫框架
    爬虫、反爬虫、反反爬虫
    ip 被封  代理IP
五:总结
    (1)加强对正则表达式的练习
    (2)加强对lambda表达式的练习!
    (3)锻炼面向对象的思维模式
代码语言:javascript
复制
Code:
代码语言:javascript
复制
 1 """
 2 this module is used to spider data!
 3 """
 4 
 5 from urllib import request
 6 import re
 7 # 代替print的断点调试方法,特别重要!!!
 8 
 9 
10 class Spider:
11     """
12     this class is used to spider data!
13     """
14     url = 'https://www.panda.tv/cate/hearthstone'
15     root_pattern = '<div class="video-info">([\s\S]*?)</div>'     # 非贪婪模式
16     name_pattern = '</i>([\s\S]*?)</span>'
17     number_pattern = '<span class="video-number">([\s\S]*?)</span>'
18 
19     def __fetch_content(self):
20         """
21             this class is used to spider data!
22         """
23 
24         r = request.urlopen(self.url)   # 提取到html
25         html_s = r.read()
26         html = str(html_s, encoding='utf-8')
27 
28         return html
29 
30     def __analysis(self, html):
31         root_html = re.findall(self.root_pattern, html)     # list
32         # print(root_html[0])   # 第一次匹配的结果
33 
34         anchors =[]
35         for html in root_html:
36             name = re.findall(self.name_pattern, html)
37             number = re.findall(self.number_pattern, html)
38             anchor = {'name': name, 'number': number}
39             anchors.append(anchor)
40         # print(anchors[0])
41 
42         return anchors
43 
44     @staticmethod
45     def __refine(anchors):
46         i = lambda anchor: {'name': anchor['name'][0].strip(),  # 列表后面只有一个元素
47                             'number': anchor['number'][0].strip()
48                             }
49         return map(i, anchors)
50 
51     def __sort(self, anchors):      # 业务处理
52         anchors = sorted(anchors, key=self.__sort_seek, reverse=True)
53         return anchors
54 
55     @staticmethod
56     def __sort_seek(anchors):
57         r = re.findall('\d*', anchors['number'])
58         number = float(r[0])
59         if '万' in anchors['number']:
60             number *= 10000
61 
62         return number
63 
64     @staticmethod
65     def __show(anchors):
66         # for anchor in anchors:
67             # print(anchor['name'] + '-----' + anchor['number'])
68         for rank in range(0, len(anchors)):
69             print('rank' + str(rank + 1)
70                   + ' : ' + anchors[rank]['name']
71                   + '   ' + anchors[rank]['number'])
72 
73     def go(self):                           # 主方法(平级的函数)
74         html = self.__fetch_content()       # 获取到文本
75         anchors = self.__analysis(html)     # 分析数据
76         anchors = self.__refine(anchors)    # 精炼数据
77         # print(list(anchors))
78         anchor = self.__sort(anchors)
79         self.__show(anchor)
80 
81 
82 spider = Spider()
83 spider.go()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档