'str'对象没有属性'push_link'

时间:2016-07-22 23:31:43

标签: python string python-2.7

我知道这似乎是一个常见的问题,但我似乎无法解决这个问题。这是代码:

def init():
    global pushbullet_client, wanted_pokemon
    # load pushbullet key
    with open('config.json') as data_file:
        data = json.load(data_file)
        # get list of pokemon to send notifications for
        wanted_pokemon = _str( data["notify"] ) . split(",")
        # transform to lowercase
        wanted_pokemon = [a.lower() for a in wanted_pokemon]
        # get api key
        api_key = _str( data["pushbullet"] )
        if api_key:
            pushbullet_client = api_key


# Safely parse incoming strings to unicode
def _str(s):
  return s.encode('utf-8').strip()

# Notify user for discovered Pokemon
def pokemon_found(pokemon):
    # get name
    pokename = _str(pokemon["name"]).lower()
    # check array
    if not pushbullet_client or not pokename in wanted_pokemon: return
    # notify
    print "[+] Notifier found pokemon:", pokename
    # Locate pokemon on Google Maps
    google_maps_link = "http://maps.google.com/maps?q=" + str(pokemon["lat"]) + "," + str(pokemon["lng"]) + ',20z'
    notification_text = "Pokemon Finder found " + _str(pokemon["name"]) + "!"
    disappear_time = str(datetime.fromtimestamp(pokemon["disappear_time"]).strftime("%I:%M%p").lstrip('0'))+")"
    location_text = "Locate on Google Maps : " + google_maps_link + ". " + _str(pokemon["name"]) + "will be available until" + disappear_time + "."

    push = pushbullet_client.push_link(notification_text, google_maps_link, body=location_text)`

我得到'str'对象没有属性'push_link'错误。我该怎么办?

1 个答案:

答案 0 :(得分:1)

你试图调用我认为是特定类的函数,但是在字符串上。

api_key = _str( data["pushbullet"] )
if api_key:
    pushbullet_client = api_key

pushbullet_client已为其分配了api_key,这是一个字符串。默认的字符串对象没有push_link属性,因此它会抛出该错误。