批量下载小说网站上的小说(python爬虫)
2017-01-01 10:39
836 查看
随便说点什么
因为在学python,所有自然而然的就掉进了爬虫这个坑里,好吧,主要是因为我觉得爬虫比较酷,才入坑的。想想看,你可以批量自动的采集互联网上海量的资料数据,是多么令人激动啊!
所以我就被这块大蛋糕吸引过来了 :)
想学爬虫自然要去找学习资料了,不过网上有很多,我找了不少,个人觉得崔庆才的爬虫教程写得不错。起码对我来说,入门是够了。
感兴趣的朋友可以点进链接看看:Python爬虫学习系列教程 <==这位兄台博客做得也很好
掌握了基本的爬虫知识,主要是urllib,urlib2,re 这些库,以及Request(),urlopen()的基本用法之后,我就开始寻找爬取目标。
正好女朋友的哥哥让她帮忙下载小说,找我推荐几本,我一时也不知道他哥喜欢什么类型的,心想干脆找个小说网批量下载一些小说。
于是说干就干,用了一个中午时间写了一个粗糙的爬虫脚本,启动发现可以运行,让脚本在这儿跑着,我回去躺在床上呼呼大睡。
起来之后发现脚本遇到错误,停掉了,于是debug,干掉bug之后跑起来陆续又发现几个错误,于是干脆在一些容易出错的地方,例如urlopen()请求服务器的地方,本地write()写入的地方(是的,这也会有超时错误!)加入了try-except捕获错误进行处理,另外加入了socket.timeout网络超时限制,修修补补之后总算可以顺畅的运行。
如此,运行了两天,爬虫脚本把这个小说网上的几乎所有小说都下载到了本地,一共27000+本小说,一共40G。
自此,大功告成,打包发了过去。另外说一句,百度云真坑,每次上传数量有限制,分享给朋友文件数量有限制,分享的文件夹大小也有限制,害的我还得整成压缩版才能分享。
下载界面
下面附上代码
#coding=utf-8 import urllib import urllib2 import re import os webroot = 'http://www.xuanshu.com' for page in range(20,220): print '正在下载第'+str(page)+'页小说' url = 'http://www.xuanshu.com/soft/sort02/index_'+str(page)+'.html' headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' } try: request = urllib2.Request(url,headers=headers) response = urllib2.urlopen(request,timeout=180) #print response.read() except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reason html = response.read().decode('utf-8') #print html pattern = re.compile(u'<li>.*?<div class="s">.*?target="_blank">(.*?)</a><br />大小:(.*?)<br>.*?</em><br>更新:(.*?)</div>.*?<a href="(.*?)"><img.*?>(.*?)</a>.*?<div class="u">(.*?)</div>',re.S) items = re.findall(pattern,html) #print items for item in items: try: book_auther = item[0].encode('gbk') book_size = item[1].encode('gbk') book_updatetime = item[2].encode('gbk') book_link = item[3].encode('gbk') book_name = item[4].encode('gbk') book_note = item[5].encode('gbk') book_full_link = webroot + book_link # 构建书的绝对地址 #请求地址 try: request = urllib2.Request(book_full_link,headers=headers) response = urllib2.urlopen(request,timeout=180) except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reason html = response.read().decode('utf-8') #print html pattern = re.compile('<a class="downButton.*?<a class="downButton" href=\'(.*?)\'.*?Txt.*?</a>',re.S) down_link = re.findall(pattern,html) print book_name print down_link # down txt try: request = urllib2.Request(down_link[0].encode('utf-8'),headers=headers) response = urllib2.urlopen(request,timeout=180) except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reason try: fp = open(book_name+'.txt','w') except IOError,e: pattern = re.compile('<strong>.*?>(.*?)<.*?</strong>',re.S) book_name = re.findall(pattern,book_name) fp = open(book_name[0]+'.txt','w') print 'start download' fp.write(response.read()) print 'down finish\n' fp.close() except Exception,e: print '该条目解析出现错误,忽略' print e print '' fp = open('error.log','a') fp.write('page:'+str(page)+'\n') fp.write(item[4].encode('gbk')) #fp.write(e) fp.write('\nThere is an error in parsing process.\n\n') fp.close()
注意
该脚本只定向抓取“选书网”小说站,“玄幻奇幻”分类下的小说。供网友们参考,可自行修改。写得比较粗糙,勿喷……
Freecode# : www.cnblogs.com/yym2013
相关文章推荐
- python 网站爬虫 下载在线盗墓笔记小说到本地的脚本
- python 网站爬虫 下载在线盗墓笔记小说到本地的脚本
- 从零开始写Python爬虫 --- 1.7 爬虫实践: 排行榜小说批量下载
- python 爬虫 网络小说下载(静态网站)
- Python 爬虫批量下载美剧 from 人人影视 HR-HDTV
- 用Python 爬虫批量下载PDF文档
- Python爬虫小实践:下载妹子图www.mzitu.com网站上所有的妹子图片,并按相册名字建立文件夹分好文件名
- 爬虫实践---排行榜小说批量下载
- Python爬虫下载QQ音乐网站歌曲
- python批量下载色影无忌和蜂鸟的图片 爬虫小应用
- python爬虫:批量下载qq空间里的照片(一)
- Python练手爬虫系列No.1 知乎福利收藏夹图片批量下载
- python批量下载兰科植物网站的图片,并重命名文件
- 使用python进行爬虫下载指定网站的图片
- [Python]_[批量下载网站文件]
- python 爬虫下载网站图片
- python 站点爬虫 下载在线盗墓笔记小说到本地的脚本
- 网络爬虫 -- 批量下载网站图片
- python一个下载小说的简易爬虫
- python实现网站内部视频批量下载