forked from yehot/sina-blog-spider
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sina_blog_list.py
201 lines (161 loc) · 6.3 KB
/
sina_blog_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/urs/bin/env python
# -*_ coding: utf-8 -*-
import sys
import urllib2
import codecs
import os
from time import strftime
def get_between(str0, str1, str2):
start = str0.find(str1)+len(str1)
end = str0.find(str2)
return str0[start:end]
k_usage_tip = '''need input your url!
Usage:
$ SBB.py <Sina blog URL> [asc]
Example:
$ SBB.py http://blog.sina.com.cn/gongmin desc
$ SBB.py http://blog.sina.com.cn/u/1239657051'''
# # 第一个参数
try:
urlInput = sys.argv[1]
except Exception as e:
print(k_usage_tip)
print(e)
sys.exit(0)
k_sina_domain = "http://blog.sina.com.cn/"
k_sina_article_url = k_sina_domain + "s/articlelist_"
k_sina_blog_url = k_sina_domain + "s/blog_"
if urlInput.find(k_sina_domain) == -1 or len(urlInput) <= 24:
print(k_usage_tip)
sys.exit(0)
# 第二个参数
# try except 用法 [https://segmentfault.com/a/1190000007736783]
try:
orderInput = sys.argv[2]
except:
orderInput = ""
print("默认升序排列")
# 在python3.3里面,用urllib.request代替urllib2
# python2.7 中,使用 urllib2.urlopen(strUserInput) 获得的是 string
# 在 3.5 中使用,得到的是 bit,需要 decode
def get_html_body(url):
response = urllib2.urlopen(url)
text = response.read()
text = text.decode(encoding='utf-8', errors='ignore')
response.close()
return text
htmlText = get_html_body(urlInput)
# Get UID for the blog, UID is critical.
htmlBody = get_between(htmlText, "format=html5;", "format=wml;")
strUID = get_between(htmlBody, "/blog/u/", '">')
if len(strUID) > 10:
print(k_usage_tip)
sys.exit(0)
# Step 1: get list for first page and article count
htmlText = get_html_body(k_sina_article_url + strUID + "_0_1.html")
# 文章编号列表
strSortDOM = "$blogArticleSortArticleids"
srtCategoryDom = "$blogArticleCategoryids"
strList = get_between(htmlText, strSortDOM, srtCategoryDom)
srtBlogPostList = get_between(strList, " : [", "],")
def get_blog_count(text):
strContent = get_between(text, u"<!--第二列start-->", u"<!-- 列表 START -->")
# 获取页数
count = get_between(strContent, "<em>(", ")</em>")
return int(count)
# 文章总数
blog_amount = get_blog_count(htmlText)
# 总页数 (默认每页 50 篇)
blog_page_amount = int(blog_amount / 50) + 1
strTitle = get_between(htmlText, "<title>", "</title>")
strBlogName = get_between(strTitle, u"博文_", u"_新浪博客")
# Step 2: get list for the rest of pages
for intPage in range(blog_page_amount - 1):
strURL = k_sina_article_url + strUID + "_0_" + str(intPage + 2) + ".html"
htmlText = get_html_body(strURL)
strPost = get_between(htmlText, strSortDOM, srtCategoryDom)
strPostList = get_between(strPost, " : [", "],")
srtBlogPostList = srtBlogPostList + "," + strPostList
# strPostID <- this string has all article IDs for current blog
strPostID = srtBlogPostList.replace('"', '')
# Step 3: get all articles one by one
arrBlogPost = strPostID.split(',')
if orderInput != "desc":
arrBlogPost.reverse()
kBlogDir = "docs"
# python 3.2创建目录新增了可选参数existok
# 把 exist_ok 设置True,创建目录如果已经存在则不会往外抛出异常
if False == os.path.exists(kBlogDir):
os.makedirs(kBlogDir)
def ge_artile_title(page_code, name):
title = get_between(page_code, "<title>", "</title>")
title = title.replace(u"_新浪博客", "")
title = title.replace("_" + name, "")
return title
def get_artile_body(text):
body = get_between(text, u"<!-- 正文开始 -->", u"<!-- 正文结束 -->")
body = body.replace("http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif", "")
body = body.replace('src=""', "")
body = body.replace("real_src =", "src =")
return body
def get_artile_time(text):
dom1 = '<span class="time SG_txtc">('
dom2 = ')</span>'
return get_between(text, dom1, dom2)
# 3.x 中直接使用 open 会得到 <_io.TextIOWrapper name='' mode='' encoding='US-ASCII'> 对象
# 导致 write str 时,报错UnicodeEncodeError: 'ascii' codec can't encode characters in position
# 改用 codecs.open
def write_file(file_path, content):
objFileIndex = codecs.open(file_path, "w", 'utf-8')
objFileIndex.write(content)
objFileIndex.close
intCounter = 0
# index.html 中的内容
strHTML4Index = ""
for strPostID in arrBlogPost:
intCounter += 1
htmlText = get_html_body(k_sina_blog_url + strPostID + '.html')
# Parse blog title
strTitle = ge_artile_title(htmlText, strBlogName)
# Parse blog post
strBody = get_artile_body(htmlText)
# Parse blog timestamp
strTime = get_artile_time(htmlText)
# Write into local file
strFileName = "Post_" + strPostID + ".html"
# %s 的 format 字符串是 是 2.x 写法, 3.x 使用 str.format()
# 参考[http://blog.xiayf.cn/2013/01/26/python-string-format/]
strHTML4Post = u'''
<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
<title>{strTitle}</title>
<link href=""http://simg.sinajs.cn/blog7style/css/conf/blog/article.css""
type=""text/css"" rel=""stylesheet"" />
</head>
<body>
<h2>{strTitle}</h2>
<p>By: <em>{strBlogName}</em> 原文发布于:<em>{strTime}</em></p>
{strBody}
<p><a href="index.html">返回目录</a></p>
</body>
</html>'''
strHTML4Post = strHTML4Post.format(strTitle=strTitle, strBlogName=strBlogName, strTime=strTime, strBody=strBody)
write_file(kBlogDir + '/' + strFileName, strHTML4Post)
strHTML4Index = strHTML4Index + '<li><a href="' + strFileName + '">' + strTitle + '</a></li>\n'
print(intCounter, "/", blog_amount)
strTimestamp = str(strftime("%Y-%m-%d %H:%M:%S"))
strHTMLBody = u'''
<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
<title>{name} 博客文章汇总</title>
</head>
<body>
<h2>新浪博客:{name}</h2>
<p>共 {page} 篇文章,最后更新:<em>{time}</em></p>
<ol>{index}</ol>
</body>
</html>'''
strHTML4Index = strHTMLBody.format(name=strBlogName, page=str(blog_amount), index=strHTML4Index, time=strTimestamp)
write_file(kBlogDir + "/index3.html", strHTML4Index)