2015-06-23 29 views
0

我试图按照教程做在Python Reddit和Twitter的僵尸。我使用的Python(2.7.10),因为我相信这是在教程中使用的版本,但我有以下错误:的Python(2.7.10):主要错误:“ID”

Traceback (most recent call last): 
    File "C:\Python27\twitterbot.py", line 82, in <module> 
     main() 
    File "C:\Python27\twitterbot.py", line 63, in main 
     post_dict, post_ids = tweet_creator(subreddit) 
    File "C:\Python27\twitterbot.py", line 30, in tweet_creator 
     short_link = shorten(post_link) 
    File "C:\Python27\twitterbot.py", line 46, in shorten 
     link = json.loads(r.text)['id'] 
     KeyError: 'id' 

完整的脚本可以在下面看到(令牌和键删除):

import praw 
import json 
import requests 
import tweepy 
import time 

access_token = 'REMOVED' 
access_token_secret = 'REMOVED' 
consumer_key = 'REMOVED' 
consumer_secret = 'REMOVED' 

def strip_title(title): 
    if len(title) < 94: 
     return title 
    else: 
     return title[:93] + "..." 

def tweet_creator(subreddit_info): 
    post_dict = {} 
    post_ids = [] 
    print "[Computer] Getting posts from Reddit" 
    for submission in subreddit_info.get_hot(limit=20): 
     post_dict[strip_title(submission.title)] = submission.url 
     post_ids.append(submission.id) 
    print "[Computer] Generating short link using goo.gl" 
    mini_post_dict = {} 
    for post in post_dict: 
     post_title = post 
     post_link = post_dict[post]   
     short_link = shorten(post_link) 
     mini_post_dict[post_title] = short_link 
    return mini_post_dict, post_ids 

def setup_connection_reddit(subreddit): 
    print "[Computer] setting up connection with Reddit" 
    r = praw.Reddit('yasoob_python reddit twitter Computer ' 
       'monitoring %s' %(subreddit)) 
    subreddit = r.get_subreddit(subreddit) 
    return subreddit 

def shorten(url): 
    headers = {'content-type': 'application/json'} 
    payload = {"longUrl": url} 
    url = "https://www.googleapis.com/urlshortener/v1/url" 
    r = requests.post(url, data=json.dumps(payload), headers=headers) 
    link = json.loads(r.text)['id'] 
    return link 

def duplicate_check(id): 
    found = 0 
    with open('posted_posts.txt', 'r') as file: 
     for line in file: 
      if id in line: 
       found = 1 
    return found 

def add_id_to_file(id): 
    with open('posted_posts.txt', 'a') as file: 
     file.write(str(id) + "\n") 

def main(): 
    subreddit = setup_connection_reddit('showerthoughts') 
    post_dict, post_ids = tweet_creator(subreddit) 
    tweeter(post_dict, post_ids) 

def tweeter(post_dict, post_ids): 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 
    api = tweepy.API(auth) 
    for post, post_id in zip(post_dict, post_ids): 
     found = duplicate_check(post_id) 
     if found == 0: 
      print "[Computer] Posting this link on twitter" 
      print post+" "+post_dict[post]+" #Python #reddit #Computer" 
      api.update_status(post+" "+post_dict[post]+" #Python #reddit #Computer") 
      add_id_to_file(post_id) 
      time.sleep(30) 
     else: 
      print "[Computer] Already posted" 

if __name__ == '__main__': 
    main() 
+0

看看这个[SO问题] [1] [1]:http://stackoverflow.com/questions/24816114/keyerror-when-key-exists – TM89

+0

我正在试图使这与我的问题有关,但做得不太好。将继续,谢谢。 – user2183587

+0

已解析的json中有哪些键('json.loads(r.text).keys()')? –

回答

0

我遇到了一些类似的问题,但我不知道这是否是同样的问题。 从PRAW 3.0开始,Redditor类正在使用用于PRAW 2.x中的Subreddit类的lazyload功能。 你可以使用 断言(redditor.has_fetched) 检查对象加载与否。

具体到类Redditor,既“ID”和“名称”被lazyload属性,以及相同的其他一些属性,如“link_karma”。我以前直接为它们进行查询: 瓦尔(redditor)[“身份证”] 它的工作对PRAW 2.x和报告PRAW 3.0 我修复的错误是调用: redditor.link_karma 加载的所有功能。