Python Twitter API尝试检索推文但错误:AttributeError:' int'对象没有属性'编码'

时间:2016-10-13 17:57:18

标签: python api twitter

为什么我会收到一个AttributeError:' int'对象没有属性'编码'? 我正在尝试使用Python上的Twitter API检索推文。完全追溯到这里:

Traceback (most recent call last):
  File "C:/Python27/lol.py", line 34, in <module>
    headers = req.to_header()
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 398, in to_header
    params_header = ', '.join(header_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 397, in <genexpr>
    header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 396, in <genexpr>
    stringy_params = ((k, escape(v)) for k, v in oauth_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 163, in escape
    s = s.encode('utf-8')
AttributeError: 'int' object has no attribute 'encode'

以下是我正在使用的代码。

import oauth2
import time
import urllib2
import json
url1="https://api.twitter.com/1.1/search/tweets.json"
params = {
    "oauth_version": "1.9.0",
    "oauth_nonce": oauth2.generate_nonce(),
    "oauth_timestamp": int(time.time())
    }

consumer = oauth2.Consumer(key="*********", secret="*********")
token = oauth2.Token(key="*********", secret="*********")

params["oauth_consumer_key"] = consumer.key
params["oauth_token"] = token.key

for i in range(1):
    url = url1
    req = oauth2.Request(method="GET", url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)
    headers = req.to_url()
    print headers
    print url

for i in range(1):
    url = url1
    params["q"] = "pictorial"
    params["count"] = 2
    req = oauth2.Request(method="GET", url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)
    headers = req.to_header()
    url = req.to_url()
    response = urllib2.Request(url)
    data = json.load(urllib2.urlopen(response))
    if data["statuses"] == []:
        print "end of data"
        break
    else:
        print data

如果我将int(time.time())更改为str(time.time()) 我收到以下错误:

Traceback (most recent call last):
  File "C:/Python27/lol.py", line 37, in <module>
    data = json.load(urllib2.urlopen(response))
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 437, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request

1 个答案:

答案 0 :(得分:0)

anaconda_linting

这里使用false,但该字段必须为字符串。