#coding:utf-8
import urllib2
import os,sys
from BeautifulSoup import BeautifulSoup # For processing HTML
from bs4 import BeautifulSoup
class BookSave():
'''
dir:html文件保存目录 url:index.html目录 static_url:js、css所在目录的上级目录
distinguish:用来区分相同tag.name dis_key:所需的tag属性 key1:所取tag
key2:tag属性 key3:tag属性值
'''
def __init__(self,dir,url,static_url,distinguish,dis_key,key1,key2,key3):
self.dir=dir
self.url = url
self.static_url = static_url
self.distinguish = distinguish
self.dis_key = dis_key
self.key1 = key1
self.key2 = key2
self.key3 = key3 def AddUrl(self):
if self.dir != '':
list = os.listdir(self.dir) #列出目录下的所有文件和目录
for line in list:
if os.path.isdir(line):
continue
elif os.path:
self.JieXiCsss(line)
self.JieXiJs(line) def JieXiCsss(self,file):
filePath = os.path.join(self.dir,file)
print filePath
fp = open(filePath)
soup = BeautifulSoup(fp)
head = soup.head
tags = head.findAll('link')#,{'rel':'stylesheet'}
if tags != []:
for item in tags:
try:
item['href'] = self.static_url + item['href']
print item['href']
except KeyError:
continue
else :
print tags,filePath
self.SaveHtml(soup,filePath) def JieXiJs(self,file):
filePath = os.path.join(self.dir,file)
fp = open(filePath)
soup = BeautifulSoup(fp)
head = soup.head
tags = head.findAll('script')#,{'rel':'stylesheet'}
if tags != []:
for item in tags:
try:
item['src'] = self.static_url + item['src']
print item['src']
self.SaveHtml(soup,filePath)
except KeyError:
continue
else :
print tags,filePath
self.SaveFile(soup,filePath) def SaveFile(self,soup,file):
html = str(soup)
with open(file,'wb') as code:
code.write(html) def IsNullArr(self,Arr):
if Arr != []:
return Arr
else:
print 'array is null' def DownLoadHtml(self,arr):
tags = bs.IsNullArr(arr)
for item in tags:
liName = item.parent.name
if any(liName in s for s in self.distinguish):
continue
else:
htmlUrl = self.url + item[self.dis_key]
print htmlUrl
fileName = os.path.join(self.dir,item[self.dis_key])
print 'saving:' + htmlUrl
self.SaveHtml(fileName,htmlUrl) def SaveHtml(self,fileName,htmlUrl):
f = urllib2.urlopen(htmlUrl)
html = f.read()
with open(fileName,"wb") as code:
code.write(html)#.decode('utf-8') def GetSearchResult(self):
doc = urllib2.urlopen(self.url)
soup = BeautifulSoup(doc)
soup.originalEncoding
tag = soup.findAll(self.key1,{self.key2:self.key3})
return tag def SplitString(self,source,sep):
return source.strip().split('/') def CreateDir(self):
if not os.path.exists(self.dir):
os.makedirs(os.path.join(self.dir))
if __name__=='__main__':
urls = 'http://docs.python.org/2/library/'
static_url = 'http://docs.python.org/2/'
dirs = 'E:/demo/PythonLib1/'
bs = BookSave(dirs,urls,static_url,'p','href','a','class','reference internal')
bs.CreateDir()
fileName = os.path.join(dirs,'index.html')
htmlUrl = urls + 'index.html'
bs.SaveHtml(fileName,htmlUrl)
tags = bs.GetSearchResult()
#print tags
bs.DownLoadHtml(tags)
bs.AddUrl()

下载python标准库--python的更多相关文章

  1. A Byte of Python 笔记(12)python 标准库:sys、os,更多内容

    第14章 python 标准库 Python标准库是随Python附带安装的,它包含大量极其有用的模块. sys 模块 sys 模块包含系统对应的功能.如 sys.argv 列表包含命令行参数. # ...

  2. Python 标准库、第三方库

    Python 标准库.第三方库 Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具.这些库可用于文件读写 ...

  3. Python标准库与第三方库详解(转载)

    转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b ...

  4. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  5. 转--Python标准库之一句话概括

    作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...

  6. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

  7. Python标准库14 数据库 (sqlite3)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...

  8. python标准库00 学习准备

    Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...

  9. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

随机推荐

  1. 文件大boss

    1.write()  写命令 f=open("a2.txt",'w',encoding='utf-8') f.write() f.close() 2.closed  判断是否是关闭 ...

  2. c#获取枚举

    在实际开发项目中,我们定义了一个枚举,往往我们需要在下拉框或其它地方展示枚举.为了加深印象,也为了帮到有需要的人,我写了一个DEMO. 第一步,我们定义一个枚举: /// <summary> ...

  3. 解决bash: mysql: command not found 的方法

    root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下 ...

  4. (转)pdf文件结构

    PDF文件结构(一)  ————物理结构 PDF(Portable   Document   Format,便携式文档结构)是一种很有用的文件格式,其最大的特点是平台无关而且功能强大(支持文字/图象/ ...

  5. [IIS]IIS扫盲(七)

    (4)汉化补丁 许多软件都是英文版本的,国人的英语水平普遍不高,包括笔者.因为这个,影响了不少人学习电脑的兴趣. 为了占领市场,软件开发商提供了中文版本:为了大家学习方便,爱好汉化工作的国人制作了汉化 ...

  6. LeetCode Implement pow(x, n).

    这个题目我也没有思路,同学们可以查看这个http://www.cnblogs.com/NickyYe/p/4442867.html 下面是我改进后的代码 第一种方法: class Solution { ...

  7. VS2015 Android

    最近安装了VS2015,体验了一下android 的开发,按模板创建运行了个,试下效果很不错.也可以可视化设计.但昨天再次打开或创建一个android程序后,设计界面直接不能显示,显示错误:(可能是升 ...

  8. 转:WIN7上搭建Windows Phone 8 开发环境——VMware Workstation下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法

    转自:http://www.cnblogs.com/shaddock2013/p/3155024.html 最近在试着在Windows 7上搭建Windows Phone 8的开发调试环境,使用的是V ...

  9. dedecms代码研究二

    dedecms代码研究(2)从index开始现在继续,今天讲的主要是dedecms的入口代码.先打开index.PHP看看里面是什么吧.打开根目录下的index.php嗯映入眼帘的是一个if语句.检查 ...

  10. SQLServer中char、varchar、nchar、nvarchar的区别:

    (1)       定义: char:    固定长度,存储ANSI字符,不足的补英文半角空格. nchar:   固定长度,存储Unicode字符,不足的补英文半角空格 varchar:  可变长度 ...