Python(2.7.10):关键错误:';id';
我正试图按照一个教程用Python制作Reddit和Twitter机器人。我使用了Python(2.7.10),因为我相信这是教程中使用的版本,但是我有以下错误:Python(2.7.10):关键错误:';id';,python,twitter,bots,reddit,praw,Python,Twitter,Bots,Reddit,Praw,我正试图按照一个教程用Python制作Reddit和Twitter机器人。我使用了Python(2.7.10),因为我相信这是教程中使用的版本,但是我有以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Python27\twitterbot.py”,第82行,在 main() 文件“C:\Python27\twitterbot.py”,第63行,在main中 post_dict,post_id=tweet_创建者(subreddit) 文件“C:\Python27\twitterbot.p
回溯(最近一次呼叫最后一次):
文件“C:\Python27\twitterbot.py”,第82行,在
main()
文件“C:\Python27\twitterbot.py”,第63行,在main中
post_dict,post_id=tweet_创建者(subreddit)
文件“C:\Python27\twitterbot.py”,第30行,在tweet\u creator中
短链接=缩短(后链接)
文件“C:\Python27\twitterbot.py”,第46行,缩略为
link=json.load(r.text)['id']
KeyError:'id'
下面可以看到完整的脚本(标记和密钥已删除):
导入praw
导入json
导入请求
进口粗花呢
导入时间
访问令牌='REMOVED'
访问\u令牌\u秘密='REMOVED'
消费者密钥='已删除'
消费者_机密=‘已删除’
def条带标题(标题):
如果len(标题)<94:
返回标题
其他:
返回标题[:93]+“…”
def tweet_创建者(subreddit_信息):
post_dict={}
post_id=[]
打印“[计算机]从Reddit获取帖子”
在subreddit_info.get_hot(限制=20)中提交:
post_dict[strip_title(submission.title)]=submission.url
post_id.append(submission.id)
打印“[计算机]使用goo.gl生成短链接”
mini_post_dict={}
对于post in post_dict:
职位
post_link=post_dict[post]
短链接=缩短(后链接)
mini_post_dict[post_title]=短链接
返回小日志,日志ID
def设置\u连接\u reddit(子reddit):
打印“[计算机]设置与Reddit的连接”
r=praw.Reddit('yasoob_python Reddit twitter Computer'
“监视%s%”(子Reddit))
subreddit=r.get_subreddit(subreddit)
返回子reddit
def缩短(url):
headers={'content-type':'application/json'}
有效负载={“longUrl”:url}
url=”https://www.googleapis.com/urlshortener/v1/url"
r=requests.post(url,data=json.dumps(有效负载),headers=headers)
link=json.load(r.text)['id']
返回链接
def重复检查(id):
找到=0
打开('posted_posts.txt','r')作为文件:
对于文件中的行:
如果id在行中:
发现=1
返回发现
def将_id_添加到_文件(id):
打开('posted_posts.txt','a')作为文件:
file.write(str(id)+“\n”)
def main():
subreddit=设置\连接\ reddit('showerthinks')
post_dict,post_id=tweet_创建者(subreddit)
tweeter(post_dict,post_id)
def高音扬声器(后置命令、后置ID):
auth=tweepy.OAuthHandler(使用者密钥,使用者密钥)
授权设置\u访问\u令牌(访问\u令牌,访问\u令牌\u密钥)
api=tweepy.api(auth)
对于邮政,邮政编码中的邮政编码(邮政编码,邮政编码):
发现=重复检查(post\U id)
如果找到==0:
打印“[计算机]在twitter上发布此链接”
打印post+“”+post_dict[post]+“#Python#reddit#计算机”
api.update_status(post+“”+post_dict[post]+“#Python#reddit#Computer”)
将\u id\u添加到\u文件(post\u id)
时间。睡眠(30)
其他:
打印“[计算机]已发布”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
我遇到了一些类似的问题,但我不确定这是否是同一个问题。
从Praw3.0开始,Redditor类使用了lazyload特性,该特性用于Praw2.x中的Subreddit类。
你可以用
断言(redditor.has_获取)
检查对象是否已加载
特定于Redditor类,“id”和“name”都是lazyload属性,对于其他一些属性(如“link_karma”)也是一样的。我直接在前面查询它们,如下所示:
vars(redditor)['id']
它适用于Praw2.x,并报告了Praw3.0的一个错误
我的解决方案是呼叫:
redditor.link\u业力
加载所有功能。看看这个[问题][1][1]:我试图让它与我的问题相关,但做得不太好。将继续,谢谢。在解析的json(
json.loads(r.text).keys()
)中有哪些键可用?@XiongChiamiovTraceback(最后一次调用):文件“C:\Python34\reddit\u bot.py”,第82行,在main()文件“C:\Python34\reddit\u bot.py”,第64行,在主推特(post\dict,post\id)文件“C:\Python34\reddit\bot.py”中,第74行,在tweeter打印中(post+“”+post_dict[post]+“#Python#reddit#Computer”)类型错误:无法将“dict_keys”对象隐式转换为str
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()