python Selenium爬取内容并存储至MySQL数据库的实现代码


Posted in Python onMarch 16, 2017

前面我通过一篇文章讲述了如何爬取CSDN的博客摘要等信息。通常,在使用Selenium爬虫爬取数据后,需要存储在TXT文本中,但是这是很难进行数据处理和数据分析的。这篇文章主要讲述通过Selenium爬取我的个人博客信息,然后存储在数据库MySQL中,以便对数据进行分析,比如分析哪个时间段发表的博客多、结合WordCloud分析文章的主题、文章阅读量排名等。
这是一篇基础性的文章,希望对您有所帮助,如果文章中出现错误或不足之处,还请海涵。下一篇文章会简单讲解数据分析的过程。

一. 爬取的结果
爬取的地址为:http://blog.csdn.net/Eastmount

python Selenium爬取内容并存储至MySQL数据库的实现代码

        

        爬取并存储至MySQL数据库的结果如下所示:

python Selenium爬取内容并存储至MySQL数据库的实现代码

        运行过程如下图所示:

python Selenium爬取内容并存储至MySQL数据库的实现代码

二. 完整代码分析

完整代码如下所示:

# coding=utf-8 
 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import selenium.webdriver.support.ui as ui   
import re
import time
import os
import codecs
import MySQLdb
 
#打开Firefox浏览器 设定等待加载时间 
driver = webdriver.Firefox()
wait = ui.WebDriverWait(driver,10) 

#获取每个博主的博客页面低端总页码  
def getPage():
 print 'getPage'
 number = 0  
 texts = driver.find_element_by_xpath("//div[@id='papelist']").text  
 print '页码', texts  
 m = re.findall(r'(\w*[0-9]+)\w*',texts) #正则表达式寻找数字  
 print '页数:' + str(m[1])  
 return int(m[1]) 
 
#主函数 
def main():
 #获取txt文件总行数
 count = len(open("Blog_URL.txt",'rU').readlines())
 print count
 n = 0
 urlfile = open("Blog_URL.txt",'r')

 #循环获取每个博主的文章摘信息 
 while n < count: #这里爬取2个人博客信息,正常情况count个博主信息
  url = urlfile.readline()
  url = url.strip("\n")
  print url
  driver.get(url)
  #获取总页码
  allPage = getPage()
  print u'页码总数为:', allPage
  time.sleep(2)

  #数据库操作结合
  try:
   conn=MySQLdb.connect(host='localhost',user='root',
         passwd='123456',port=3306, db='test01')
   cur=conn.cursor() #数据库游标

   #报错:UnicodeEncodeError: 'latin-1' codec can't encode character
   conn.set_character_set('utf8')
   cur.execute('SET NAMES utf8;')
   cur.execute('SET CHARACTER SET utf8;')
   cur.execute('SET character_set_connection=utf8;')
   
   #具体内容处理
   m = 1 #第1页
   while m <= allPage:
    ur = url + "/article/list/" + str(m)
    print ur
    driver.get(ur)
    
    #标题
    article_title = driver.find_elements_by_xpath("//div[@class='article_title']")
    for title in article_title:
     #print url
     con = title.text
     con = con.strip("\n")
     #print con + '\n'
    
    #摘要
    article_description = driver.find_elements_by_xpath("//div[@class='article_description']")
    for description in article_description:
     con = description.text
     con = con.strip("\n")
     #print con + '\n'

    #信息
    article_manage = driver.find_elements_by_xpath("//div[@class='article_manage']")
    for manage in article_manage:
     con = manage.text
     con = con.strip("\n")
     #print con + '\n'

    num = 0
    print u'长度', len(article_title)
    while num < len(article_title):
     #插入数据 8个值
     sql = '''insert into csdn_blog
        (URL,Author,Artitle,Description,Manage,FBTime,YDNum,PLNum)
       values(%s, %s, %s, %s, %s, %s, %s, %s)'''
     Artitle = article_title[num].text
     Description = article_description[num].text
     Manage = article_manage[num].text
     print Artitle
     print Description
     print Manage
     #获取作者
     Author = url.split('/')[-1]
     #获取阅读数和评论数
     mode = re.compile(r'\d+\.?\d*')
     YDNum = mode.findall(Manage)[-2]
     PLNum = mode.findall(Manage)[-1]
     print YDNum
     print PLNum
     #获取发布时间
     end = Manage.find(u' 阅读')
     FBTime = Manage[:end]
     cur.execute(sql, (url, Author, Artitle, Description, Manage,FBTime,YDNum,PLNum)) 
     
     num = num + 1
    else:
     print u'数据库插入成功'    
    m = m + 1
     
  
  #异常处理
  except MySQLdb.Error,e:
   print "Mysql Error %d: %s" % (e.args[0], e.args[1])
  finally:
   cur.close() 
   conn.commit() 
   conn.close()
  
  n = n + 1
    
 else:
  urlfile.close()
  print 'Load Over'
   
