Python 爬虫

简介:

初学 Python, 结合网络资料整理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
#coding=UTF-8

'''
爬虫 v 1.0 (L)
'''

import time
import sys
import re
#====================================

根据 Python 的版本选择需要加载的模块

#====================================
if (sys.version)[0] == '3':
import urllib.request as urlreq
import _thread as thread
else:
import urllib2 as urlreq
import thread as thread

class Spider:
def init(self):
self.pageidx = 1 # 标记页码
self.items = [] # 存放正文内容
self.read = False # 存放是否继续 True:继续; False:停止
self.restr = ''
self.user_agent = ['Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', '', ''] # UA 列表
self.header = { 'User-Agent' : (self.user_agent)[0] }# 存放浏览器UA(防止网站限定header)

#=========================================================
# 功能: 获取网页内容(按规则)
# 参数: self; url:页面地址;  recom:正则
#=========================================================
def GetItems(self, url, recom):
    request = urlreq.Request(url, headers = self.header)
    html = urlreq.urlopen(request).read()

    # 页面编码转换 (encode: unicode->其它; decode: 其它->unicode)
    uhtml = html.decode("UTF-8")

    # 根据规则从页面选定内容;
    return re.findall(recom, uhtml)

#=========================================================
# 功能: 加载网页内容
# 参数: self;
#=========================================================
def LoadItems(self, url, recom):

    while self.read:
        # 判断当items中的内容不足两条时,请求下一页数据并存入items列表中
        if len(self.items) < 2:
            try:
                uri = url.replace("$PX$", str(self.pageidx))
                item = self.GetItems(uri, recom)
                self.items.append(item)
                self.pageidx += 1
            except:
                print('[ ' + uri + ' ] 被外星人劫持[访问失败]....')
                self.read = False
        else:
            time.sleep(1)

#=========================================================
# 功能: 现实网页内容(并接收用户输入)
# 参数: self;
#=========================================================
def ShowItems(self, items, pidx = 1):
    for item in items:
        print(u'[%d]' % pidx, item[1], '\n', item[0].replace('\n', '').replace('<br/>', '\n'))
        print('-' * 70)
        option = input().upper()
        if option == 'Q':
            self.read = False
            return 9
        elif option == 'R':
            self.pageidx = 1 # 初始值
            return 0
    return 1

#=========================================================
# 功能: 现实网页内容(并接收用户输入)
# 参数: self;
#=========================================================
def Launcher(self, url, recom):
    self.read = True
    pidx = self.pageidx

    print(u'正在加载中请稍候......\n', '=' * 70)

    thread.start_new_thread(self.LoadItems, (url, recom))

    while self.read:
        if self.items:
            item = self.items[0]
            del self.items[0]
            ret = self.ShowItems(item, pidx)
            if ret == 9:
                print(u'即将推出阅读....')
                break
            else:
                pidx += ret

def usage():
print(u"""

-   程序:糗百爬虫(阅读文章)
-   版本:0.1
-   作者:L
-   日期:2015-06-30
-   语言:Python 3.3.5
-   操作:Q:退出阅读; R:刷新到第一页; 回车:阅读
-   功能:按下回车依次浏览今日的糗百热点
---------------------------------------------
""")

if name == "main":
usage()

    url = 'http://m.qiushibaike.com/hot/page/$PX$'
    # re.S是任意匹配模式(如:匹配换行符)
    recom = re.compile(r'<div class="content">(.*?)<!--(.*?)-->.*?</div>', re.S)

    input('浏览今日的糗百内容(任意键):')
    spider = Spider()
    spider.Launcher(url, recom)
    print('欢迎再次使用....')

 本文转自 技术花妞妞 51CTO博客,原文链接:http://blog.51cto.com/xiaogongju/2061749

相关文章
|
7天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
11天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱
|
30天前
|
数据采集 Python
爬虫实战-Python爬取百度当天热搜内容
爬虫实战-Python爬取百度当天热搜内容
65 0
|
17天前
|
数据采集 安全 Python
python并发编程:Python实现生产者消费者爬虫
python并发编程:Python实现生产者消费者爬虫
24 0
python并发编程:Python实现生产者消费者爬虫
|
29天前
|
数据采集 数据挖掘 调度
异步爬虫实践攻略:利用Python Aiohttp框架实现高效数据抓取
本文介绍了如何使用Python的Aiohttp框架构建异步爬虫,以提升数据抓取效率。异步爬虫利用异步IO和协程技术,在等待响应时执行其他任务,提高效率。Aiohttp是一个高效的异步HTTP客户端/服务器框架,适合构建此类爬虫。文中还展示了如何通过代理访问HTTPS网页的示例代码,并以爬取微信公众号文章为例,说明了实际应用中的步骤。
|
12天前
|
数据采集 存储 前端开发
Python爬虫如何快速入门
写了几篇网络爬虫的博文后,有网友留言问Python爬虫如何入门?今天就来了解一下什么是爬虫,如何快速的上手Python爬虫。
17 0
|
25天前
|
数据采集 存储 Web App开发
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧
|
27天前
|
数据采集 前端开发 JavaScript
Python爬虫零基础到爬啥都行
Python爬虫项目实战全程实录,你想要什么数据能随意的爬,不管抓多少数据几分钟就能爬到你的硬盘,需要会基本的前端技术(HTML、CSS、JAVASCRIPT)和LINUX、MYSQL、REDIS基础。
20 1
Python爬虫零基础到爬啥都行
|
1月前
|
数据采集 Web App开发 数据安全/隐私保护
Python爬虫-使用代理伪装IP
介绍代理,设置代理,使用代理伪装IP案例
23 0
|
1月前
|
数据采集 JSON NoSQL
python爬虫 Appium+mitmdump 京东商品
python 爬虫 Charles + appium + mitmproxy 实现 app 京东商品数据获取
29 0

热门文章

最新文章