地理标记推文是指包含地理位置信息的推文,这些信息可以是:
要使用Twitter API仅搜索地理标记的推文,主要有以下几种方法:
import requests
import os
bearer_token = os.environ.get("TWITTER_BEARER_TOKEN")
def search_geo_tweets(query, max_results=10):
url = "https://api.twitter.com/2/tweets/search/recent"
params = {
"query": f"{query} has:geo",
"max_results": max_results,
"tweet.fields": "created_at,geo",
"expansions": "author_id,geo.place_id",
"place.fields": "full_name,id,country,country_code,geo,place_type"
}
headers = {
"Authorization": f"Bearer {bearer_token}",
"User-Agent": "v2RecentSearchPython"
}
response = requests.get(url, headers=headers, params=params)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()
# 示例调用
result = search_geo_tweets("football")
print(result)
关键点:
has:geo
操作符确保只返回带有地理标记的推文tweet.fields
请求geo字段expansions
和place.fields
获取地点详细信息可以组合使用以下操作符:
point_radius:[longitude latitude radius]
- 搜索特定半径内的推文bounding_box:[west_long south_lat east_long north_lat]
- 搜索矩形区域内的推文place:place_id
- 搜索特定地点的推文place_country:country_code
- 搜索特定国家的推文import requests
import os
import json
bearer_token = os.environ.get("TWITTER_BEARER_TOKEN")
def stream_geo_tweets():
url = "https://api.twitter.com/2/tweets/search/stream"
params = {
"tweet.fields": "created_at,geo",
"expansions": "author_id,geo.place_id",
"place.fields": "full_name,id,country,country_code,geo,place_type"
}
headers = {
"Authorization": f"Bearer {bearer_token}",
"User-Agent": "v2FilteredStreamPython"
}
response = requests.get(url, headers=headers, params=params, stream=True)
for response_line in response.iter_lines():
if response_line:
json_response = json.loads(response_line)
print(json.dumps(json_response, indent=4, sort_keys=True))
# 需要先设置规则
def set_stream_rules():
url = "https://api.twitter.com/2/tweets/search/stream/rules"
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-type": "application/json"
}
rule = {
"add": [
{"value": "has:geo", "tag": "geo-tagged-tweets"}
]
}
response = requests.post(url, headers=headers, json=rule)
if response.status_code != 201:
raise Exception(response.status_code, response.text)
return response.json()
# 先设置规则,再开始流式传输
set_stream_rules()
stream_geo_tweets()
可能原因:
解决方案:
Twitter API返回的地理数据可能有几种形式:
解决方案:
geo.coordinates
和place
字段geo.coordinates
数据Twitter API有严格的速率限制:
解决方案:
next_token
处理大量结果通过以上方法,你可以有效地搜索和分析Twitter上的地理标记推文,获取有价值的空间社交媒体数据。