main()

在Blog_Url.txt文件中放置需要爬取用户的博客地址URL,如下图所示。注意在此处,作者预先写了个爬取CSDN所有专家的URL代码,这里为访问其他人用于提升阅读量已省略。

python Selenium爬取内容并存储至MySQL数据库的实现代码

分析过程如下所示。
1.获取博主总页码
首先从Blog_Url.txt读取博主地址,然后访问并获取页码总数。代码如下:

#获取每个博主的博客页面低端总页码  
def getPage():
 print 'getPage'
 number = 0  
 texts = driver.find_element_by_xpath("//div[@id='papelist']").text  
 print '页码', texts  
 m = re.findall(r'(\w*[0-9]+)\w*',texts) #正则表达式寻找数字  
 print '页数:' + str(m[1])  
 return int(m[1])

比如获取总页码位17页,如下图所示:

python Selenium爬取内容并存储至MySQL数据库的实现代码

2.翻页DOM树分析
这里的博客翻页采用的是URL连接,比较方便。
如:http://blog.csdn.net/Eastmount/article/list/2
故只需要 :1.获取总页码;2.爬取每页信息;3.URL设置进行循环翻页;4.再爬取。
也可以采用点击"下页"跳转,没有"下页"停止跳转,爬虫结束,接着爬取下一个博主。

python Selenium爬取内容并存储至MySQL数据库的实现代码

3.获取详细信息:标题、摘要、时间
然后审查元素分析每个博客页面,如果采用BeautifulSoup爬取会报错"Forbidden"。
发现每篇文章都是由一个<div></div>组成,如下所示,只需要定位到该位置即可。

python Selenium爬取内容并存储至MySQL数据库的实现代码

        这里定位到该位置即可爬取,这里需要分别定位标题、摘要、时间。

python Selenium爬取内容并存储至MySQL数据库的实现代码

代码如下所示。注意,在while中同时获取三个值,它们是对应的。

#标题
article_title = driver.find_elements_by_xpath("//div[@class='article_title']")
for title in article_title:
 con = title.text
 con = con.strip("\n")
 print con + '\n'
    
#摘要
article_description = driver.find_elements_by_xpath("//div[@class='article_description']")
for description in article_description:
 con = description.text
 con = con.strip("\n")
 print con + '\n'

#信息
article_manage = driver.find_elements_by_xpath("//div[@class='article_manage']")
for manage in article_manage:
 con = manage.text
 con = con.strip("\n")
 print con + '\n'

num = 0
print u'长度', len(article_title)
while num < len(article_title):
 Artitle = article_title[num].text
 Description = article_description[num].text
 Manage = article_manage[num].text
 print Artitle, Description, Manage

 4.特殊字符串处理
获取URL最后一个/后的博主名称、获取字符串时间、阅读数代码如下:

#获取博主姓名
url = "http://blog.csdn.net/Eastmount"
print url.split('/')[-1]
#输出: Eastmount

