Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 转换为字符串时未生成所需值_Python - Fatal编程技术网

Python 转换为字符串时未生成所需值

Python 转换为字符串时未生成所需值,python,Python,我有一些代码正在使用“gdshortener”模块生成源URL的缩短版本: import simplejson import httplib2 import twitter import gdshortener from random import randint print("Python will now attempt to submit tweets to twitter...") try: api = twitter.Api(consumer_key='',

我有一些代码正在使用“gdshortener”模块生成源URL的缩短版本:

import simplejson
import httplib2
import twitter
import gdshortener
from random import randint

print("Python will now attempt to submit tweets to twitter...")


try:

    api = twitter.Api(consumer_key='',
                      consumer_secret='',
                      access_token_key='',
                      access_token_secret='')

    b = 0

    for a in range(0, 1): #only range 0-1 for this question, actually 1-21
        b = b + 1
        a = randint(1,60000000)
        randint
        print ("a = ", a)
        aa = str(a)


        s1 = gdshortener.ISGDShortener()
        print s1.shorten(url = 'http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html', custom_url = aa)
        ss1 = str(s1)

        status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + ss1 + " #propellerhead #synapse")



    print("Tweets submitted successfully!")

except Exception,e:
    print str(e)    
    print("Twitter submissions have failed!!!")
我正在使用一个随机数生成器生成六位数,然后将这些数字输入到该模块的自定义url参数。这很好,我得到了一系列伪随机数。然而,当我尝试连接我的tweet字符串、我的动态短URL和一些hashtags时,我得到一个错误,我无法连接字符串和整数值

然后我创建了变量'ss1',它是's1'的字符串,但是现在它会生成如下tweet:

The new Synapse Antidote Rack Extension:<gdshortener.gdshortener.ISGDShortener object at 0x000000000542AA20> #propellerhead #synapse

谢谢

检查了模块,发现它返回了一个元组。请参阅以下内容以提取正确的URL

代码:

import gdshortener

s1 = gdshortener.ISGDShortener()
x1 = s1.shorten(url='http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html')[0]
print x1
http://is.gd/KKxmFd
[Finished in 0.8s]
结果:

import gdshortener

s1 = gdshortener.ISGDShortener()
x1 = s1.shorten(url='http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html')[0]
print x1
http://is.gd/KKxmFd
[Finished in 0.8s]
请注意,我是如何在
shorten
的末尾添加了一个
[0]
。这是因为
shorten
返回一个元组,我们可以像列表一样索引它


希望这能有所帮助。

ISGDShortener()方法不会返回可以简单转换为字符串的对象。您可以查看API文档或键入
dir(s1)
查看当前使用的对象的属性/方法。此外,您(用户3045351)试图打印对象
s1
,而不是
shorten
方法的结果。@Human是对的。这有点像打印函数或生成器本身,而不是打印结果。:)@南希:是的,我觉得我的编码方式有点不对劲。谢谢你的帮助。我现在唯一的问题是,“”链接格式触发了来自twitter的警告,上面的扩展通常用于垃圾邮件或网络钓鱼活动。到目前为止,我还没有找到一个标准的url缩短器,尽管它提供了一个通过api定制url的选项。