首页 > 解决方案 > TypeError: 无法使用 Bit.ly_API 连接 'str' 和 'dict' 对象错误

问题描述

我有点问题。我对python还是很陌生。所以dict对我来说很新。我知道在我去的时候解决问题,并希望从我的错误中吸取教训。

所以错误在下面,第一个文本变量中带有“shortURL”。只是想知道这个问题意味着什么以及我该如何解决它?我在网上查看了一下,并没有完全理解为什么很多问题都在查看字符串但 bit.ly_api 只是返回一个 URL。

谢谢你的帮助 :)

    print("")
    print("Welcome to Sole Retriever Tweet Formulator b0.1")
    type = (raw_input('What kind of tweet would you like to do?' + '\n' + '1. Store URL + Website Site' + '\n' + '2.Website URL Only' + '\n'))
    if type == ('1'):
        store1 = (raw_input('What is the store name hosting the Off-White x Converse Raffle? '))
        storeURL = (raw_input('What is the direct URL to the raffle? '))
        shortURL = b.shorten(storeURL)
        text = ('Woof! ' + store1 + ' raffle is now live for the Off-White x Converse Chuck Taylor!' + '\n' + '\n' + shortURL + '\n' + '\n' + 'For raffle details and where to enter check and filter by "live" -' + '\n' + '\n' + 'https://www.soleretriever.com/off-white-x-converse-chuck-taylor/' + '\n' + '\n' + '#soleretriever #offwhite #converse #sneakers')
        print (text)
        os.system("echo '%s' | pbcopy" % text)
        print ('\n')
        print ('Copied to Clipboard')
        twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        twitter.update_status(status=text)
    if type == ('2'):   
        store = (raw_input('What is the store name hosting the Off-White x Converse Raffle? '))
        text = ('Woof! ' + store + ' raffle is now live for the Off-White x Converse Chuck Taylor!' + '\n' + '\n' + 'For raffle details and where to enter check and filter by "live" -' + '\n' + '\n' + 'https://www.soleretriever.com/off-white-x-converse-chuck-taylor/' + '\n' + '\n' + '#soleretriever #offwhite #converse #sneakers')
        print (text)
        os.system("echo '%s' | pbcopy" % text)
        print ('\n')
        print ('Copied to Clipboard')
        twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        twitter.update_status(status=text)

标签: pythonstringpython-2.7directory

解决方案


调用b.shorten(storeURL)将返回一个dict键/值对,包括url. 当您像在print(). 我建议像:

response = b.shorten(storeURL)
shortURL = response['url']

推荐阅读