前言

写博客的人一定都会有一个图床,将图片存在那里。发现自己以前没有注意图片来源问题,随手就贴在博客上面了。现在有不少图片都挂了,换句话来说有可能自己目前用的图床不提供服务了,那所有的图片都有可能丢失......所以打算写个脚本,把以前博客的图片保存下来,并在以后发博的时候注意保存本地图片。emmm,程序如下,主要是个人用脚本,所以很多地方就是图自己的方便、根据自己的情况写的:

代码

# -*- coding: utf-8 -*-
import os
import requests
from bs4 import BeautifulSoup
import re
import time
from subprocess import call path = "./back_up/"
url = "https://www.cnblogs.com/yunlambert/p/"
model = 'http://equations.online/' def download(DownUrl, DownPath, OutPutFileName):
IDM = r'D:\yun_install_software\IDM\IDMan.exe'
DownPath = r'E:\workstation\Github\Blog_Pictures\back_up'
call([IDM, '/d', DownUrl, '/p', DownPath, '/f', OutPutFileName, '/n']) def get_url(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/51.0.2704.63 Safari/537.36'}
response = requests.get(url, headers=headers, timeout=30)
if response.status_code == 200:
return response.text
else:
print(response.status_code)
return None
except:
print('Error open the page... ')
return None def get_pages(post_page):
pages = []
while True:
try:
post_pages = post_page.find_all(name="a")[-2].get('href')
temp = get_url("https://www.cnblogs.com" + post_pages)
post_page = BeautifulSoup(temp, "lxml")
pages.append("https://www.cnblogs.com" + post_pages)
# print(post_pages)
except Exception as e:
print(Exception, ":", e)
break
return pages def main():
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
html = get_url(url)
soup = BeautifulSoup(html, "lxml") post_page_1 = soup.find(name='div', attrs={"class": "Pager"})
post_page = get_pages(post_page_1)
post_page.insert(0, "https://www.cnblogs.com/yunlambert/p/?page=1")
print(post_page)
post_article = [] for i in range(0, len(post_page)):
link = post_page[i]
page = BeautifulSoup(get_url(link), "lxml")
try:
article = page.find_all(name="div", attrs={"class": "postTitl2"})
for j in range(0, len(article)):
post_article.append(article[j].a.get("href")) except Exception as e:
print(Exception, ":", e)
continue
print(post_article) img_url_list = []
for i in range(0, len(post_article)):
print("new article....")
m = get_url(post_article[i])
soup_article = BeautifulSoup(m, "lxml") replace_pattern = r'<[img|IMG].*?/>' # img标签的正则式
img_url_pattern = r'.+?src="(\S+)"' # img_url的正则式 # 只在段落中查找图片
need_replace_list = re.findall(replace_pattern, str(soup_article.find_all('p'))) # 找到所有的img标签
for tag in need_replace_list:
if re.findall(img_url_pattern, tag) != []:
download_path = "E:\\workstation\\Github\\Blog_Pictures\\back_up\\"
# download_name = re.findall(img_url_pattern, tag)[0].split('/')[-6]
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
download_name = now + ".png"
download(re.findall(img_url_pattern, tag)[0], download_path, download_name)
print(re.findall(img_url_pattern, tag)[0])
img_url_list.append(re.findall(img_url_pattern, tag)[0]) # 找到所有的img_url if __name__ == "__main__":
main()

用IDM下载博客图片的更多相关文章

  1. Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片

    Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片 其实没太大用,就是方便一些,因为现在各个平台之间的图片都不能共享,比如说在 CSDN 不能用简书的图片, ...

  2. hexo博客图片问题

    hexo博客图片问题 第一步 首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folde ...

  3. Ruby:多线程队列(Queue)下载博客文章到本地

    Ruby:多线程下载博客文章到本地的完整代码 #encoding:utf-8 require 'net/http' require 'thread' require 'open-uri' requir ...

  4. 利用Python进行博客图片压缩

    自己写博客的时候常常要插入一些手机拍的照片,都是几M的大小,每张手动压缩太费事了,于是根据自己博客的排版特点用Python写了一个简单的图片压缩脚本,功能是将博客图片生成缩略图,横屏的图片压缩为宽度最 ...

  5. 博客图片失效?使用npm工具一次下载/替换所有失效的外链图片

    前言 大约一个月前,微博的图片外链失效了,以及掘金因为盗链问题也于2019/06/06决定开启防盗链,造成的影响是:个人博客网站的引用了这些图片外链都不能显示. 目前微博和掘金的屏蔽,在CSDN和se ...

  6. 网易云免费OSS服务用做Markdown图床或博客图片外链

    我使用据说是Windows下最好用的Markdown编辑器“MarkdownPad2”(个人感觉还是Visual Code+Markdown插件666)写Markdown,在贴图方面遇到一个问题,于是 ...

  7. 博客图片上传picgo工具安装配置github图传使用

    摘要 对于每一个写博客的人来说,图片是至关重要.这一路经历了多次图片的烦恼,之前选择了微博个人文章那里粘贴图片的方式上传,感觉也挺方便的.但是由于新浪的图片显示问题,如果header中不设置 标签就不 ...

  8. 修正_typora文档复制到博客图片失效

    开始 今天开始尝试使用 Typora 写markdown 然后复制到博客园,不过会有一个问题 那就是 typroa 插入的图片都是本地的,md文档复制到博客园之后,图片都失效了 通过百度,有工具可以直 ...

  9. Hexo 博客图片添加至图床---腾讯云COS图床使用。

    个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 腾讯云官网 登录注册 创建存储桶 进入上面的存 ...

随机推荐

  1. Spring总结三:DI(依赖注入)

    简介: 所谓的依赖注入,其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中,比如你使用Spring容器创建的对象A里面需 ...

  2. 刷题向》关于搜索+tarjan的奇怪组合题 BZOJ1194 (normal+)

    关于这道题,其实看懂了的话还是比较好写的,只是题目实在又臭又长,没有让人读下去的勇气. 给出题目翻译: 给你S张图, 每张图有M个点,其中M个点中有N个是特殊单位,会给出. 每个点又有0.1两条边指向 ...

  3. PHP内核介绍及扩展开发指南—Extensions 的编写

    Extensions 的编写 理解了这些运行机制以后,本章着手介绍Extensions 的编写,但凡写程序的人都知道hello world,那好,就从hello world开始. 1.1Hello W ...

  4. IntelliJ IDEA——maven环境下整合SSM

    SSM整合目录结构 开发环境:JDK1.8:apache-tomcat-7.0.52 : MySql5.7 开发工具:IntelliJ IDEA pom.xml <?xml version=&q ...

  5. linux 首次登陆与线上求助

    开始下达指令概念 上述指令详细说明如下:1. 一行指令中第一个输入的部分绝对是『指令(command)』或『可执行文件案(例如批次脚本,script)』2. command 为指令的名称,例如变换工作 ...

  6. 构建使用 Azure 网站的云

    Apurva JoshiSunitha Muthukrishna 在设计云解决方案时,设计始终要为故障做好准备.这一点很重要,应牢记. 然而,许多应用程序并非按照这种方式构建. 出现这种情况的主要原因 ...

  7. jqGrid查询案例(实用)

    var ThisTime = getNowFormatDate(); //加载表格 function GetGrid() { var selectedRowIndex = 0; var $gridTa ...

  8. 设计模式07: Bridge 桥接模式(结构型模式)

    Bridge 桥接模式(结构型模式) 抽象与实现 抽象不应该依赖于实现细节,实现细节应该依赖于抽象. 抽象B稳定,实现细节b变化 问题在于如果抽象B由于固有的原因,本身并不稳定,也有可能变化,怎么办? ...

  9. Python htmlTestRunner生成测试报告Demo

    #该代码段是ReadTxt_demo.py 的代码,用户读取txt 文件中的用户信息. #ReadTxt_demo.py def readTxt(filePath): fo = open(filePa ...

  10. .net core webapi 文件上传在 Swagger 文档中的有好提示处理

    前提: 需要nuget   Swashbuckle.AspNetCore 我暂时用的是  4.01 最新版本: 描述:解决 .net core webapi 上传文件使用的是 IFormFile,在S ...