#获取数字
name = "2015-09-08 18:06 阅读(909) 评论(0)"
print name
import re
mode = re.compile(r'\d+\.?\d*') 
print mode.findall(name)
#输出: ['2015', '09', '08', '18', '06', '909', '0']
print mode.findall(name)[-2]
#输出: 909


#获取时间
end = name.find(r' 阅读')
print name[:end]
#输出: 2015-09-08 18:06

import time, datetime
a = time.strptime(name[:end],'%Y-%m-%d %H:%M')
print a
#输出: time.struct_time(tm_year=2015, tm_mon=9, tm_mday=8, tm_hour=18, tm_min=6,
#  tm_sec=0, tm_wday=1, tm_yday=251, tm_isdst=-1)

三. 数据库相关操作
SQL语句创建表代码如下:

CREATE TABLE `csdn` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `URL` varchar(100) COLLATE utf8_bin DEFAULT NULL,
 `Author` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '作者',
 `Artitle` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '标题',
 `Description` varchar(400) COLLATE utf8_bin DEFAULT NULL COMMENT '摘要',
 `Manage` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '信息',
 `FBTime` datetime DEFAULT NULL COMMENT '发布日期',
 `YDNum` int(11) DEFAULT NULL COMMENT '阅读数',
 `PLNum` int(11) DEFAULT NULL COMMENT '评论数',
 `DZNum` int(11) DEFAULT NULL COMMENT '点赞数',
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9371 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

显示如下图所示:

python Selenium爬取内容并存储至MySQL数据库的实现代码

其中,Python调用MySQL推荐下面这篇文字。
python专题九.Mysql数据库编程基础知识
核心代码如下所示:

# coding:utf-8 
import MySQLdb
 
try:
 conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306, db='test01')
 cur=conn.cursor()
 
 #插入数据
 sql = '''insert into student values(%s, %s, %s)'''
 cur.execute(sql, ('yxz','111111', '10'))

 #查看数据
 print u'\n插入数据:'
 cur.execute('select * from student')
 for data in cur.fetchall():
  print '%s %s %s' % data
 cur.close()
 conn.commit()
 conn.close()
except MySQLdb.Error,e:
  print "Mysql Error %d: %s" % (e.args[0], e.args[1])

注意,在下载过程中,有的网站是新版本的,无法获取页码。
比如:http://blog.csdn.net/michaelzhou224
这时需要简单设置,跳过这些链接,并保存到文件中,核心代码如下所示:

#获取每个博主的博客页面低端总页码  
def getPage():
 print 'getPage'
 number = 0  
 #texts = driver.find_element_by_xpath("//div[@id='papelist']").text
 texts = driver.find_element_by_xpath("//div[@class='pagelist']").text
 print 'testsss'
 print u'页码', texts
 if texts=="":
  print u'页码为0 网站错误'
  return 0
 m = re.findall(r'(\w*[0-9]+)\w*',texts) #正则表达式寻找数字  
 print u'页数:' + str(m[1])  
 return int(m[1])

主函数修改:

error = codecs.open("Blog_Error.txt", 'a', 'utf-8')

 #循环获取每个博主的文章摘信息 
 while n < count: #这里爬取2个人博客信息,正常情况count个博主信息
  url = urlfile.readline()
  url = url.strip("\n")
  print url
  driver.get(url+"/article/list/1")
  #print driver.page_source
  #获取总页码
  allPage = getPage()
  print u'页码总数为:', allPage
  #返回错误,否则程序总截住
  if allPage==0:
   error.write(url + "\r\n")
   print u'错误URL'
   continue; #跳过进入下一个博主
  time.sleep(2)
  #数据库操作结合
  try:
    .....

最后希望文章对你有所帮助,如果文章中存在错误或不足之处,还请海涵~
提高效率,提升科研,认真教学,娜美人生。

