This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
forked from twintproject/twint
-
Notifications
You must be signed in to change notification settings - Fork 82
/
hao.py
52 lines (45 loc) · 1.37 KB
/
hao.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import snscrape.modules.twitter as sntwitter
import pandas as pd
from datetime import date
def get_tweets_from_user(user: str = "haoel", n_tweets: int = 100000) -> pd.DataFrame:
# To Run: get_tweets_from_user('john', n_tweets=15)
query = "from:" + str(user)
# Created a list to append all tweet attributes(data)
attributes_container = []
# Using TwitterSearchScraper to scrape data and append tweets to list
for i, tweet in enumerate(sntwitter.TwitterSearchScraper(query).get_items()):
if i > n_tweets:
break
attributes_container.append(
[
tweet.id,
tweet.date,
tweet.likeCount,
tweet.retweetCount,
tweet.replyCount,
tweet.sourceLabel,
tweet.content,
tweet.links,
str(tweet),
]
)
# Creating a dataframe from the tweets list above
tweets_df = pd.DataFrame(
attributes_container,
columns=[
"id",
"Date Created",
"LikesCount",
"retweetCount",
"replyCount",
"Source of Tweet",
"Text",
"links",
"url",
],
)
return tweets_df
if __name__ == "__main__":
df = get_tweets_from_user()
print(df)
df.to_csv("haoel.csv")