用IDM下载博客图片
前言
写博客的人一定都会有一个图床,将图片存在那里。发现自己以前没有注意图片来源问题,随手就贴在博客上面了。现在有不少图片都挂了,换句话来说有可能自己目前用的图床不提供服务了,那所有的图片都有可能丢失......所以打算写个脚本,把以前博客的图片保存下来,并在以后发博的时候注意保存本地图片。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下载博客图片的更多相关文章
- Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片
Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片 其实没太大用,就是方便一些,因为现在各个平台之间的图片都不能共享,比如说在 CSDN 不能用简书的图片, ...
- hexo博客图片问题
hexo博客图片问题 第一步 首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folde ...
- Ruby:多线程队列(Queue)下载博客文章到本地
Ruby:多线程下载博客文章到本地的完整代码 #encoding:utf-8 require 'net/http' require 'thread' require 'open-uri' requir ...
- 利用Python进行博客图片压缩
自己写博客的时候常常要插入一些手机拍的照片,都是几M的大小,每张手动压缩太费事了,于是根据自己博客的排版特点用Python写了一个简单的图片压缩脚本,功能是将博客图片生成缩略图,横屏的图片压缩为宽度最 ...
- 博客图片失效?使用npm工具一次下载/替换所有失效的外链图片
前言 大约一个月前,微博的图片外链失效了,以及掘金因为盗链问题也于2019/06/06决定开启防盗链,造成的影响是:个人博客网站的引用了这些图片外链都不能显示. 目前微博和掘金的屏蔽,在CSDN和se ...
- 网易云免费OSS服务用做Markdown图床或博客图片外链
我使用据说是Windows下最好用的Markdown编辑器“MarkdownPad2”(个人感觉还是Visual Code+Markdown插件666)写Markdown,在贴图方面遇到一个问题,于是 ...
- 博客图片上传picgo工具安装配置github图传使用
摘要 对于每一个写博客的人来说,图片是至关重要.这一路经历了多次图片的烦恼,之前选择了微博个人文章那里粘贴图片的方式上传,感觉也挺方便的.但是由于新浪的图片显示问题,如果header中不设置 标签就不 ...
- 修正_typora文档复制到博客图片失效
开始 今天开始尝试使用 Typora 写markdown 然后复制到博客园,不过会有一个问题 那就是 typroa 插入的图片都是本地的,md文档复制到博客园之后,图片都失效了 通过百度,有工具可以直 ...
- Hexo 博客图片添加至图床---腾讯云COS图床使用。
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 腾讯云官网 登录注册 创建存储桶 进入上面的存 ...
随机推荐
- 【bzoj1083】[SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2424 Solved: 1591[Submit][Sta ...
- vs2008评估期已过的解决方法[win7]
以下是网上提供的方法(对win7无效): 启动visual studio 2008后显示对话框:visual studio的试用版评估期已结束.下面有两个按钮,点第一个链接到微软网页,第二个直接关闭. ...
- 面试题:Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- ROS indigo Ubuntu14.04 安装问题
错误信息:Unpacking ros-indigo-desktop-full (1.1.6-0trusty-20181006-135515-0800) ... Errors were encounte ...
- OpenCV 2.4.13 installed in Ubuntu 14 and CMakeLists Demo
1. 配置编译器环境 [compiler] sudo apt-get install build-essential 2. 安装OpenCV的依赖包 [required] -dev pkg-confi ...
- Luogu 4841 城市规划
BZOJ 3456 权限题 太菜了推不出式子 我们设$f(n)$表示$n$个点的无向连通图的数量,那么有 $$f(n) = 2^{\binom{n}{2}} - \sum_{i = 1}^{n - 1 ...
- 内存中DataTable去除重复行
删除内存中DataTable表的重复行 假设在内存中(不是数据库中)有两个表: 表一:TableA Name Phone 张三 123456 李四 123457 王五 1234568 表二:Table ...
- CSS3 grayscale滤镜图片变黑白
1. 使整个页面的图片都变成灰色的,代码如下. html{ font-size: 100%; -webkit-text-size-adjust: none; -ms-text-s ...
- 301 MovedPermanently 重定向
页面永久性移走(301重定向)是一种非常重要的“自动转向”技术. 301重定向可促进搜索引擎优化效果 从搜索引擎优化角度出发,301重定向是网址重定向最为可行的一种办法.当网站的域名发生变更后,搜索引 ...
- IOC AOP 设计模式
IOC AOP 不是什么技术而是一种设计模式 学习 IOC AOP 其实是在学习一种思想. 1.IOC IOC其实是 将对象的创建和获取提取到外部.由外部IOC容器提供需要的组件. 看下面代码: p ...