#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. git branch几个简单操作

    1.git branch  该命令会列出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 2.git branch deve ...

  2. NPOI生成单元格(列)下拉框

    客户提出能否将导入模板中,课程一列添加下拉框方便选择,不用手输入,以减少输入错误的可能性.于是在网上找了点代码,稍加整理后,形成了以下方案,代码部分: 一:生成课程列表,并放置在excel的单独she ...

  3. elastic search 配置问题

    http://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html 此处有关于ES硬件规格的建议和各种推荐参数. 内存: ...

  4. resignFirstResponder

    What is this resignFirstResponder business? Here is the short version:Some view objects are also con ...

  5. 八、MPxToolCommand, tool command

    1. Tool Property Sheets: 是用来更改context属性的编辑框,类似于attribute editor.(property和attribute本质上是一个意思)作用于activ ...

  6. entityframework 入门-来自微软

    必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet. 1.创 ...

  7. Device Path in WinPrefetchView

    As we know that the Prefetch file is used for optimizing the loading time of the application in the ...

  8. Android开发--布局

    一:LinearLayout  1.线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上.每一个LinearLayout里面又可分为垂直布局(android:orie ...

  9. Linux下/boot目录

    /boot目录存放的是开机所需的文件----内核,开机菜单,及所需配置文件等: (1)系统Kernel的配置文件: (2)启动管理程序GRUB的目录,里面放的都是GRUB在启动时所需要的画面.配置及各 ...

  10. Android IOS WebRTC 音视频开发总结(七二)-- 看到Google Duo,你想到了什么?

    本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 在昨天的Google I/O大会上Go ...