我非常一般的编码新手;我钻研这个项目是为了帮助我的朋友在Tumblr上标记她的一万五千多个帖子。我们终于完成了,但她想确定我们没有错过任何东西...... 所以,我搜索了互联网,试图找到一个编码解决方案。我遇到了一个发现here的脚本,据称它完全符合我们的要求 - 所以我下载了Python,并且......它不起作用。
更具体地说,当我点击脚本时,黑盒子会出现大约半秒钟然后消失。我无法截屏该框以找出完全它所说的内容,但我相信它说有语法错误。起初,我尝试使用Python 2.4;它似乎没有找到创建者使用的Json模块,所以我切换到Python 3.3 - Windows的最新版本,这就是语法错误发生的地方。
#!/usr/bin/python
import urllib2
import json
hostname = "(Redacted for Privacy)"
api_key = "(Redacted for Privacy)"
url = "http://api.tumblr.com/v2/blog/" + hostname + "/posts?api_key=" + api_key
def api_response(url):
req = urllib2.urlopen(url)
return json.loads(req.read())
jsonresponse = api_response(url)
post_count = jsonresponse["response"]["total_posts"]
increments = (post_count + 20) / 20
for i in range(0, increments):
jsonresponse = api_response(url + "&offset=" + str((i * 20)))
posts = jsonresponse["response"]["posts"]
for i in range(0, len(posts)):
if not posts[i]["tags"]:
print posts[i]["post_url"]
print("All finished!")
所以,嗯,我的问题是这样的:如果这个编码有一个语法错误,可以修复,然后用于在Tumblr上找到未标记的帖子,那可能是什么错误? 如果此代码已过时(通过Tumblr或通过Python更新),那么有空闲时间的人是否愿意帮助创建新脚本以在Tumblr上查找未标记的帖子?搜索Tumblr,这似乎是一个半常见的问题。
如果重要,Python安装在C:\ Python33中。
感谢您的协助。
答案 0 :(得分:2)
当我点击脚本时,会出现一个黑盒子,大约半秒钟然后 消失
至少,您应该能够从命令行运行Python脚本,例如,执行Exercise 0 from "Learn Python The Hard Way"。
"Finding Untagged Posts on Tumblr" blog post包含Python 2脚本(请查看源代码中的import urllib2
。{3}中将urllib2
重命名为urllib.request
。将脚本移植到Python 3很容易:
#!/usr/bin/env python3
"""Find untagged tumblr posts.
Python 3 port of the script from
http://www.alexwlchan.net/2013/08/untagged-tumblr-posts/
"""
import json
from itertools import count
from urllib.request import urlopen
hostname, api_key = "(Redacted for Privacy)", "(Redacted for Privacy)"
url = "https://api.tumblr.com/v2/blog/{blog}/posts?api_key={key}".format(
blog=hostname, key=api_key)
for offset in count(step=20):
r = json.loads(urlopen(url + "&offset=" + str(offset)).read().decode())
posts = r["response"]["posts"]
if not posts: # no more posts
break
for post in posts:
if not post["tags"]: # no tags
print(post["post_url"])
这是使用官方Python Tumblr API v2 Client(仅限Python 2的库)实现的相同功能:
#!/usr/bin/env python
from itertools import count
import pytumblr # $ pip install pytumblr
hostname, api_key = "(Redacted for Privacy)", "(Redacted for Privacy)"
client = pytumblr.TumblrRestClient(api_key, host="https://api.tumblr.com")
for offset in count(step=20):
posts = client.posts(hostname, offset=offset)["posts"]
if not posts: # no more posts
break
for post in posts:
if not post["tags"]: # no tags
print(post["post_url"])
答案 1 :(得分:1)
Tumblr有一个API。使用它可能会有更好的成功。