'''
Created on Apr 23, 2010
@author: Leyond
'''
import urllib
from BeautifulSoup import BeautifulSoup
import re
def saveToFile(dir, htmlContent, title,url=""):
nFail = 0
dir +="/%s" % (url)
#print dir
while nFail < 1:
try:
myfile = open(dir, 'w')
myfile.write("<html><head><title>"+str(title)+"</title></head><body>"+str(htmlContent)+"</body></html>")
myfile.close()
return
except:
nFail += 1
print "%s download Fail." % (title)
def findNextBlogHtml(user,htmlContent):
urls = re.findall(r"var.*pre.*?/blog/item/.*?html",htmlContent,re.I)
if(len(urls)==1):
blogUrl = re.findall(r"/blog/item/\w*.html",urls[0],re.I)
print blogUrl[0]
if(len(blogUrl[0])>17):
htmlAddr = blogUrl[0][11:]
#print htmlAddr
else:
htmlAddr ="None"
else:
htmlAddr ="None"
return htmlAddr
def getBlogContentAndTitle(user,htmlUrl):
blogUrl="http://hi.baidu.com/" + user+"/blog/item/"+htmlUrl
sock = urllib.urlopen(blogUrl)
blogHtmlContent = sock.read()
sock.close()
htmlContent = unicode(blogHtmlContent,'gb2312','ignore').encode('utf-8','ignore')
# parser the html content
htmlsoup = BeautifulSoup(htmlContent)
blogContentBlock = htmlsoup.findAll("div",{"id":"m_blog"})
blogContentBlockZero = blogContentBlock[0].findAll("table",{"style":"table-layout:fixed;width:100%"})
#get the title
blogTitleZero = blogContentBlock[0].findAll("div",{"class":"tit"})
blogTitle = blogTitleZero[0].string
#get blog publish date
blogPublishDate = blogContentBlock[0].findAll("div",{"class":"date"})
blogDate = blogPublishDate[0].string
blogData =str("<B>"+blogDate+"</B>") + str(blogContentBlockZero[0])
return blogData,blogTitle,htmlContent
def backUpBlog(user,firstBlogUrl ):
#first read first blog's title and content
blogContent, blogTitle,htmlContent = getBlogContentAndTitle(user,firstBlogUrl)
#save the html to file
saveToFile(user,blogContent,blogTitle,firstBlogUrl)
#find next url
firstBlogUrl = findNextBlogHtml(user,htmlContent)
if firstBlogUrl != "None" :
backUpBlog(user,firstBlogUrl)
else:
print "Backup Finished"
backUpBlog(user="wbweast",firstBlogUrl= "235bf024035c721b8b82a1c6.html")
使用方法跟第一篇相同:
用之前,需要在文件所在目录新建一个目录,例如我的博客就是
codedeveloper,使用这段程序,需要更改两个参数:
其中user那里指的是你的用户名,firstBlogUrl说的是你最
新那篇博文的地址~
有个问题:如何支持中文目录呢?
分享到:
相关推荐
py交换机自动备份脚本。支持华为、思科、华三、 Aruba交换机。将设备ip、账号、密码、写入配置文件运行即可。多台设备一键备份。记录日志,自定义备份位置。需要先安装调用的模块,怎么安装自行百度。
本脚本是用python写的,用于实现redis配置的自动备份。
python百度网盘纯代码分片上传目录文件 不依赖第三方, AppKey等信息需要自己去申请。然后获取code码就可以一直使用。
python 教程百度云在此,请点击以下链 接: 千锋 Python 基础教程:http://pan.baidu.com/s/1qYTZiNE Python 课程教学高手晋级视频总目录: http://pan.baidu.com/s/1hrXwY8k Python 课程 windows 知识点:...
python自动备份mysql数据库,并删除七天前文件。 省去每天手动备份 删除 空出的时间摸鱼不香吗? 备份时间可自己设置 备份文件夹可自己设置 删除文件时间范围可自己设置
python定时压缩备份文件 定时器+os.system方法调用zip命令
人们都喜欢最好的东西,想找 Python 视频教程百度网盘?在这呢。 千锋 Python 基础教程:http://pan.baidu.com/s/1qYTZiNE Python 课程教学高手晋级视频总目录: http://pan.baidu.com/s/1hrXwY8k Python 课程 ...
基于python的百度网盘批量转存工具.zip基于python的百度网盘批量转存工具.zip基于python的百度网盘批量转存工具.zip基于python的百度网盘批量转存工具.zip基于python的百度网盘批量转存工具.zip基于python的百度网盘...
基于Python的数据版本备份、回滚源代码 基于UDP、TCP传输协议 演示视频、项目文档
该脚本是用python编写,用于对每日文件进行压缩备份存放,适用于初学者,望多指教~
python Windows 压缩备份文件(支持多文件夹)
基于python的百度云网盘爬虫,资源包含项目源代码和运行方法,包含百度云网盘爬虫、网站前后端、搜索,整套服务,欢迎伙伴们下载学习。
python 爬取百度搜索结果,及安全验证问题
python爬取百度图片 使用任意关键字 python爬取百度图片 使用任意关键字 python爬取百度图片 使用任意关键字 python爬取百度图片 使用任意关键字 python爬取百度图片 使用任意关键字 python爬取百度图片
在windows环境下通过Python对重要的文件进行备份,备份的文件将打包为zip格式。由于windows环境下不提供标准的zip命令进行打包,因此要通过GnuWin32安装zip命令,并将C:\Program Files (x86)\GnuWin32\bin添加到系统...
Python地理空间分析指南 第2版 pdf文档
python爬取百度百科的页面主要用BeautifulSoup ,urllib2等
全书共分为10章,分别介绍了Python与地理空间分析、地理空间数据、地理空间技术、Python的地理空间分析工具、Python与地理信息系统、Python与遥感、Python与高程数据、Python与地理空间高级建模、实时数据、综合应用...
python 抓取百度云分享数据,百度云最新接口抓取分享链接。
慕课python百度百科爬取1000个页面 里面分为五个python代码文件,从spider入口程序到爬取功能界面,所有代码已经运行通过,均可行