保存一个网页的所有相关资源(包括图片,js,css)的两种方法
发现一个很酷的网页,想把它保存下来怎么办? ??
网上找离线下载工具? 找了半天,没有找到合适的。
决定自己写一个, 我想可能以后会用到。
第一种方法是使用wget方式下载。 比较简单,但是一些高级的自定义的无法实现。 例如有的网页是如下这种方式,就没有办法爬了。
wget \--page-requisites \
--convert-links \
--domains www.17sucai.com \
--no-parent \
http://www.17sucai.com/preview/949344/2018-03-19/LoginHTML/demo.html
保存之后的路径
参数说明
The options are:
–recursive: download the entire Web site.
–domains website.org: don’t follow links outside website.org.
–no-parent: don’t follow links outside the directory tutorials/html/.
–page-requisites: get all the elements that compose the page (images, CSS and so on).
–html-extension: save files with the .html extension.
–convert-links: convert links so that they work locally, off-line.
–restrict-file-names=windows: modify filenames so that they will work in Windows as well.
–no-clobber: don’t overwrite any existing files (used in case the download is interrupted and resumed).
再提供一种python的方法
用python的方法稍微复杂一点,但是可以满足灵活的需求。
# -*- coding:UTF8 -*-import re
headers = {'Host': 'riji.bozhong.com',
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"User-Agent": "Baiduspider+(+http://www.baidu.com/search/spider.htm)",
"Upgrade-Insecure-Requests": "1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Language":"zh,zh-CN;q=0.9,en-US;q=0.8,en;q=0.7"}from urllib.parse import quote
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.cookiejar
import os
import string#from urlparse import urljoindef sendwithhead(url,data,headers):ret = ""try:if headers is None:request = urllib.request.Request(url, data)else:request = urllib.request.Request(url, data, headers)ret = urllib.request.urlopen(request).read().decode('utf-8')#print(ret)except Exception as e:print(e)return retdef write(mydir,filename,content):if not os.path.exists(mydir):os.makedirs(mydir)mypath = (mydir+filename)#print(mydir + filename,mypath )output = open(mypath, 'w',encoding="utf-8")output.write(content)output.close()def saveurl(url,mydir,filename,headers):
# mydir = mydir.replace("\\\\","\\")if not os.path.exists( (mydir+"/"+filename) ) \or os.path.getsize( (mydir+"/"+filename) )==0:url = quote(url, safe=string.printable)content = sendwithhead(url, None, headers)write(mydir,filename,content)def saveimg(url,localpath):try:if not os.path.exists(localpath) \or os.path.getsize(localpath) == 0:dirname = os.path.dirname(localpath)if not os.path.exists(dirname):os.makedirs(dirname)url = quote(url, safe=string.printable)urllib.request.urlretrieve(url,localpath)except Exception as e:print(e)def read(filepath):file_object = open(filepath,encoding="utf8")try:all_the_text = file_object.read()finally:file_object.close()return all_the_textdef getUrl(html):patternjs = '<script.*?src="(.*?)"'patternimg = '<img.*?src="(.*?)"'patterncss = '<link.*?href="(.*?)"'patternimg2 = '<div.*?data-src="(.*?)"'href = re.compile(patternjs, re.S | re.I ).findall(html)href += re.compile(patternimg, re.S | re.I).findall(html)href += re.compile(patterncss, re.S | re.I).findall(html)href += re.compile(patternimg2, re.S | re.I).findall(html)return hrefrootpath = "C:/Users/xwm/Desktop/wangye/"
url = "http://www.17sucai.com/preview/949344/2018-03-19/LoginHTML/demo.html"
filename = url.split("/")[-1]
saveurl(url, rootpath , filename,headers)
html = read(rootpath+filename)
urls = getUrl(html)for item in urls:print(item)myurl = urllib.parse.urljoin(url,item)if "http" in myurl:o = urllib.parse.urlparse(item)filename = myurl.split("/")[-1]filedir = o.path.replace(filename,"")if item.endswith(".jpg") or item.endswith(".png") or item.endswith(".gif") :saveimg(myurl, rootpath + o.path)else:saveurl(myurl, rootpath + filedir, filename, None)
保存一个网页的所有相关资源(包括图片,js,css)的两种方法相关推荐
- vue 图片转base64的两种方法(包括h5+plus调取手机图片)
vue 图片转base64的两种方法(包括h5+plus调取手机图片) 1.获取图片文件对象进行转换(主要是对PC端的) 在main.js文件下添加全局方法 Vue.prototype.$base64 ...
- xp系统网页java不显示,xp系统打开jsp文件的两种方法
使用xp系统的时候会碰到各种文件,比如png.log.exe等等,一位用户说遇到后缀名是jsp的文件,jsp文件是什么?有什么作用呢?jsp格式文件是一种动态网页技术标准,是用java script脚 ...
- 近似推断包括采样和变分两种方法,前者是通过_____进行近似,后者是通过_______进行近似。
Single Choice (1 point) 近似推断包括采样和变分两种方法,前者是通过_____进行近似,后者是通过_______进行近似. 采样来对目标分布的期望; 把多个变量的积的求和问题转换 ...
- html5保存到桌面,win8.1将IE浏览器网页内容保存到电脑桌面的两种方法
win8.1系统使用IE浏览器浏览网页的时候,看到有用的网页内容,想要保存下来,这样即使在没有联网的情况下也能够打开该网页查看.那么win8.1将IE浏览器网页内容保存到电脑桌面呢?下面小编介绍两种方 ...
- 怎么把html数据导入excel,将网页表格数据导入到Excel中的两种方法
将网页表格数据导入到Excel中的第一种方法: 第一步,将包括所需表格的网页打开,并按CTRL+C把网址复制到剪贴板,以备下一步使用. 第二步,打开运行Excel软件,单击菜单栏中的"数据→ ...
- JAVA中初始化线程的两种方法_java中最简单的方式新起一个线程
启动一个线程 在一个方法中启动一个线程,有两种方法 第一种是让类实现Runable接口,这样的话编译器就会提示你实现里面的未实现的方法(就是run方法) 第二种是,现在方法中new一个线程,然后直接调 ...
- html边框直线代码,网页制作学习:实现细线边框的两种方法_html
很多朋友都有过制作网页的经历,如今,众多网页的设计都用到了表格.这样不仅有利于网页的维护,同时,提高了网页的观赏性.在众多网页制作风格中,细边框这个制作方法是必不可少的.这里,我将简单地谈一下细边框的 ...
- NLP:两种方法(自定义函数和封装函数)实现提取两人对话内容(***分隔txt文档),并各自保存为txt文档
NLP:两种方法(自定义函数和封装函数)实现提取两人对话内容(***分隔txt文档),并各自保存为txt文档 目录 问题探究 实现代码 问题探究 实现代码 f=open("niu.txt&q ...
- 加载dict_PyTorch 7.保存和加载pytorch模型的两种方法
众所周知,python的对象都可以通过torch.save和torch.load函数进行保存和加载(不知道?那你现在知道了(*^_^*)),比如: x1 = {"d":" ...
最新文章
- 微信内置浏览器无法清除缓存问题
- Tmux终端复用工具小解
- vue源码解析之选项合并(二)
- 并发的核心:CAS 是什么?Java8是如何优化 CAS 的?
- 基于.NET实现数据挖掘--线性回归算法
- 告别 ROR windows 部署的噩梦-在 windows 上面 使用 Apache 部署 Ruby On Rails
- React 项目开发问题积累
- 基于密度的异常值检测方法整理
- 大道至简:算法工程师须知的十个炼丹trick
- SAP License:SAP凭证的类别和记账码
- MySQL 事务控制语句(TCL)
- 8种处理VMware报错的高效方法
- java连接sybase的pom_SpringBoot下多数据源连接Sybase数据库
- 组合数学(2)——组合矩阵
- 定制小狼豪(五笔+拼音)输入法
- Fluent compiled 失败:UDF library you are trying to load (libudf) is not compiled for parallel on the
- Preliminary Design Review(初步设计评审(回顾))
- 文件下载兼容ie,Firefox,chrome
- Designing an IAM Framework with Oracle Identity and Access Management Suite[文摘]
- JdbcTemplate增删改查总结