首页 / 爬虫 / 「玩转Python」打造十万博文爬虫篇
「玩转Python」打造十万博文爬虫篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了「玩转Python」打造十万博文爬虫篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3172字,纯文字阅读大概需要5分钟。
内容图文
![「玩转Python」打造十万博文爬虫篇](/upload/InfoBanner/zyjiaocheng/739/b3412051e3014263aaca96a11df4e09c.jpg)
前言
这里以爬取博客园文章为例,仅供学习参考,某些AD满天飞的网站太浪费爬虫的感情了。
爬取
-
使用 BeautifulSoup 获取博文
-
通过 html2text 将 Html 转 Markdown
-
保存 Markdown 到本地文件
-
下载 Markdown 中的图片到本地并替换图片地址
-
写入数据库
工具
使用到的第三方类库:BeautifulSoup、html2text、PooledDB
代码
获取博文:
# 获取标题和文章内容
def getHtml(blog):
res = requests.get(blog, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
# 获取博客标题
title = soup.find('h1', class_='postTitle').text
# 去除空格等
title = title.strip()
# 获取博客内容
content = soup.find('div', class_='blogpost-body')
# 去掉博客外层的DIV
content = article.decode_contents(formatter="html")
info = {"title": title, "content": content}
return info
Html 转 Markdown:
# 这里使用开源第三方库 html2text
md = text_maker.handle(info['content'])
保存到本地文件:
def createFile(md, title):
print('系统默认编码:{}'.format(sys.getdefaultencoding()))
save_file = str(title) +".md"
# print(save_file)
print('准备写入文件:{}'.format(save_file))
# r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
# w+ 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
# a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
f = codecs.open(save_file, 'w+', 'utf-8')
f.write(md)
f.close()
print('写入文件结束:{}'.format(f.name))
return save_file
下载图片到本地并替换图片地址:
def replace_md_url(md_file):
"""
把指定MD文件中引用的图片下载到本地,并替换URL
"""
if os.path.splitext(md_file)[1] != '.md':
print('{}不是Markdown文件,不做处理。'.format(md_file))
return
cnt_replace = 0
# 日期时间为目录存储图片
dir_ts = time.strftime('%Y%m', time.localtime())
isExists = os.path.exists(dir_ts)
# 判断结果
if not isExists:
os.makedirs(dir_ts)
with open(md_file, 'r', encoding='utf-8') as f: # 使用utf-8 编码打开
post = f.read()
matches = re.compile(img_patten).findall(post)
if matches and len(matches) > 0:
for match in list(chain(*matches)):
if match and len(match) > 0:
array = match.split('/')
file_name = array[len(array) - 1]
file_name = dir_ts + "/" + file_name
img = requests.get(match, headers=headers)
f = open(file_name, 'ab')
f.write(img.content)
new_url = "https://blog.52itstyle.vip/{}".format(file_name)
# 更新MD中的URL
post = post.replace(match, new_url)
cnt_replace = cnt_replace + 1
# 如果有内容的话,就直接覆盖写入当前的markdown文件
if post and cnt_replace > 0:
url = "https://blog.52itstyle.vip"
open(md_file, 'w', encoding='utf-8').write(post)
print('{0}的{1}个URL被替换到{2}/{3}'.format(os.path.basename(md_file), cnt_replace, url, dir_ts))
elif cnt_replace == 0:
print('{}中没有需要替换的URL'.format(os.path.basename(md_file)))
写入数据库:
# 写入数据库
def write_db(title, content, url):
sql = "INSERT INTO blog (title, content,url) VALUES(%(title)s, %(content)s, %(url)s);"
param = {"title": title, "content": content, "url": url}
mysql.insert(sql, param)
小结
互联网时代一些开放的博客社区的确方便了很多,但是也伴随着随时消失的可能性,最好就是自己备份一份到本地;你也可以选择自己喜欢的博主,爬取下收藏。
源码:https://gitee.com/52itstyle/Python
列表:https://blog.52itstyle.top/index
详情:https://blog.52itstyle.top/49.shtml
内容总结
以上是互联网集市为您收集整理的「玩转Python」打造十万博文爬虫篇全部内容,希望文章能够帮你解决「玩转Python」打造十万博文爬虫篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。