下载python标准库--python
#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的更多相关文章
- A Byte of Python 笔记(12)python 标准库:sys、os,更多内容
第14章 python 标准库 Python标准库是随Python附带安装的,它包含大量极其有用的模块. sys 模块 sys 模块包含系统对应的功能.如 sys.argv 列表包含命令行参数. # ...
- Python 标准库、第三方库
Python 标准库.第三方库 Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具.这些库可用于文件读写 ...
- 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 ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
- Python标准库14 数据库 (sqlite3)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...
- python标准库00 学习准备
Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...
- Python标准库:内置函数hasattr(object, name)
Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...
随机推荐
- iOS Storyboard全解析
来源:http://iaiai.iteye.com/blog/1493956 Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果, ...
- log4j打印mybatis sql语句
Mybatis默认使用有slf4j 必须加上依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId& ...
- Python基础篇【第8篇】: Socket编程 (一)
Python Socket 官方关于 Socket 的函数请看 http://docs.python.org/library/socket.html 一.socket介绍 在网络编程中的一个基本组件就 ...
- 实习日记:图像检索算法 LSH 的总结与分析
先贴上这两天刚出炉的C++代码.(利用 STL 偷了不少功夫,代码待优化) Head.h #ifndef HEAD_H #define HEAD_H #include "D:\\LiYang ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- JAVA设计模式之模版方法模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述模板方法(Template Method)模式的: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式 ...
- 创建线程方式-pthread
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Range-Based for Loops
for ( decl : coll ) { statement } where decl is the declaration of each element of the passed collec ...
- 一千行MySQL学习笔记
以下为本人当年初学MySQL时做的笔记,也从那时起没再更新过,但还是囊括了基本的知识点,有时还翻出来查查.是不是干货,就看亲们了~ 如果哪天笔记有更新了,我还是会更新该文章滴,其实笔记已经放到了Git ...
- Android IOS WebRTC 音视频开发总结(七一)-- H265/H264有何不同
本文整理自自网络,非原创,喜欢相关文章请关注我们的微信公众号:blackerteam H.265 H.265是ITU-TVCEG继H.264之后所制定的新的视频编码标准.H.265标准围绕着现有的视频 ...