forked from Geralt-TYH/obsidian-zhihu-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
313 lines (252 loc) · 10.5 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding:utf-8 -*-
import os
import random
import sys
import time
import requests
from bs4 import BeautifulSoup
import re
from tqdm import tqdm
import argparse
from utils import filter_title_str
from markdownify import MarkdownConverter
parser = argparse.ArgumentParser(description='知乎文章剪藏')
parser.add_argument('collection_url', metavar='collection_url', type=str,nargs=1,
help='收藏夹(必须是公开的收藏夹)的网址')
class ObsidianStyleConverter(MarkdownConverter):
"""
Create a custom MarkdownConverter that adds two newlines after an image
"""
def chomp(self, text):
"""
If the text in an inline tag like b, a, or em contains a leading or trailing
space, strip the string and return a space as suffix of prefix, if needed.
This function is used to prevent conversions like
<b> foo</b> => ** foo**
"""
prefix = ' ' if text and text[0] == ' ' else ''
suffix = ' ' if text and text[-1] == ' ' else ''
text = text.strip()
return (prefix, suffix, text)
def convert_img(self, el, text, convert_as_inline):
alt = el.attrs.get('alt', None) or ''
src = el.attrs.get('src', None) or ''
downloadDir = os.path.join(os.path.expanduser("~"), "Downloads", "剪藏")
if not os.path.exists(downloadDir):
os.mkdir(downloadDir)
assetsDir = os.path.join(downloadDir,'assets')
if not os.path.exists(assetsDir):
os.mkdir(assetsDir)
img_content = requests.get(url=src, headers=headers).content
img_content_name = src.split('?')[0].split('/')[-1]
imgPath = os.path.join(assetsDir,img_content_name)
with open(imgPath, 'wb') as fp:
fp.write(img_content)
return '![[%s]]\n(%s)\n\n' % (img_content_name, alt)
def convert_a(self, el, text, convert_as_inline):
prefix, suffix, text = self.chomp(text)
if not text:
return ''
href = el.get('href')
# title = el.get('title')
if el.get('aria-labelledby') and el.get('aria-labelledby').find('ref') > -1:
text = text.replace('[', '[^')
return '%s' % text
if (el.attrs and 'data-reference-link' in el.attrs) or ('class' in el.attrs and ('ReferenceList-backLink' in el.attrs['class'])):
text = '[^{}]: '.format(href[5])
return '%s' % text
return super(ObsidianStyleConverter, self).convert_a(el, text, convert_as_inline)
def convert_li(self, el, text, convert_as_inline):
if el and el.find('a', {'aria-label': 'back'}) is not None:
return '%s\n' % ((text or '').strip())
return super(ObsidianStyleConverter, self).convert_li(el, text, convert_as_inline)
def markdownify(html, **options):
return ObsidianStyleConverter(**options).convert(html)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Connection": "keep-alive",
"Accept": "text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8"
}
# 获取收藏夹的回答总数
def get_article_nums_of_collection(collection_id):
"""
:param starturl: 收藏夹连接
:return: 收藏夹的页数
"""
try:
collection_url = "https://www.zhihu.com/api/v4/collections/{}/items".format(collection_id)
html = requests.get(collection_url, headers=headers)
html.raise_for_status()
# 页面总数
return html.json()['paging'].get('totals')
except:
return None
# 解析出每个回答的具体链接
def get_article_urls_in_collection(collection_id):
collection_id =collection_id.replace('\n','')
offset = 0
limit = 20
article_nums = get_article_nums_of_collection(collection_id)
url_list = []
title_list = []
while offset < article_nums:
collection_url = "https://www.zhihu.com/api/v4/collections/{}/items?offset={}&limit={}".format(collection_id,
offset, limit)
try:
html = requests.get(collection_url, headers=headers)
content = html.json()
except:
return None
for el in content['data']:
url_list.append(el['content']['url'])
try:
if el['content']['type'] == 'answer':
title_list.append(el['content']['question']['title'])
else:
title_list.append(el['content']['title'])
except:
print('********')
print('TBD 非回答, 非专栏, 想法类收藏暂时无法处理')
for k, v in el['content'].items():
if k in ['type', 'url']:
print(k, v)
print('********')
url_list.pop()
offset += limit
return url_list, title_list
# 获得单条答案的数据
def get_single_answer_content(answer_url):
# all_content = {}
# question_id, answer_id = re.findall('https://www.zhihu.com/question/(\d+)/answer/(\d+)', answer_url)[0]
html_content = requests.get(answer_url, headers=headers)
soup = BeautifulSoup(html_content.text, "lxml")
try:
answer_content = soup.find('div', class_="AnswerCard").find("div", class_="RichContent-inner")
except:
print(answer_url, 'failed')
return -1
# 去除不必要的style标签
for el in answer_content.find_all('style'):
el.extract()
for el in answer_content.select('img[src*="data:image/svg+xml"]'):
el.extract()
for el in answer_content.find_all('a'): # 处理回答中的卡片链接
aclass = el.get('class')
if isinstance(aclass, list):
if aclass[0] == 'LinkCard':
linkcard_name = el.get('data-text')
el.string = linkcard_name if linkcard_name is not None else el.get('href')
else:
pass
try:
if el.get('href').startswith('mailto'): # 特殊bug, 正文的[email protected]会被识别为邮箱, 嵌入<a href='mailto:xxx'>中, markdown转换时会报错
el.name = 'p'
except:
print(answer_url, el) # 一些广告卡片, 不需要处理
# 添加html外层标签
answer_content = html_template(answer_content)
return answer_content
# 获取单条专栏文章的内容
def get_single_post_content(paper_url):
html_content = requests.get(paper_url, headers=headers)
soup = BeautifulSoup(html_content.text, "lxml")
post_content = soup.find("div", class_="Post-RichText")
# 去除不必要的style标签
if post_content:
for el in post_content.find_all('style'):
el.extract()
for el in post_content.select('img[src*="data:image/svg+xml"]'):
el.extract()
for el in post_content.find_all('a'): # 处理专栏文章中的卡片链接
aclass = el.get('class')
if isinstance(aclass, list):
if aclass[0] == 'LinkCard':
linkcard_name = el.get('data-text')
el.string = linkcard_name if linkcard_name is not None else el.get('href')
else:
pass
try:
if el.get('href').startswith('mailto'): # 特殊bug, 正文的[email protected]会被识别为邮箱, 嵌入<a href='mailto:xxx'>中, markdown转换时会报错
el.name = 'p'
except:
print(paper_url, el)
else:
post_content = "该文章链接被404, 无法直接访问"
# 添加html外层标签
post_content = html_template(post_content)
return post_content
def html_template(data):
# api content
html = '''
<html>
<head>
</head>
<body>
%s
</body>
</html>
''' % data
return html
if __name__=='__main__':
args = parser.parse_args()
collection_url = args.collection_url[0]
collection_id = collection_url.split('?')[0].split('/')[-1]
urls, titles = get_article_urls_in_collection(collection_id)
assert len(urls) == len(titles), '地址标题列表长度不一致'
print('共获取 %d 篇可导出回答或专栏' % len(urls))
downloadDir = os.path.join(os.path.expanduser("~"), "Downloads", "剪藏")
if not os.path.exists(downloadDir):
os.mkdir(downloadDir)
for i in tqdm(range(len(urls))):
content = None
url = urls[i]
title = titles[i]
if os.path.exists(os.path.join(downloadDir, filter_title_str(title) + ".md")): # 跳过已经保存的文件
continue
if url.find('zhuanlan') != -1:
content = get_single_post_content(url)
else:
content = get_single_answer_content(url)
if content == -1:
print(url, 'get content failed.')
continue
try:
md = markdownify(content, heading_style="ATX")
md = '> %s\n' % url + md
id = url.split('/')[-1]
with open(os.path.join(downloadDir, filter_title_str(title) + ".md"), "w", encoding='utf-8') as md_file:
md_file.write(md)
# print("{} 转换成功".format(id))
time.sleep(random.randint(1,5))
except Exception as e:
print(content)
print(e)
print(url, 'error')
print("全部下载完毕")
# def testMarkdownifySingleAnswer():
# url = "https://www.zhihu.com/question/506166712/answer/2271842801"
# content = get_single_answer_content(url)
# md = markdownify(content, heading_style="ATX")
# id = url.split('/')[-1]
#
# downloadDir = os.path.join(os.path.expanduser("~"), "Downloads", "剪藏")
# if not os.path.exists(downloadDir):
# os.mkdir(downloadDir)
# with open(os.path.join(downloadDir, id + ".md"), "w", encoding='utf-8') as md_file:
# md_file.write(md)
# print("{} 转换成功".format(id))
#
# def testMarkdownifySinglePost():
# url = 'https://zhuanlan.zhihu.com/p/386395767'
# content = get_single_post_content(url)
# md = markdownify(content, heading_style="ATX")
# id = url.split('/')[-1]
# with open("./" + id + ".md", "w", encoding='utf-8') as md_file:
# md_file.write(md)
# print("{} 转换成功".format(id))
#
#
# # if __name__ == '__main__':
# # testMarkdownifySingleAnswer()
#