Twitter 有没有可能知道某条推特来自哪个国家?

Twitter 有没有可能知道某条推特来自哪个国家?,twitter,Twitter,我正在解决一个问题,这要求我知道推特发端的国家。我不需要更小的粒度。国家也可以。我已经有推特了。有可能从twitterapi获取这些信息吗 Twitter API提到: 地点:当存在时,表示tweet是关联的,但不一定来自某个地点 坐标:表示用户或客户端应用程序报告的此推文的地理位置。内部坐标数组的格式为:首先是经度,然后是纬度 一些搜索表明,地点可能不是实现这一目标的最佳方式。我应该用坐标来代替吗?它们准确吗?使用它有什么好处吗?我使用locations参数从边界框中获取tweet。这将返回普

我正在解决一个问题,这要求我知道推特发端的国家。我不需要更小的粒度。国家也可以。我已经有推特了。有可能从twitterapi获取这些信息吗

Twitter API提到:

地点:当存在时,表示tweet是关联的,但不一定来自某个地点

坐标:表示用户或客户端应用程序报告的此推文的地理位置。内部坐标数组的格式为:首先是经度,然后是纬度

一些搜索表明,地点可能不是实现这一目标的最佳方式。我应该用坐标来代替吗?它们准确吗?使用它有什么好处吗?

我使用locations参数从边界框中获取tweet。这将返回普通推文和地理标记推文

请注意,如果坐标字段已填充或坐标为空但位置已填充,则流将返回tweet。如果坐标字段不为空,则它表示推文起源于地理标记推文的地球表面上的确切位置。如果坐标字段为空,但显示了地点字段,则将显示边界框/多边形的坐标,该边界框/多边形代表用户在从博物馆到城市/国家的普通推文中标记的地点。Twitter还能够从IP地址检索一些位置信息,尽管粒度很低,比如城市级别。详情请参阅

编辑:当您使用流式API将一组tweet拉入.txt文件时,您可以使用以下代码。我使用了一个名为Tweet Parser的Python包

import pandas as pd
from tweet_parser.tweet import Tweet
from tweet_parser.tweet_parser_errors import NotATweetError
import fileinput
import json

#remove all blank lines
with open('test.txt') as infile, open('test.json', 'w') as outfile:
    for line in infile:
        if not line.strip(): continue  # skip the empty line
        outfile.write(line)  # non-empty line. Write it to output

df  = pd.DataFrame(columns=['DateTime','user_id','country','tweet'])
for line in fileinput.FileInput("test.json"):
    try:
        tweet_dict = json.loads(line)
        tweet = Tweet(tweet_dict)
    except (json.JSONDecodeError,NotATweetError):
        pass
    df= df.append({'DateTime':tweet.created_at_datetime,'user_id':tweet.user_id,'country':tweet_dict['place']['country'],'tweet':tweet.text},ignore_index=True)

Tweet JSON对象中是否包含任何类型的位置或坐标数据?如果没有,就没有办法得出这个结论,有些人确实是这样。但通过粗略的目视检查,我可以看到大多数设置为空。是因为这些推文没有地理标记吗?是的,这就是原因。谢谢你的回答,但这不是我想要的。我已经有推特了。在这些推文中,我需要过滤掉来自某个特定国家的推文。你认为你可以分享一些信息吗。如果你可以的话?对不起,我没能解决这个问题。我正在根据我检索到的一组推文编辑我的答案。希望这就足够了。
import pandas as pd
from tweet_parser.tweet import Tweet
from tweet_parser.tweet_parser_errors import NotATweetError
import fileinput
import json

#remove all blank lines
with open('test.txt') as infile, open('test.json', 'w') as outfile:
    for line in infile:
        if not line.strip(): continue  # skip the empty line
        outfile.write(line)  # non-empty line. Write it to output

df  = pd.DataFrame(columns=['DateTime','user_id','country','tweet'])
for line in fileinput.FileInput("test.json"):
    try:
        tweet_dict = json.loads(line)
        tweet = Tweet(tweet_dict)
    except (json.JSONDecodeError,NotATweetError):
        pass
    df= df.append({'DateTime':tweet.created_at_datetime,'user_id':tweet.user_id,'country':tweet_dict['place']['country'],'tweet':tweet.text},ignore_index=True)