Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6596273
  • 博文数量: 227
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6678
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-11 10:33
个人简介

网上的蜘蛛

文章分类

全部博文(227)

文章存档

2010年(19)

2009年(29)

2008年(179)

分类: Python/Ruby

2010-04-25 22:00:56

Python备 份百度博客 在此功能上做些代码优化,性能还有待...



'''
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(""</span><span style="color: rgb(0, 0, 204);">+</span>str<span style="color: rgb(0, 0, 204);">(</span>title<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">+</span><span style="color: rgb(255, 0, 255);">""+str(htmlContent)+" ")
            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="" + 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(""+blogDate+""
) + str(blogContentBlockZero[0])
    return blogData,blogTitle,htmlContent

def backUpBlog(user,firstBlogUrl ):
    #first read first blog


使用方法跟第一篇相同: 用之前,需要在文件所在目录新建一个目录,例如我的博客就是 codedeveloper,使用这段程序,需要更改两个参数:

 

其中user那里指的是你的用户名,firstBlogUrl说的是你最 新那篇博文的地址~

有个问题:如何支持中文目录 呢?


阅读(4216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~