三个问题:
其中pdf标注页码,没有做,因为pdf文件本身就自带了页码功能,所以做的必要性不是很大.
先看效果图
三个星球
效果图带超链接与图片
效果图多张图片展示
开始正文:
这里要先构建好模板,多张图片与多个超链接,都可以用一个字符串填充,特别注意
self.html_template = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>{title}</h1> <h3>{author_time}</h3> <p style='font-size:20px'>正文内容:<br>{text}</p> <p style='font-size:20px'>相关链接:<br>{hrefs}</p> # 这里也是一个字符串,可以放多条链接,后面会讲到 <p style="text-align:center">{images}</p> # 这个images是一个字符串,可以放多张图片
</body> </html> """左右滑动,查看代码
<e type = 'web' href='' title = ''>
我只需要取出这个属性中的href与 title就行了urllib.parse.unquote
来解码, 因为一个动态中可能有多个web标签,所以我这里遍历一下.hrefs
转换成字符串的超链接,然后直接填充就可以展示出来def get_tag_web(self, content):
"""处理一下e标签内容, 主要是web链接有点用处,所以这里只处理web链接"""
soup = BeautifulSoup(content, 'html.parser')
list_e = soup.find_all('e') # 获取 <e>标签列表
hrefs = []
if list_e:
for e in list_e:
if e['type'] == 'web': # 这里只处理web超链接
hrefs.append('<a href="{}">{}</a>'.format(parse.unquote(e['href']), parse.unquote(e['title']))) # 解码和提取 <e > 标签中的 href与 title
return ''.join(hrefs) if len(hrefs) > 0 else '无'
左右滑动,查看代码
把得到的结果填充到模板中去即可
q&a
模式也有图片,所以这里直接用一个方法处理了,图片都在images
列表中, 另外注意 topic.get('talk').get('images')
取到的列表,而不是对象,不能直接append,我在这里犯错找了好久imgs
列表转换成字符串,然后传入模式中即可def get_all_imgs(self, topic):
"""获取帖子中的图片"""
images = []
if topic.get('talk') and topic.get('talk').get('images'): # 会话模式的
images += topic.get('talk').get('images') # 获取图片列表
if topic.get('question') and topic.get('question').get('images'): # 问题模式
images += topic.get('question').get('images') # 获取图片列表
if topic.get('answer') and topic.get('answer').get('images'): # 回答模式
images += topic.get('answer').get('images') # 获取图片列表
try:
imgs = []
if images is not None:
for image in images:
imgs.append(
'<img src = {} ,style = "text-align:center,width:400px,height:300px">'.format(
image.get('large').get('url'))) # 获取原图
return ''.join(imgs)
else:
return ''
except Exception as e:
print(e.args)
return ''
就这样制作知识星球电子书就基本完成了,再回头看看我完成了那些功能