#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. Storm Ack框架笔记

    Storm利用Acker Bolt节点跟踪消息,当Spout发送出去的消息以及这些消息所衍生出来的消息均被处理后,Spout将受到对应于该消息的Ack.实现要点: 1.Storm中每条发送出去的消息都 ...

  2. RabbitMQ介绍6 - 其它

    深入话题 Exchange实现路由的方法 http://www.rabbitmq.com/blog/2010/09/14/very-fast-and-scalable-topic-routing-pa ...

  3. Gocd持续部署利器

    http://www.go.cd/documentation/user/current http://www.go.cd/images/home-image1.png Gocd 是ThoughtWor ...

  4. android studio新项目时提示:Plugin is too old, please update to a more recent version

    今天想写一个程序来测试一下android studo代码,但是创建好项目后,提示: Error:(1, 0) Plugin is too old, please update to a more re ...

  5. JDBC进行批处理

    转自 http://mousepc.iteye.com/blog/1131462 业务场景:当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升 ...

  6. 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree

    Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...

  7. Bootstrap 4 中 Alerts 实现

    Alert 的使用说明 http://v4-alpha.getbootstrap.com/components/alerts/ JavaScript behavior Triggers Enable ...

  8. WCF大文件传输

    WCF传输文件的时候可以设置每次文件的传输大小,如果是小文件的时候,可以很方便的将文件传递到服务端,但是如果文件比较大的话,就不可取了 遇到大文件的话可以采取分段传输的方式进行文件传输 思路: 1.客 ...

  9. PHP获取日期

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

  10. nohup输入密码后继续后台运行

    Linux/Unix 是真正的多用户,多任务.Linux 提供了 fg 和bg 命令,让你轻松调度正在运行的任务. 假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctr ...