在 Tumblr API 中使用多个 "blog name"/命令行参数
Using multiple "blog name"/command line arguments with Tumblr API
不久前,我在此处发帖寻求使用 API 从 Tumblr 博客下载数据的帮助。蓝莓 (https://whosebug.com/users/297696/birryree) was kind enough to help me correct my script and figure out where I had been going wrong, and I have been using his script with no problems since ().
此脚本要求我每次手动输入我要下载的博客名称。但是,我需要下载数百个博客,因此这导致我使用同一个脚本的数百个版本并且非常耗时。我做了一些谷歌搜索,发现可以编写 Python 脚本,您可以在其中从命令行输入参数,然后它们将被一个一个地处理(如果这是正确的术语)。
我尝试编写一个脚本,让我从命令提示符中 运行 命令,然后下载我在命令提示符中要求的三个博客。 (在这种情况下,“prettythingsicantafford.tumblr.com;theficrecfairy.tumblr.com;和 staff.tumblr.com)。
所以我尝试 运行 的脚本是:
import pytumblr
import sys
def get_all_posts(client, blog):
offset = 0
while True:
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
# Get the 'posts' field of the response
posts = response['posts']
if not posts: return
for post in posts:
yield post
# move to the next offset
offset += 20
client = pytumblr.TumblrRestClient('SECRET')
blog = (sys.argv[1], sys.argv[2], sys.argv[3])
# use our function
with open('{}-posts.txt'.format(blog), 'w') as out_file:
for post in get_all_posts(client, blog):
print >>out_file, post
我运行正在命令提示符下执行以下命令
tumblr_test2.py theficrecfairy prettythingsicantafford staff
但是,我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\izzy\test\tumblr_test2.py", line 29, in <module>
for post in get_all_posts(client, blog):
File "C:\Users\izzy\test\tumblr_test2.py", line 8, in get_all_posts
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
File "C:\Python27\lib\site-packages\pytumblr\helpers.py", line 46, in add_dot_tumblr
args[1] += ".tumblr.com"
TypeError: can only concatenate tuple (not "str") to tuple
为了响应这个错误,我已经尝试修改我的脚本大约两周了,但我一直无法纠正我无疑是非常明显的错误,非常感谢任何帮助或建议。
根据 vishes_shell 的建议进行编辑:
我现在正在使用以下脚本:
import pytumblr
import sys
def get_all_posts(client, blogs):
for blog in blogs:
offset = 0
while True:
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True, filter='raw')
# Get the 'posts' field of the response
posts = response['posts']
if not posts: return
for post in posts:
yield post
# move to the next offset
offset += 20
client = pytumblr.TumblrRestClient('SECRET')
blog = sys.argv
# use our function
with open('{}-postsredux.txt'.format(blog), 'w') as out_file:
for post in get_all_posts(client, blog):
print >>out_file, post
但是,我现在收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\izzy\test\tumblr_test2.py", line 27, in <module>
with open('{}-postsredux.txt'.format(blog), 'w') as out_file:
IOError: [Errno 22] invalid mode ('w') or filename: "
['C:\\Users\\izzy\\test\\tumblr_test2.py',
'prettythingsicantafford', 'theficrecfairy']-postsredux.txt"
当 blog
是 tuple
对象时,您尝试 client.posts(blog, ...)
的问题,声明为:
blog = (sys.argv[1], sys.argv[2], sys.argv[3])
您需要重构您的方法以分别查看每个博客。
def get_all_posts(client, blogs):
for blog in blogs:
offset = 0
...
while True:
response = client.posts(blog, ...)
...
...
blog = sys.argv
...
不久前,我在此处发帖寻求使用 API 从 Tumblr 博客下载数据的帮助。蓝莓 (https://whosebug.com/users/297696/birryree) was kind enough to help me correct my script and figure out where I had been going wrong, and I have been using his script with no problems since (
此脚本要求我每次手动输入我要下载的博客名称。但是,我需要下载数百个博客,因此这导致我使用同一个脚本的数百个版本并且非常耗时。我做了一些谷歌搜索,发现可以编写 Python 脚本,您可以在其中从命令行输入参数,然后它们将被一个一个地处理(如果这是正确的术语)。
我尝试编写一个脚本,让我从命令提示符中 运行 命令,然后下载我在命令提示符中要求的三个博客。 (在这种情况下,“prettythingsicantafford.tumblr.com;theficrecfairy.tumblr.com;和 staff.tumblr.com)。
所以我尝试 运行 的脚本是:
import pytumblr
import sys
def get_all_posts(client, blog):
offset = 0
while True:
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
# Get the 'posts' field of the response
posts = response['posts']
if not posts: return
for post in posts:
yield post
# move to the next offset
offset += 20
client = pytumblr.TumblrRestClient('SECRET')
blog = (sys.argv[1], sys.argv[2], sys.argv[3])
# use our function
with open('{}-posts.txt'.format(blog), 'w') as out_file:
for post in get_all_posts(client, blog):
print >>out_file, post
我运行正在命令提示符下执行以下命令
tumblr_test2.py theficrecfairy prettythingsicantafford staff
但是,我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\izzy\test\tumblr_test2.py", line 29, in <module>
for post in get_all_posts(client, blog):
File "C:\Users\izzy\test\tumblr_test2.py", line 8, in get_all_posts
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
File "C:\Python27\lib\site-packages\pytumblr\helpers.py", line 46, in add_dot_tumblr
args[1] += ".tumblr.com"
TypeError: can only concatenate tuple (not "str") to tuple
为了响应这个错误,我已经尝试修改我的脚本大约两周了,但我一直无法纠正我无疑是非常明显的错误,非常感谢任何帮助或建议。
根据 vishes_shell 的建议进行编辑:
我现在正在使用以下脚本:
import pytumblr
import sys
def get_all_posts(client, blogs):
for blog in blogs:
offset = 0
while True:
response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True, filter='raw')
# Get the 'posts' field of the response
posts = response['posts']
if not posts: return
for post in posts:
yield post
# move to the next offset
offset += 20
client = pytumblr.TumblrRestClient('SECRET')
blog = sys.argv
# use our function
with open('{}-postsredux.txt'.format(blog), 'w') as out_file:
for post in get_all_posts(client, blog):
print >>out_file, post
但是,我现在收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\izzy\test\tumblr_test2.py", line 27, in <module>
with open('{}-postsredux.txt'.format(blog), 'w') as out_file:
IOError: [Errno 22] invalid mode ('w') or filename: "
['C:\\Users\\izzy\\test\\tumblr_test2.py',
'prettythingsicantafford', 'theficrecfairy']-postsredux.txt"
当 blog
是 tuple
对象时,您尝试 client.posts(blog, ...)
的问题,声明为:
blog = (sys.argv[1], sys.argv[2], sys.argv[3])
您需要重构您的方法以分别查看每个博客。
def get_all_posts(client, blogs):
for blog in blogs:
offset = 0
...
while True:
response = client.posts(blog, ...)
...
...
blog = sys.argv
...