I am new to python and I am doing a project on fetching tweets. I was able to do so and now I want to get the hashtags from all those tweets. But when I tried to do a for loop in order to append my list, it only captures the last item/values. I've been all over google and even tried chatGPT but I could not seem to get it right.
Here is my code if anyone is kind to please look and see what I am missing.
## trying to get hashtags of tweets fetched.
#Get the tweet text
tweet_text = tweet.full_text
#Use a regex to find all hashtags in the tweet text
hashtags = re.findall(r"#(\w )", tweet_text)
print('items:', hashtags)
# Use the entities attribute to get the hashtags
hashtag_list = []
for word in tweets:
if word == '#':
hashtag_list.append(word)
print('List:', hashtag_list)
I've been everywhere on google trying to find the answer but to no avail. I have been stuck with this problem for over a week now.
CodePudding user response:
As RedzMakersError said, you only check if the word is #
, and only #
.
You should try something like:
if word.startswith('#'):
hashtag_list.append(word)
As the name of the function says, it returns True
if the string starts with #
, False
otherwise.
Official documentation: https://docs.python.org/3/library/stdtypes.html#str.startswith
CodePudding user response:
I think your problem is that with this line:
if word == '#':
you check if the word is only a "#"
You probaly just want to check if the word starts with a hashtag character (as hashtags do). You can do that with the startswith()
function wich checks if a string starts with the given character and returns true
if it does.
So in youre case your code should probably look like this:
if word.startswith("#"):
hashtag_list.append(word)
Here you can read a bit more about startswith()
:
https://www.w3schools.com/python/ref_string_startswith.asp
hope this helps :)