内容简介:这是python3实战入门系列的第三篇文章,要学习这一篇需要了解前两篇,要不学起来比较费劲下面来正式开始把我们第一节爬取到的新闻数据保存到mysql数据中通过定义一个MySQLCommand类来配置数据库连接参数,并定义一个connectMysql方法连接数据库
这是 python 3实战入门系列的第三篇文章,要学习这一篇需要了解前两篇,要不学起来比较费劲
- python3实战入门python爬虫篇001---网页爬虫,图片爬虫,文章爬虫,Python爬虫爬取新闻网站新闻
- python3操作数据库002 借助pycharm快速连接并操作 mysql 数据库
下面来正式开始把我们第一节爬取到的新闻数据保存到mysql数据中
一,首先我们需要连接数据库
通过定义一个MySQLCommand类来配置数据库连接参数,并定义一个connectMysql方法连接数据库
# -*- coding: utf-8 -*- # 作者微信:2501902696 import pymysql # 用来操作数据库的类 class MySQLCommand(object): # 类的初始化 def __init__(self): self.host = 'localhost' self.port = 3306 # 端口号 self.user = 'root' # 用户名 self.password = "" # 密码 self.db = "home" # 库 self.table = "home_list" # 表 # 链接数据库 def connectMysql(self): try: self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db, charset='utf8') self.cursor = self.conn.cursor() except: print('connect mysql error.') 复制代码
二,连接完数据库后我们需要插入数据了
插入数据之前我们有两个问题
- 1,重复的数据如何去重
- 2,新数据的主键id应该从哪里开始 针对上面的两个问题我贴出一部分代码来看解决思路
# 插入数据,插入之前先查询是否存在,如果存在就不再插入 def insertData(self, my_dict): table = "home_list" # 要操作的表格 # 注意,这里查询的 sql 语句url=' %s '中%s的前后要有空格 sqlExit = "SELECT url FROM home_list WHERE url = ' %s '" % (my_dict['url']) res = self.cursor.execute(sqlExit) if res: # res为查询到的数据条数如果大于0就代表数据已经存在 print("数据已存在", res) return 0 # 数据不存在才执行下面的插入操作 try: cols = ', '.join(my_dict.keys())#用,分割 values = '"," '.join(my_dict.values()) sql = "INSERT INTO home_list (%s) VALUES (%s)" % (cols, '"' + values + '"') #拼装后的sql如下 # INSERT INTO home_list (img_path, url, id, title) VALUES ("https://img.huxiucdn.com.jpg"," https://www.huxiu.com90.html"," 12"," ") try: result = self.cursor.execute(sql) insert_id = self.conn.insert_id() # 插入成功后返回的id self.conn.commit() # 判断是否执行成功 if result: print("插入成功", insert_id) return insert_id + 1 except pymysql.Error as e: # 发生错误时回滚 self.conn.rollback() # 主键唯一,无法插入 if "key 'PRIMARY'" in e.args[1]: print("数据已存在,未插入数据") else: print("插入数据失败,原因 %d: %s" % (e.args[0], e.args[1])) except pymysql.Error as e: print("数据库错误,原因%d: %s" % (e.args[0], e.args[1])) 复制代码
通过上面代码我们来看如何去重
- 我们在每次插入之前需要查询下数据是否已经存在,如果存在就不在插入,我们的home_list表格的字段有 id,title,url,img_path。通过分析我们抓取到的数据titlehe和img_path字段都可能为空,所以这里我们通过url字段来去重。知道去重原理以后再去读上面的代码,你应该能容易理解了
三,查询数据库中最后一条数据的id值,来确定我们新数据id的开始值
通过下面的getLastId函数来获取home_list表里的最后一条数据的id值
# 查询最后一条数据的id值 def getLastId(self): sql = "SELECT max(id) FROM " + self.table try: self.cursor.execute(sql) row = self.cursor.fetchone() # 获取查询到的第一条数据 if row[0]: return row[0] # 返回最后一条数据的id else: return 0 # 如果表格为空就返回0 except: print(sql + ' execute failed.') 复制代码
下面贴出MySQLCommand数据库操作类的完整代码
# -*- coding: utf-8 -*- # 作者微信:2501902696 import pymysql # 用来操作数据库的类 class MySQLCommand(object): # 类的初始化 def __init__(self): self.host = 'localhost' self.port = 3306 # 端口号 self.user = 'root' # 用户名 self.password = "" # 密码 self.db = "home" # 库 self.table = "home_list" # 表 # 链接数据库 def connectMysql(self): try: self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db, charset='utf8') self.cursor = self.conn.cursor() except: print('connect mysql error.') # 插入数据,插入之前先查询是否存在,如果存在就不再插入 def insertData(self, my_dict): table = "home_list" # 要操作的表格 # 注意,这里查询的sql语句url=' %s '中%s的前后要有空格 sqlExit = "SELECT url FROM home_list WHERE url = ' %s '" % (my_dict['url']) res = self.cursor.execute(sqlExit) if res: # res为查询到的数据条数如果大于0就代表数据已经存在 print("数据已存在", res) return 0 # 数据不存在才执行下面的插入操作 try: cols = ', '.join(my_dict.keys())#用,分割 values = '"," '.join(my_dict.values()) sql = "INSERT INTO home_list (%s) VALUES (%s)" % (cols, '"' + values + '"') #拼装后的sql如下 # INSERT INTO home_list (img_path, url, id, title) VALUES ("https://img.huxiucdn.com.jpg"," https://www.huxiu.com90.html"," 12"," ") try: result = self.cursor.execute(sql) insert_id = self.conn.insert_id() # 插入成功后返回的id self.conn.commit() # 判断是否执行成功 if result: print("插入成功", insert_id) return insert_id + 1 except pymysql.Error as e: # 发生错误时回滚 self.conn.rollback() # 主键唯一,无法插入 if "key 'PRIMARY'" in e.args[1]: print("数据已存在,未插入数据") else: print("插入数据失败,原因 %d: %s" % (e.args[0], e.args[1])) except pymysql.Error as e: print("数据库错误,原因%d: %s" % (e.args[0], e.args[1])) # 查询最后一条数据的id值 def getLastId(self): sql = "SELECT max(id) FROM " + self.table try: self.cursor.execute(sql) row = self.cursor.fetchone() # 获取查询到的第一条数据 if row[0]: return row[0] # 返回最后一条数据的id else: return 0 # 如果表格为空就返回0 except: print(sql + ' execute failed.') def closeMysql(self): self.cursor.close() self.conn.close() # 创建数据库操作类的实例 复制代码
再贴出把爬虫爬取数据插入到数据库的代码
# -*- coding: utf-8 -*- # 作者微信:2501902696 from bs4 import BeautifulSoup from urllib import request import chardet from db.MySQLCommand import MySQLCommand url = "https://www.huxiu.com" response = request.urlopen(url) html = response.read() charset = chardet.detect(html) html = html.decode(str(charset["encoding"])) # 设置抓取到的html的编码方式 # 使用剖析器为html.parser soup = BeautifulSoup(html, 'html.parser') # 获取到每一个class=hot-article-img的a节点 allList = soup.select('.hot-article-img') # 连接数据库 mysqlCommand = MySQLCommand() mysqlCommand.connectMysql() #这里每次查询数据库中最后一条数据的id,新加的数据每成功插入一条id+1 dataCount = int(mysqlCommand.getLastId()) + 1 for news in allList: # 遍历列表,获取有效信息 aaa = news.select('a') # 只选择长度大于0的结果 if len(aaa) > 0: # 文章链接 try: # 如果抛出异常就代表为空 href = url + aaa[0]['href'] except Exception: href = '' # 文章图片url try: imgUrl = aaa[0].select('img')[0]['src'] except Exception: imgUrl = "" # 新闻标题 try: title = aaa[0]['title'] except Exception: title = "" #把爬取到的每条数据组合成一个字典用于数据库数据的插入 news_dict = { "id": str(dataCount), "title": title, "url": href, "img_path": imgUrl } try: # 插入数据,如果已经存在就不在重复插入 res = mysqlCommand.insertData(news_dict) if res: dataCount=res except Exception as e: print("插入数据失败", str(e))#输出插入失败的报错语句 mysqlCommand.closeMysql() # 最后一定要要把数据关闭 dataCount=0 复制代码
如果对上面代码不是很了解可以到我的第一节文章去看下 python3实战入门python爬虫篇---网页爬虫,图片爬虫,文章爬虫,Python爬虫爬取新闻网站新闻
到此我们的python3爬虫+python3数据库篇就完事了,看下操作效果图
gif图片质量不是很好,大家凑合着看吧☺☺☹☎
写于---Python零基础实战入门第四天
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 4 万字全面掌握数据库、数据仓库、数据集市、数据湖、数据中台
- Oracle数据库查询重复数据及删除重复数据方法
- sqlserver数据库获取数据库信息
- 从大数据到数据库
- node连接oracle数据库,更新数据后,数据库中不生效问题
- mybatis从数据库中取数据且分组,返回分组数据
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!