小说爬虫
来源:互联网 发布:海马玩模拟器 for mac 编辑:程序博客网 时间:2024/04/27 00:51
通过读取源码,正则表达式扣出想要的内容还不完善,在扣文章内容时部分内容有链接需要修改正则表达式
# -*- coding:utf-8 -*-####################### 程序:百度贴吧爬虫# 版本:0.9# 作者:昊海东青# 日期:2014-09-28# 语言:python2.7# 功能:查看小说最近的更新章节,并可以查看内容######################import reimport urllib2import urllibimport threadimport timeimport chardetimport sys#--------------- '加载处理贴吧'-------------reload(sys)sys.setdefaultencoding('utf-8')#str转utf-8class spider_model: def __init__(self): self.page=1 self.pages=[] #待定 def get_req(self,myurl): user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers ={'User-Agent':user_agent} req=urllib2.Request(myurl,headers=headers) return req #模拟浏览器并且获得页面 def get_depage(self,req): myresquest=urllib2.urlopen(req) mypage=myresquest.read() mychar=chardet.detect(mypage) bianma=mychar['encoding'] if bianma!='utf-8' and bianma!='UTF-8': mypage=mypage.decode('gb2312','ignore').encode('utf-8') return mypage #获得编码后的页面 def getpage(self,myurl): req=self.get_req(myurl) mypage=self.get_depage(req) res='<a href="(.*?)".*?title="(.*?[^"])".*?>.*?</a>' items=[] myitem=re.findall(res,mypage,re.S) for i in myitem: if i[1].decode()[0]==u'完' or i[1].decode()[0]==u'第' : items.append([i[0],i[1].replace("\n","")]) return items#显示 def showpage(self,items,n): for i in range(n): print(str(i)+'. '+items[i][1]) def shownr(self,url,items,n): if n>=0: url=url+items[n][0] print(url) self.getnr(url) else : return def getnr(self,url): user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers={'User-Agent':user_agent} req=urllib2.Request(url,headers=headers) myresquest=urllib2.urlopen(req) mypage=myresquest.read() mychar=chardet.detect(mypage) bianma=mychar['encoding'] if bianma!='utf-8' and bianma!='UTF-8': mypage=mypage.decode('gb2312','ignore').encode('utf-8') #匹配 res='<br>(.[^abcdefghijklmnopqrstuvwxyz]*?)<br>' items=[] myitem=re.findall(res,mypage,re.S) for i in myitem:items.append(i.replace("\n","")) for i in items: print(i) return items############wz=["http://tieba.baidu.com/f/good?kw=%CD%EA%C3%C0%CA%C0%BD%E7%D0%A1%CB%B5&tab=good&cid=4", "http://tieba.baidu.com/f/good?kw=%B1%A6%BC%F8&cid=1", "http://tieba.baidu.com/f/good?kw=%C3%A7%BB%C4%BC%CD&tab=good&cid=2", "http://tieba.baidu.com/f/good?kw=%C5%D1%C4%E6%B5%C4%D5%F7%CD%BE&cid=2"]############w=spider_model()while 1: gg=input("输入想看的书的编号\n0.完美世界\n1.宝鉴\n2.莽荒纪\n3.叛逆的征途\n") if(gg>=0): wan=w.getpage(wz[gg]) w.showpage(wan,10) while 1: pp=input("输入想看的章节编号或者-1退出") if(pp<0): break else: w.shownr('http://tieba.baidu.com',wan,pp) w.showpage(wan,10) else: break
0 0
- 小说爬虫
- python 爬虫抓小说
- C# 爬虫 抓取小说
- C# 爬虫 抓取小说
- 爬虫下载小说实现
- 爬虫爬取完本小说
- Python小说爬虫
- 简单的小说网站爬虫--爬小说
- 爬虫起点小说网所有小说基本信息
- Python 爬虫(获取小说)
- Python爬虫小说采集程序
- 爬虫实践---抓取小说网站
- 爬虫 scrapy 抓取小说实例
- C#网络爬虫抓取小说
- 简单爬虫入门-小说下载
- Python爬虫实战二 | 抓取小说网完结小说
- 写爬虫,只是为了下小说
- java爬虫之下载txt小说
- poj 3111 K Best
- poj 3109 Inner Vertices 树状数组+离散化
- C 语言的可变参数表函数的设计
- poj 3061 尺取法
- abstract类举例
- 小说爬虫
- poj 3045 Cow Acrobats
- FMDB
- Map三种遍历方式
- poj 2976 Dropping tests
- 【c++程序】求最大公约数
- poj 2886 Who Gets the Most Candies?
- 常用字符串长度计算函数
- Hdu 3341 Lost's revenge (ac自动机+dp+hash)