首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Tumblr使用多个“博客名”/command行参数

使用Tumblr使用多个“博客名”/command行参数
EN

Stack Overflow用户
提问于 2018-03-25 09:25:27
回答 1查看 141关注 0票数 0

不久前,我在这里发布了使用API从Tumblr博客下载数据的帮助。birryree (https://stackoverflow.com/users/297696/birryree)很好地帮助我纠正了我的脚本,找出了我的错误所在,而且我一直在使用他的脚本,从那时起我就没有问题了(Print more than 20 posts from Tumblr API)。

这个脚本要求我每次都要手动输入我想下载的博客名。但是,我需要下载数百个博客,所以这导致我使用了数百个相同脚本的版本,而且非常耗时。我做了一些搜索,发现编写Python脚本是可能的,您可以从命令行输入参数,然后逐个处理它们(如果这是正确的术语)。

我试图编写一个脚本,它允许我从命令提示符中运行一个命令,然后在命令提示符中下载我所要求的三个博客。(在本例中,"prettythingsicantafford.tumblr.com;theficrecfairy.tumblr.com;和staff.tumblr.com“)。

所以我想要运行的脚本是:

代码语言:javascript
复制
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

我正在从命令提示符运行以下命令

代码语言:javascript
复制
tumblr_test2.py theficrecfairy prettythingsicantafford staff

但是,我收到以下错误消息:

代码语言:javascript
复制
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建议:

我现在正在使用以下脚本:

代码语言:javascript
复制
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

但是,我现在收到以下错误消息:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-25 09:34:22

client.posts(blog, ...)tuple对象时,您试图进行tuple处理的问题,声明为:

代码语言:javascript
复制
blog = (sys.argv[1], sys.argv[2], sys.argv[3])

您需要重构您的方法,以单独浏览每个博客。

代码语言:javascript
复制
def get_all_posts(client, blogs):
    for blog in blogs:
        offset = 0
        ...

        while True:
            response = client.posts(blog, ...)
        ...
    ...

blog = sys.argv
...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49474348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档