-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterScraper.py
34 lines (26 loc) · 1.12 KB
/
TwitterScraper.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
import pandas as pd
import snscrape.modules.twitter as sntwitter
import itertools
import numpy as np
tweets_list2 = []
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('#Gillette').get_items()):
if i > 10:
break
tweets_list2.append([tweet.date, tweet.id, tweet.content, tweet.user.username])
tweets_df2 = pd.DataFrame(tweets_list2, columns=['Datetime', 'Tweet Id', 'Text', 'Username'])
# import csv
# import subprocess
# import json
# # search query
# query="#Gillette"
# # scrape tweets using snscrape
# command = f'snscrape --jsonl --max-results 1000 twitter-search "{query}" >2018_tweets_windies.json'
# subprocess.call(command, shell=True)
# # save tweets in CSV file
# with open("2018_tweets_windies.csv", "w", newline="", encoding="utf-8") as csvfile:
# writer = csv.writer(csvfile)
# writer.writerow(["Date","Username","Tweet"])
# with open("2018_tweets_windies.json", "r", encoding="utf-8") as jsonfile:
# for line in jsonfile:
# tweet = json.loads(line)
# writer.writerow([tweet["date"],tweet["username"],tweet["content"],])