when I start my code, it doesn't return any results, any idea what could be going wrong? I have it searching a yaml file with around 15~ keywords, and if it finds profiles with these keywords, it will notify me of their username bio.
my yaml file:
keywords:
- Free|free Mint|mint
- Private|private Discord|discord
- Discord|discord Invite|invite Only|only
- Coming|coming soon|Soon
- DM|dm|Dm for|For collaboration|Collaboration
- DM|dm|Dm for|For
- Discord|discord closed
- No|no Discord|discord
- No|no Roadmap|roadmap
- Mint|mint date|Date
- Founded|founded by|By
- Created|created by|By
- Minting|minting
- Supply|supply
I have also tried adding simple words to the yaml file, just to try to get some type of results.
# define authentication handler
auth_handler = tweepy.OAuth1UserHandler(consumer_key,consumer_secret)
# Set access tokens
auth_handler.set_access_token(access_token,access_token_secret)
# Creating the API
api = tweepy.API(auth_handler,wait_on_rate_limit=True)
# Set the path to the YAML file
yaml_path = 'C:\\Users\\CF\\Desktop\\New tweepy testing\\keywords.yaml'
def get_keywords(yaml_path):
"""
Reads keywords from a YAML file and returns them as a list.
Parameters:
- yaml_path: The path to the YAML file
Returns:
- A list of keywords
"""
# Open the YAML file
with open(yaml_path, 'r') as f:
# Parse the YAML file
data = yaml.safe_load(f)
# Retrieve the list of keywords from the YAML file
keywords = data['keywords']
# Return the keywords
return keywords
# Get the keywords from the YAML file
keywords = get_keywords(yaml_path)
# Set the starting page number
page = 1
# Set the maximum number of pages to retrieve
max_pages = 100
# Set a flag to indicate when to stop paginating
done = False
# Iterate through the pages of search results
while not done:
# Search for profiles using the 'users/search' endpoint
results = api.search_users(q=keywords, page=page)
# Iterate through the search results and print the screen name and biography of each profile
if results:
for user in results:
# Encode the screen name and biography in UTF-8 encoding
screen_name = user.screen_name.encode('utf-8')
biography = user.description.encode('utf-8')
# Print the screen name and biography
print(f'Screen name: {screen_name}')
print(f'Biography: {biography}')
else:
print('No results found.')
CodePudding user response:
You are passing the entire keywords list to q=
in your API call. Change how you iterate the keywords.
# old
while not done:
# Search for profiles using the 'users/search' endpoint
results = api.search_users(q=keywords, page=page)
# new
for word in keywords:
results = api.search_users(q=word, page=page)