下午好,
我对 Python 很陌生,但我正在尝试编写一个代码,该代码允许我将所有帖子(包括“笔记”)从指定的 Tumblr 帐户下载到我的计算机。
鉴于我对编码缺乏经验,我试图找到一个可以让我这样做的预制脚本。我在 GitHub 上找到了几个出色的脚本,但它们都没有真正返回 Tumblr 帖子中的注释(据我所知,如果有人知道,请纠正我!)。
因此,我尝试编写自己的脚本。我在下面的代码中取得了一些成功。它打印来自给定 Tumblr 的最近 20 个帖子(尽管格式相当丑陋——基本上数百行文本都打印到记事本文件的一行中):
#This script prints all the posts (including tags, comments) and also the
#first 20notes from all the Tumblr blogs.
import pytumblr
# Authenticate via API Key
client = pytumblr.TumblrRestClient('myapikey')
#offset = 0
# Make the request
client.posts('staff', limit=2000, offset=0, reblog_info=True, notes_info=True,
filter='html')
#print out into a .txt file
with open('out.txt', 'w') as f:
print >> f, client.posts('staff', limit=2000, offset=0, reblog_info=True,
notes_info=True, filter='html')
但是,我希望脚本连续打印帖子,直到到达指定博客的末尾。
我搜索了这个网站,发现了一个非常相似的问题(Getting only 20 posts returned through PyTumblr),stackoverflow 用户戳回答了这个问题。但是,我似乎无法真正实现 poke 的解决方案,以便它适用于我的数据。实际上,当我运行以下脚本时,根本不会产生任何输出。
import pytumblr
# Authenticate via API Key
client = pytumblr.TumblrRestClient('myapikey')
blog = ('staff')
def getAllPosts (client, blog):
offset = 0
while True:
posts = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
if not posts:
return
for post in posts:
yield post
offset += 20
我应该注意到,这个网站上有几篇关于 Tumblr 笔记的帖子(例如,使用 Tumblr API 获得超过 50 条笔记),其中大多数都询问如何在每个帖子中下载超过 50 条笔记。我对每个帖子只有 50 个注释感到非常满意,这是我想增加的帖子数量。
此外,我已将这篇文章标记为 Python,但是,如果有更好的方法来使用另一种编程语言获取我需要的数据,那就更好了。
非常感谢您抽出宝贵时间!