#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. UVA 10003 切木棍(普通DP)

    切木棍 紫书P278 算是简单的dp了吧,当然,这是看完别人题解后的想法,呵呵,我仍然是想了半小时,没思路,啥时候能自个整个dp啊!!→_→ dp的时候,输入数组必须从1开始,一定要注意状态的设计,和 ...

  2. iOS Storyboard全解析

    来源:http://iaiai.iteye.com/blog/1493956 Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果, ...

  3. jquery.easypiechart.js简介

    此插件主要是用来统计新的访问.跳出率.服务器负载.使用的RAM等,功能很强大,带有HTML5的动画效果,效果非常炫,看效果吧easyPieChart一款新型的EASY饼图数据统计Jquery插件截图: ...

  4. 【C-001】printf理解

    输出整型的数值就不说了, 显示浮点型: printf("%.2f",3.33); //保留两位小数显示,数字前面没有空格填充 显示的时候,设计到0-1之间的数的时候: printf ...

  5. PHP获取日期

    <?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...

  6. QlikView TEXT控件固定显示图片

    QlikView 的Text控件不仅可以用来展示文本也可以用来展示图片. 数据表 2个列,一个是 关联字段(Country),另外一个保存图片物理路径. TEXT控件固定显示图片 ,编辑控件表达式 = ...

  7. json格式的时间转换

    //yyyy-MM-dd HH:mm:SS function JsonDateToDate(jsondate) { var date = new Date(parseInt(jsondate.repl ...

  8. SQL Server常用技巧

    1:在SQL语句中,将存储过程结果集(表)存入到临时表中 insert into #tmp EXEC P_GET_AllChildrenComany '80047' 说明:#tmp要提前创建好 2:字 ...

  9. Java基础以及与C++的一些对比

    这两天回忆一些Java基础,感觉自己很多地方都不是很牢固,也花费在不少时间和不少流量在手机上查资料. 还是写下来这些东西以免再忘记. 同时还是要记住多动手,编程最重要的就是动手敲啊,有想法有疑问就要自 ...

  10. 消息框用法MessageBox

    关键字:C# MessageBox 消息对话框 在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示.在C#中,MessageBox消 ...