Python 相关文章推荐
python实现通过shelve修改对象实例
Sep 26 Python
使用Python脚本将绝对url替换为相对url的教程
Apr 24 Python
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
May 27 Python
Python列表删除的三种方法代码分享
Oct 31 Python
用python编写第一个IDA插件的实例
May 29 Python
pycharm运行出现ImportError:No module named的解决方法
Oct 13 Python
基于Python实现定时自动给微信好友发送天气预报
Oct 25 Python
python实现一行输入多个值和一行输出多个值的例子
Jul 16 Python
如何给Python代码进行加密
Jan 10 Python
解决python运行效率不高的问题
Jul 20 Python
Python调用飞书发送消息的示例
Nov 10 Python
python快速安装OpenCV的步骤记录
Feb 22 Python
python开发利器之ulipad的使用实践
Mar 16 #Python
离线安装Pyecharts的步骤以及依赖包流程
Apr 23 #Python
Python中%r和%s的详解及区别
Mar 16 #Python
Python 装饰器深入理解
Mar 16 #Python
WINDOWS 同时安装 python2 python3 后 pip 错误的解决方法
Mar 16 #Python
Django卸载之后重新安装的方法
Mar 15 #Python
Python json 错误xx is not JSON serializable解决办法
Mar 15 #Python
You might like
PHP 一个页面执行时间类代码
2010/03/05 PHP
php 5.6版本中编写一个PHP扩展的简单示例
2015/01/20 PHP
详解WordPress中创建和添加过滤器的相关PHP函数
2015/12/29 PHP
curl 出现错误的调试方法(必看)
2017/02/13 PHP
心扬JS分页函数代码
2010/09/10 Javascript
原始的js代码和jquery对比体会
2013/09/10 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
JavaScript中的关联数组问题
2015/03/04 Javascript
JS实现的不规则TAB选项卡效果代码
2015/09/18 Javascript
JS组件中bootstrap multiselect两大组件较量
2016/01/26 Javascript
简单掌握JavaScript中const声明常量与变量的用法
2016/05/21 Javascript
jQuery实现的多张图无缝滚动效果【测试可用】
2016/09/12 Javascript
关于不同页面之间实现参数传递的几种方式讨论
2017/02/13 Javascript
Bootstrap Multiselect 常用组件实现代码
2017/07/09 Javascript
vscode中Vue别名路径提示的实现
2020/07/31 Javascript
浅析JavaScript中的事件委托机制跟深浅拷贝
2021/01/20 Javascript
解决Python网页爬虫之中文乱码问题
2018/05/11 Python
matplotlib subplots 设置总图的标题方法
2018/05/25 Python
对numpy中向量式三目运算符详解
2018/10/31 Python
python中数组和矩阵乘法及使用总结(推荐)
2019/05/18 Python
python可视化爬虫界面之天气查询
2019/07/03 Python
python实现几种归一化方法(Normalization Method)
2019/07/31 Python
python2爬取百度贴吧指定关键字和图片代码实例
2019/08/14 Python
Python pip 安装与使用(安装、更新、删除)
2019/10/06 Python
Python中用pyinstaller打包时的图标问题及解决方法
2020/02/17 Python
Python+Appium实现自动化测试的使用步骤
2020/03/24 Python
Django与pyecharts结合的实例代码
2020/05/13 Python
python 日志模块logging的使用场景及示例
2021/01/04 Python
台湾租车首选品牌:IWS艾维士租车
2019/05/03 全球购物
2014年大学生自我评价
2014/01/19 职场文书
2014年底个人工作总结
2015/03/10 职场文书
入党介绍人意见怎么写
2015/06/03 职场文书
搞笑婚庆主持词
2015/06/29 职场文书
《小蝌蚪找妈妈》教学反思
2016/02/23 职场文书
个人销售励志奋斗口号
2019/12/05 职场文书
nodejs利用readline提示输入内容实例代码
2021/07/15 NodeJs