Issue
This Content is from Stack Overflow. Question asked by Captain Kush
I wrote a code to send follow request to specific users and my logic also seems perfect. Even when I print response.data it shows me my followers’ data in a dictionary. But I can’t find a way to fetch those names altogether and compare with the specific name of the person I want to follow.
import tweepy
import api_keys as ak
def about_me(client: tweepy.Client) -> None:
"""Print information about the client's user."""
# The `public_metrics` addition will give me my followers count, among other things
me = client.get_me(user_fields=["public_metrics"])
print(f"My name: {me.data.name}")
print(f"My handle: @{me.data.username}")
print(f"My followers count: {me.data.public_metrics['followers_count']}")
print(f"My User ID: {me.data.id}")
if __name__ == "__main__":
client = tweepy.Client(
bearer_token=ak.Bearer_Key,
consumer_key=ak.API_Key,
consumer_secret=ak.API_Key_Secret,
access_token=ak.Access_Token,
access_token_secret=ak.Access_Token_Secret)
print("=== About Me ===")
about_me(client)
for response in tweepy.Paginator(client.get_users_followers, 2351371758, max_results=10, limit=5):
df = pd.DataFrame(response.data)
print(df)
if df['name'] == 'Leta Disandro':
response.follow_user()
It even stores my requested data into a dataframe but throws error when I try to check for the name in that dataframe. I even tried different variations and these are the errors I encountered:
raise ValueError(ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
AttributeError: 'Response' object has no attribute 'follow'
AttributeError: 'Response' object has no attribute 'follow_user'
Solution
df = pd.DataFrame(response.data)
erases df
at each iteration of the loop.
if df['name'] == 'Leta Disandro':
is incorrect since you want to check for the 'name'
field of the objects in the dataframe, not from the dataframe object itself.
But do you really need a dataframe anyway?
Finally, follow_user
is a method of Client
objects, not of Response
objects.
This Question was asked in StackOverflow by Captain Kush and Answered by Mickaƫl Martinez It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.