Python爬虫:爬取小说并存储到数据库
2017-05-31 20:07
549 查看
爬取小说网站的小说,并保存到数据库
第一步:先获取小说内容
执行后结果如下:
第二步:存储到数据库
1、设计数据库
1.1 新建库:novel
1.2 设计表:novel
1.3 设计表:chapter
并设置外键
2、编写脚本
3、执行脚本
4、查看数据库
可以看到已经存储成功了。
报错:
_mysql_exceptions.OperationalError: (1364, "Field 'novelid' doesn't have a default value")
解决:执行sql语句
SELECT @@GLOBAL.sql_mode;
SET @@GLOBAL.sql_mode="NO_ENGINE_SUBSTITUTION";
报错参考:http://blog.sina.com.cn/s/blog_6d2b3e4901011j9w.html
第一步:先获取小说内容
#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib2,re domain = 'http://www.quanshu.net' headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" } def getTypeList(pn=1): #获取分类列表的函数 req = urllib2.Request('http://www.quanshu.net/map/%s.html' % pn) #实例将要请求的对象 req.headers = headers #替换所有头信息 #req.add_header() #添加单个头信息 res = urllib2.urlopen(req) #开始请求 html = res.read().decode('gbk') #decode解码,解码成Unicode reg = r'<a href="(/book/.*?)" target="_blank">(.*?)</a>' reg = re.compile(reg) #增加匹配效率 正则匹配返回的类型为List return re.findall(reg,html) def getNovelList(url): #获取章节列表函数 req = urllib2.Request(domain + url) req.headers = headers res = urllib2.urlopen(req) html = res.read().decode('gbk') reg = r'<li><a href="(.*?)" title=".*?">(.*?)</a></li>' reg = re.compile(reg) return re.findall(reg,html) def getNovelContent(url): #获取章节内容 req = urllib2.Request(domain + url) req.headers = headers res = urllib2.urlopen(req) html = res.read().decode('gbk') reg = r'style5\(\);</script>(.*?)<script type="text/javascript">style6\(\)' return re.findall(reg,html)[0] if __name__ == '__main__': for type in range(1,10): for url,title in getTypeList(type): for zurl,ztitle in getNovelList(url): print u'正则爬取----%s' %ztitle content = getNovelContent(url.replace('index.html',zurl)) print content break break
执行后结果如下:
第二步:存储到数据库
1、设计数据库
1.1 新建库:novel
1.2 设计表:novel
1.3 设计表:chapter
并设置外键
2、编写脚本
#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib2,re import MySQLdb class Sql(object): conn = MySQLdb.connect(host='192.168.19.213',port=3306,user='root',passwd='Admin123',db='novel',charset='utf8') def addnovels(self,sort,novelname): cur = self.conn.cursor() cur.execute("insert into novel(sort,novelname) values(%s , '%s')" %(sort,novelname)) lastrowid = cur.lastrowid cur.close() self.conn.commit() return lastrowid def addchapters(self,novelid,chaptername,content): cur = self.conn.cursor() cur.execute("insert into chapter(novelid,chaptername,content) values(%s , '%s' ,'%s')" %(novelid,chaptername,content)) cur.close() self.conn.commit() domain = 'http://www.quanshu.net' headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" } def getTypeList(pn=1): #获取分类列表的函数 req = urllib2.Request('http://www.quanshu.net/map/%s.html' % pn) #实例将要请求的对象 req.headers = headers #替换所有头信息 #req.add_header() #添加单个头信息 res = urllib2.urlopen(req) #开始请求 html = res.read().decode('gbk') #decode解码,解码成Unicode reg = r'<a href="(/book/.*?)" target="_blank">(.*?)</a>' reg = re.compile(reg) #增加匹配效率 正则匹配返回的类型为List return re.findall(reg,html) def getNovelList(url): #获取章节列表函数 req = urllib2.Request(domain + url) req.headers = headers res = urllib2.urlopen(req) html = res.read().decode('gbk') reg = r'<li><a href="(.*?)" title=".*?">(.*?)</a></li>' reg = re.compile(reg) return re.findall(reg,html) def getNovelContent(url): #获取章节内容 req = urllib2.Request(domain + url) req.headers = headers res = urllib2.urlopen(req) html = res.read().decode('gbk') reg = r'style5\(\);</script>(.*?)<script type="text/javascript">style6\(\)' return re.findall(reg,html)[0] mysql = Sql() if __name__ == '__main__': for sort in range(1,10): for url,title in getTypeList(sort): lastrowid = mysql.addnovels(sort, title) for zurl,ztitle in getNovelList(url): print u'正则爬取----%s' %ztitle content = getNovelContent(url.replace('index.html',zurl)) print u'正在存储----%s' %ztitle mysql.addchapters(lastrowid,ztitle,content)
3、执行脚本
4、查看数据库
可以看到已经存储成功了。
报错:
_mysql_exceptions.OperationalError: (1364, "Field 'novelid' doesn't have a default value")
解决:执行sql语句
SELECT @@GLOBAL.sql_mode;
SET @@GLOBAL.sql_mode="NO_ENGINE_SUBSTITUTION";
报错参考:http://blog.sina.com.cn/s/blog_6d2b3e4901011j9w.html
相关文章推荐
- Python整站式爬虫并存储数据库练习
- python爬虫01-创建一个简单的爬虫(附赠爬取的100G某小说网数据库)
- python爬虫爬取ithome的新闻存储到本地数据库
- 关于Python爬虫无数据库的存储1之存储为csv格式
- [python]微信开发access_token数据库存储 和 socket存取
- 04 Python正则表达式 爬虫程序 变量的引用,浅拷贝,深拷贝 多线程 进程锁 数据库模块
- python 网站爬虫 下载在线盗墓笔记小说到本地的脚本
- 零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
- python 爬虫抓小说
- python爬虫--连接数据库
- 基于Python的DBLP数据库爬虫系统
- mysql 图片存储到数据库中 python
- python3.5爬虫完成笔趣阁小说的爬取
- Python爬虫之MySql5.7.11数据库安装教程及其路径选择
- Python 练习册 3-存储激活码到Redis数据库
- Python爬虫实战六之抓取爱问知识人问题并保存至数据库
- 《网络爬虫-Python和数据分析》数据库建库建表问题
- python实现简易数据库之一——存储和索引建立
- python 网站爬虫 下载在线盗墓笔记小说到本地的脚本
- Python高级爬虫(三):数据存储以及多线程