Python抓拍博客园文章,并存入数据库
在学习python后,想做个爬虫,抓取博客园文章。
爬虫思路很简单,模拟浏览器访问网页,得到网页的html代码,再根据页面结构,从html中提取自己需要的内容。
本文代码主要分为3个部分:
1、读取博客园首页文章链接。
https://www.cnblogs.com/是博客园的首页,列出了文章,分析页面内容,读取文章的链接。
这需要看页面的结构,可以使用浏览器,再浏览页面代码,选择元素,看界面上选中哪一部分,根据自己的需要,可以看到对应模块的代码。
2、对于每个页面,分析页面内容。
这需要看页面结构。
3、分析完页面内容后,将需要的数据插入到数据库中。
数据设计:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | -- 博客系统爬虫模块 -- 1、创建库 drop database if exists blog_service_spider; -- 直接删除数据库,不提醒 create database blog_service_spider; -- 创建数据库 use blog_service_spider; -- 选择数据库 -- -- table structure for table `spider_page` -- drop table if exists `spider_page`; create table `spider_page` ( `id` varchar (60) not null comment '主键' , `create_time` datetime default current_timestamp comment '创建时间' , `creator` varchar (60) not null comment '创建人id' , `modified_time` datetime default null on update current_timestamp comment '修改时间' , `modifier` varchar (60) default null comment '修改人id' , `title` varchar (100) default null comment '文章标题' , `title_url` varchar (100) default null comment '文章地址' , `content` text default null comment '文章内容' , `post_time` datetime default null comment '文章发表时间' , `author` varchar (100) default null comment '作者' , `author_page` varchar (100) default null comment '作者主页' , primary key (`id`) ) engine=innodb default charset=utf8 comment= '抓取的文章' ; |
python代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | ''' File Name: webspider Author: tim Date: 2018/7/27 14:36 Description: 网页爬虫。抓取博客园首页文章,放入数据库中。 放入数据库中的内容:标题、作者、发表时间、文章内容、文章地址、作者主页 ''' from urllib import request import ssl from bs4 import BeautifulSoup import pymysql import uuid # 传入url,读取url,将返回的页面转换成BeautifulSoup对象 def html_parser(url): ssl._create_default_https_context = ssl._create_unverified_context # 加入ssl req = request.Request(url) # 构建请求 # 代理,模拟浏览器在访问,避免被屏蔽 req.add_header( 'User-Agent' , 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36' ) resp = request.urlopen(req) # 发起请求 html = resp.read().decode( 'utf-8' ) # 转码 bf = BeautifulSoup(html, "html.parser" ) # 将页面转换成BeautifulSoup对象 return bf # 分析单个网页 def read_page(url): bf = html_parser(url) # 获取BeautifulSoup对象 # 内容分析 post_info = bf.find( 'div' , class_ = 'post' ) # 有用的内容区域,下面的查找从该有用区域中进一步获取 title = post_info.find( id = 'cb_post_title_url' ).get_text() # 文章标题 title_url = post_info.find( id = 'cb_post_title_url' )[ 'href' ] # 文章地址 content = post_info.find( id = 'cnblogs_post_body' ) # 文章内容 postdate = post_info.find( id = 'post-date' ).get_text() # 文章发表时间 author = post_info.find( 'div' , class_ = 'postDesc' ).find( 'a' ).get_text() # 作者 author_page = post_info.find( 'div' , class_ = 'postDesc' ).find( 'a' )[ 'href' ] # 作者主页 '''print(title) print(title_url) print(content) print(postdate) print(author) print(author_page)''' # 分析完每个页面后,将页面内容插入到数据库中 operate_db(title, title_url, content, postdate, author, author_page) # 分析博客园首页文章列表 def read_post_list(): bf = html_parser( 'https://www.cnblogs.com/' ) post_list = bf.find( id = 'post_list' ).find_all( 'div' , class_ = "post_item" ) for post in post_list: page_url = post.find( 'div' , class_ = 'post_item_body' ).h3.a[ 'href' ] # 读取每篇文章的url,分别进行页面分析 read_page(page_url) # 操作数据库 def operate_db(title, title_url, content, postdate, author, author_page): # 打开数据库连接 conn = pymysql.connect( 'localhost' , 'root' , 'root' , 'blog_service_spider' ) # 使用cursor()方法获取操作游标 cursor = conn.cursor() # 执行的sql insert_sql = "insert into spider_page (id,creator,title,title_url,content,post_time,author,author_page) values(%s,%s,%s,%s,%s,%s,%s,%s)" # 生成的ID id = str (uuid.uuid1()) # 文章内容 str_content = str (content) # 创建人 creator = 'admin' try : cursor.execute(insert_sql, ( id , creator, title, title_url, str_content, postdate, author, author_page)) # 执行sql语句 conn.commit() # 提交到数据库执行 except Exception as e: # 如果执行sql语句出现问题,则执行回滚操作 conn.rollback() print (e) finally : # 不论try中的代码是否抛出异常,这里都会执行 # 关闭游标和数据库连接 cursor.close() conn.close() # start if __name__ = = '__main__' : read_post_list() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)