我正在编写一个脚本,该脚本从指定的 subreddit 获取一些帖子,然后使用该 url 在 tumblr 上使用两个站点 api 和 python 创建一个图像帖子。我的问题是,当我尝试从 reddit 检索 url 以发布到 tumblr 时,我的程序改为打印随机 subreddit 的 url(总是 url https://www.reddit.com/r/accidentallyliberian/)我想知道什么错误在我的代码中,以及为什么它打印该 url 而不是我在 if 语句中指定的关于 url 中包含“.img”或“.png”的字符串的一个。
import praw
import json
import pytumblr
reddit = praw.Reddit(client_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
user_agent='url grabber by /u/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
username='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
oauth_token = 'http://www.tumblr.com/oauth/request_token'
AUTHORIZATION_URL = 'http://www.tumblr.com/oauth/authorize'
ACCESS_TOKEN_URL = 'http://www.tumblr.com/oauth/access_token'
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = pytumblr.TumblrRestClient(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
)
#print(reddit.user.me())
def get_post_url(subreddit, amount):
for submission in reddit.subreddit(subreddit).hot(limit = amount):
if '.img' or '.png' in submission.url:
client.create_photo('username', state="published", tags=["testing", "ok"],
source = submission.url)
#urllib.request.urlretrieve(submission.url, filename)
print(submission.url)
else:
print('')
if __name__ == '__main__':
get_post_url('funny', 1)