自动下载并保存博客 Python脚本
谢了一个自动下载指定人的博客的脚本
这个脚本是用来下载csdn博客的
同样的方法可以下载一般其他网站的博客,如sina
有时页面访问会被拒绝,重新运行即可
这种程序是在分析了指定网站,我在这儿是csdn,之后编写出的
会牵涉到网页的编码问题,有时程序运行会因此终止
我自己的博客已经下载忘了
只是下载网页
使用网页分析后可以整理出文章,那样更实用
#
#blogdownloader_csdn.py
# @Author:onezeros@yahoo.cn ||Zhijie Lee
# I didnot realize the image_download function
# but it's not very difficult,so that you can do it by yourself
#cmd usage:blogdownloader_csdn.py blogname "full directory path"
#further extension :classify the articles to
# "原创""转载" etc.according to csdn
import os
import sys
import unicodedata
import urllib.request
#globle var
username=sys.argv[1]
#username='onezeros'
#total number of pages
total_num=0
dst_urls=[]
#use data as the file name
dst_title=[]
###########################################
#function to find urls of articals
#it's neccessory to verify whether the url exits
def url_finder(url_directory,firstpage=False):
global username,total_num,dst_urls,dst_title
url_f=urllib.request.urlopen(url_directory)
print("open url "+url_directory+" successfully\n")
url_front='/'+username+'/archive/'
for line in url_f.readlines():
lin=line.decode('utf-8')
pos_front=lin.find(url_front)
if(pos_front!=-1 ):
pos_post=len('2009/12/13/4998191.aspx')+len(url_front)+pos_front
if(lin[pos_post]=='#'):
dst_urls.append('http://blog.csdn.net'+lin[pos_front:pos_post])
s=lin[pos_front+len(url_front):pos_post-len('.aspx')]
s=s.replace('/','-')
print(s)
dst_title.append(s)
if(firstpage==True):
pos=lin.find('第1页')
if(pos!=-1):
pt=lin.find('页',pos+5)
total_num=int(lin[pos+5:pt])
if(firstpage==True and total_num==0):
prin
相关文档:
Python 3 是 Guido van Rossum 功能强大的通用编程语言的最新版本。它虽然打破了与 2.x 版本的向后兼容性,但却清理了某些语法方面的问题。本文是系列文章中的第一篇,介绍了影响该语言及向后兼容性的各种变化,并且还提供了新特性的几个例子。
Python 版本 3,也被称为 Python 3000 或 Py3K(仿效 Microsoft® Windows ......
python 中的re 模块
正则表达式
就个人而言,主要用它来做一些复杂字符串分析,提取想要的信息
学习原则:够用就行,需要的时候在深入
现总结如下:
正则表达式中特殊的符号:
“.” 表任意字符
“^ ” 表string起始
“$” 表string 结束
“*” “+” & ......
原帖:http://www.cnblogs.com/jingleguo/archive/2008/06/02/1211820.html
当python中间处理非ASCII编码时,经常会出现如下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
0x??是超出128的数字,python在默认的情况下认为语言的编码是ascii编码,所以无法 ......
这是一个我们在处理中文时, 经常遇到的问题.
python里面基本上要考虑三种编码格式
1 源文件编码
在文件头部使用coding声明。告诉python解释器该代码文件所使用的字符集。
#/usr/bin/python
#coding: utf8
2 内部编码
代码文件中的字符串,经过decode以后,被转换为统一的unicode格式的内部数据,类似于u'*'。unic ......