如何使用tweeepy . paginator从tweets中获取twitter用户和位置数据 &g



我使用tweepy从tweet获取数据。我设法得到了主要的推文项目,比如'id', 'author_id'。然而,我很难从user_fieldsplace_fields中获得用户和放置物品。

我期望tweet.includes["users"]tweet.includes["places"]给我访问这些项目的权限,但是唉。

注意,这个例子是保守的:它一次请求10条tweet,乘以2,结果总共限制为20条tweet。

import tweepy
import time
client = tweepy.Client("MYSECRETTOKEN")
# query to search for tweets
query = '"Matariki" lang:en'  
start_time = "2022-06-23T22:00:00.000Z"  
end_time =   "2022-06-26T22:00:00.000Z"
paginator = tweepy.Paginator(client.search_all_tweets,
query=query,
start_time=start_time,
end_time=end_time,
tweet_fields=['id', 'author_id',  "created_at", "text", "source", 'lang', 'in_reply_to_user_id', 'conversation_id', 'public_metrics', 'referenced_tweets', 'reply_settings'],
user_fields=["name", "username", "location", "verified", "description", "created_at"],
place_fields=['full_name', 'id', 'country', 'country_code', 'geo', 'name', 'place_type'],
expansions='author_id,geo.place_id',
max_results=10)
#%%
# Loop the paginator, and throttle the request to not offend Twitter
tel = 0
for tweet in paginator.flatten(limit=20): # Total number of tweets to retrieve
tel +=1
print(tweet.text, tel, 'n')
if tel%10 == 0:
time.sleep(1)

tweepy.Paginator.flatten返回Tweet对象,这些对象没有附加includes数据。

相反,您想要读取tweepy.Paginator直接返回的响应,其中将包括data字段和Tweet对象列表,以及metaincludes等附加字段。

# coding: utf-8
import os
import tweepy
if __name__ == "__main__":
twitter_token = os.environ["TWITTER_TOKEN"]
twitter = tweepy.Client(
bearer_token=twitter_token,
wait_on_rate_limit=True)
query = '"Matariki" lang: en'
start_time = "2022-06-23T22:00:00.000Z"
end_time = "2022-06-26T22:00:00.000Z"
# Most of us don't have access to the 'search_all_tweets'
# endpoint, so I switched to the 'search_recent_tweets'
# for this example
paginator = tweepy.Paginator(
twitter.search_recent_tweets,
query=query,
start_time=start_time,
end_time=end_time,
tweet_fields=["id", "author_id",  "created_at", "text", "source", "lang", "in_reply_to_user_id", "conversation_id", "public_metrics", "referenced_tweets", "reply_settings"],
user_fields=["name", "username", "location", "verified", "description", "created_at"],
place_fields=["full_name", "id", "country", "country_code", "geo", "name", "place_type"],
expansions="geo.place_id,author_id",
max_results=10,
limit=10
)
for rsp in paginator:
print(f"Included Places: {rsp.includes.get('places')}")
print(f"Included Users: {rsp.includes.get('users')}")
print("-" * 80)

结果是这样的…

Included Places: [<Place id=07d9dd109e080000 full_name=Te Wharewaka o Poneke>]
Included Users: [<User id=136324666 name=Steph username=StephJ2731>, <User id=1018242338278256640 name=M T Bashir username=MBashir48666504>, <User id=1103426302537695233 name=Emily username=emilyalexrado>, <User id=55514340 name=Manaiakalani Cluster username=clusternz>, <User id=2671939628 name=Traci Houpapa MNZM JP CFInstD username=TraciHoupapa>, <User id=298390513 name=ZarahnSouthon username=ZarahnSouthon>, <User id=1850936329 name=lynnwoodprice username=lynnwoodprice>, <User id=275843300 name=Kevin Veale username=krveale>, <User id=1495585940034580480 name=Apollo username=apollonz1>]
--------------------------------------------------------------------------------
Included Places: None
Included Users: [<User id=28025094 name=1. Matthew Reweti for Te Whanganui-a-Tara username=poneke_matt>, <User id=18541658 name=Sina Brown-Davis username=uriohau>, <User id=25228534 name=Rangimārie ✌🏽 username=Amonish>, <User id=79055874 name=AklBotanicGrdns username=AklBotanicGrdns>, <User id=21252945 name=Tara username=tarasutherland>, <User id=138197586 name=Flawed by design username=RiversAwarau>, <User id=1100256089830961152 name=Hobsonville Point Public Transport User username=PointUser>, <User id=286644799 name=Sober Lady username=soberlady1998>, <User id=2831083298 name=Whānau Ora Commissioning Agency username=WhanauOraAgency>]
--------------------------------------------------------------------------------

相关内容

  • 没有找到相关文章

最新更新