Python 爬虫 之 阅读呼叫转移(二)
上一篇博客我们成功地从网页上爬下了小说的一个章节。理所当然地。接下来我们要把整本小说都爬下来。首先。我们要把程序从原来的读完一章就结束,改成读完一章之后能够继续进行下一章的阅读。
注意到每一个小说章节的网页以下都有下一页的链接。
通过查看网页源码。略微整理一下( 不显示了)。我们能够看到这一部分的 HTML 是以下这样的格式的:
<div id="footlink"> <script type="text/javascript" charset="utf-8" src="/scripts/style5.js"></script> <a href="http://www.quanben.com/xiaoshuo/0/910/59301.html">上一页</a> <a href="http://www.quanben.com/xiaoshuo/0/910/">返回文件夹</a> <a href="http://www.quanben.com/xiaoshuo/0/910/59303.html">下一页</a> </div>
上一页 返回文件夹 下一页 都在一个 id 为 footlink 的 div 中。假设想要对每一个链接进行匹配的话,会抓取到网页上大量的其它链接,可是 footlink 的 div 仅仅有一个啊!我们能够把这个 div 匹配到。抓下来,然后在这个抓下来的 div 里面再匹配 <a> 的链接,这时就仅仅有三个了。仅仅要取最后一个链接就是下一页的 url 的,用这个 url 更新我们抓取的目标 url ,这样就能一直抓到下一页。用户阅读逻辑为每读一个章节后。等待用户输入。假设是 quit 则退出程序,否则显示下一章。
基础知识:
上一篇的基础知识加上 Python 的 thread 模块.
源码:
# -*- coding: utf-8 -*- import urllib2 import re import thread import chardet class Book_Spider: def __init__(self): self.pages = [] self.page = 1 self.flag = True self.url = "http://www.quanben.com/xiaoshuo/10/10412/2095096.html" # 将抓取一个章节 def GetPage(self): myUrl = self.url user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } req = urllib2.Request(myUrl, headers = headers) myResponse = urllib2.urlopen(req) myPage = myResponse.read() charset = chardet.detect(myPage) charset = charset['encoding'] if charset == 'utf-8' or charset == 'UTF-8': myPage = myPage else: myPage = myPage.decode('gb2312','ignore').encode('utf-8') unicodePage = myPage.decode("utf-8") # 找出 id="content"的div标记 try: #抓取标题 my_title = re.search('<h1>(.*?)</h1>',unicodePage,re.S) my_title = my_title.group(1) except: print '标题 HTML 变化。请又一次分析!' return False try: #抓取章节内容 my_content = re.search('<div.*?id="htmlContent" class="contentbox">(.*?)<div',unicodePage,re.S) my_content = my_content.group(1) except: print "内容 HTML 变化,请又一次分析。" return False my_content = my_content.replace("<br />","\n") my_content = my_content.replace(" "," ") #用字典存储一章的标题和内容 onePage = {'title':my_title,'content':my_content} try: #找到页面下方的连接区域 foot_link = re.search('<div.*?
class="chapter_Turnpage">(.*?)</div>',unicodePage,re.S) foot_link = foot_link.group(1) #在连接的区域找下一页的连接。依据网页特点为第三个 nextUrl = re.findall(u'<a.*?href="(.*?)".*?>(.*?)</a>',foot_link,re.S) nextUrl = nextUrl[2][0] # 更新下一次进行抓取的链接 self.url = nextUrl except: print "底部链接变化,请又一次分析!" return False return onePage # 用于载入章节 def LoadPage(self): while self.flag: if(len(self.pages) - self.page < 3): try: # 获取新的页面 myPage = self.GetPage() if myPage == False: print '抓取失败!' self.flag = False self.pages.append(myPage) except: print '无法连接网页。' self.flag = False #显示一章 def ShowPage(self,curPage): print curPage['title'] print curPage['content'] print "\n" user_input = raw_input("当前是第 %d 章,回车读取下一章或者输入 quit 退出:" % self.page) if(user_input == 'quit'): self.flag = False print "\n" def Start(self): print u'開始阅读......\n' # 新建一个线程 thread.start_new_thread(self.LoadPage,()) # 假设self的page数组中存有元素 while self.flag: if self.page <= len(self.pages): nowPage = self.pages[self.page-1] self.ShowPage(nowPage) self.page += 1 print u"本次阅读结束" #----------- 程序的入口处 ----------- print u""" --------------------------------------- 程序:阅读呼叫转移 版本号:0.2 作者:angryrookie 日期:2014-07-07 语言:Python 2.7 功能:按下回车浏览下一章节 --------------------------------------- """ print u'请按下回车:' raw_input(' ') myBook = Book_Spider() myBook.Start()
相关文章
- Python 实现 8 个概率分布公式及可视化
- 进一步了解XPath(利用XPath爬取飞哥的博客)【python爬虫入门进阶】(04)
- Python爬虫——写出最简单的网页爬虫
- python爬虫 随机UA库
- 基于Python和JavaScript编写物联网温度计程序
- 第49天:Python 多线程之 threading 模块
- 整理汇总Python框架、库以及软件资源
- 像计算机科学家一样思考python-第1章 程序之道
- python 爬虫系列02-小说
- Python,OpenCV进行直方图反投影
- Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式
- 【Python】利用matplotlib在Pycharm中显示本地图片
- 用python requests库写一个人人网相册爬虫
- 第2.3节 Python运算符大全
- python中sqlite的基本调用和数据库类的封装案例(1)
- Python numpy.cross函数方法的使用
- Python pandas.DataFrame.median函数方法的使用
- 【Python】使用Python压缩文件/文件夹
- 为什么python是编程入门最适合的语言
- Python学习知识点汇总(入门,进阶,web开发,数据爬虫,人工智能).pdf
- 【Python爬虫实战】Python实现动态网页爬虫(requests模块)
- Python爬虫技术系列-06requests完成yz网数据采集V01
- python爬虫requests 下载图片
- python cv2 - 获取视频文件播放时长