Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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 试图限制程序返回的tweet数量_Python_Twitter - Fatal编程技术网

Python 试图限制程序返回的tweet数量

Python 试图限制程序返回的tweet数量,python,twitter,Python,Twitter,我使用的代码是这样的 import oauth2 as oauth import urllib2 as urllib # See assignment1.html instructions or README for how to get these credentials api_key = "" api_secret = "" access_token_key = "" access_token_secret = "" _debug = 0 oauth_token = oaut

我使用的代码是这样的

import oauth2 as oauth
import urllib2 as urllib

# See assignment1.html instructions or README for how to get these credentials

api_key = ""
api_secret = ""
access_token_key = ""
access_token_secret = ""

_debug = 0

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()

http_method = "GET"


http_handler  = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)

Construct, sign, and open a twitter request
using the hard-coded credentials above.
'''
def twitterreq(url, method, parameters):
  req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                             token=oauth_token,
                                             http_method=http_method,
                                             http_url=url,
                                             parameters=parameters)

  req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)

  headers = req.to_header()

  if http_method == "POST":
    encoded_post_data = req.to_postdata()
  else:
    encoded_post_data = None
    url = req.to_url()

  opener = urllib.OpenerDirector()
  opener.add_handler(http_handler)
  opener.add_handler(https_handler)

  response = opener.open(url, encoded_post_data)

  return response

def fetchsamples():
  url = "https://stream.twitter.com/1.1/statuses/sample.json?"
  parameters = ['en']
  response = twitterreq(url, "GET", parameters)
  for line in response:
    print (line.strip())

if __name__ == '__main__':
  fetchsamples()

所以这只是源源不断的推特。我想把这个限制在100。我尝试将?count=100添加到url,但status/sample.json不允许使用count参数。任何帮助都将不胜感激。谢谢

看来我的评论对你有用,这是一个答案

替换这个

for line in response:
用这个

for line in response[:100]:

分享你的令牌和秘密有点不明智。我把它们删掉了。是的,但是:。您应该通过Twitter重置/重新创建它们。为什么不在100次迭代后中断响应行的
循环:
或响应行的
循环[:100]: