Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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 如何从某个国家获取过去7天的推文?_Python_Python 3.x_Twitter_Tweepy - Fatal编程技术网

Python 如何从某个国家获取过去7天的推文?

Python 如何从某个国家获取过去7天的推文?,python,python-3.x,twitter,tweepy,Python,Python 3.x,Twitter,Tweepy,我试图从某个国家获得过去一周的推文(到目前为止,7天是我读到的限制),但我似乎没有得到很多结果。这仅仅是因为人们在推特时没有打开他们的地理标签吗?还是我的代码有问题?当我现在运行它的时候,我得到了44条,但我觉得在过去的一周里,肯尼亚有超过44条推特 # -*- coding: utf-8 -*- import tweepy import csv # info CONSUMER_KEY = '...' CONSUMER_SECRET = '...' ACCESS_TOKEN = '...' A

我试图从某个国家获得过去一周的推文(到目前为止,7天是我读到的限制),但我似乎没有得到很多结果。这仅仅是因为人们在推特时没有打开他们的地理标签吗?还是我的代码有问题?当我现在运行它的时候,我得到了44条,但我觉得在过去的一周里,肯尼亚有超过44条推特

# -*- coding: utf-8 -*-
import tweepy
import csv

# info
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
ACCESS_TOKEN = '...'
ACCESS_SECRET = '...'
COUNTRY = 'Kenya'

# get authorization
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)

# get tweets from country
place = api.geo_search(query=COUNTRY, granularity="country")
place_id = place[0].id

# print tweets and save to csv file
with open('tweets.csv', 'w', newline='', encoding='utf-8') as csvFile:
    tweetWriter = csv.writer(csvFile, delimiter=',')
    tweets = api.search(q='place:%s' % place_id, count=100, since='1')
    count = 0
    for tweet in tweets:
        count += 1
        # tweet.id = unique id for tweet, text = text, place.name = where it was posted, created_at = UTC time
        tweetData = [tweet.id, tweet.user.name, tweet.text, tweet.place.name, tweet.created_at]
        tweetWriter.writerow(tweetData)

    print(count)
编辑:
我似乎找不到办法从一个国家获得超过过去20-30分钟的推文,我选择使用tweepy的流,并使用坐标将结果过滤到我国家的一个框中,而不是试图提取过去的数据

顺便说一句,
since=“1”
选项是无用的。我如何确保它将获得过去7天的全部信息,而不是仅仅从过去几分钟中随机选择的信息?根据“Twitter搜索API根据过去7天发布的最新推文样本进行搜索。[…]如果您想匹配完整性,则应该考虑使用流式API。“按时间顺序排序,搜索功能将尽可能多地返回过去七天的Twitter。你不能直接在搜索中指定你希望推文的发布时间,但你得到的44条推文已经足够了。我对推文中的geo知之甚少,也许可以试试类似的东西。谢谢,我觉得奇怪的是,在过去的7天里,所有打开位置的推特都发生在过去15-20分钟左右,根据它们的创建值。即使删除'since=1'也会得到相同的结果