-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter_Scraping_Streamlit.py
147 lines (89 loc) · 2.79 KB
/
Twitter_Scraping_Streamlit.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# importing libraries
import streamlit as st
import camelot as cam
import pandas as pd
import json
import subprocess
import os
import base64
import snscrape.modules.twitter as sntwitter
# In[2]:
# Assigning a title for our GUI
st.title("Lets Scrape some Tweets")
#st.subheader("Twitter Scraper")
st.image("./wp4056133-twitter-wallpapers.jpg")
# In[3]:
# Creating a Streamlit form
with st.form(key = 'Twitter Form'):
name = st.text_input(label = 'Please enter your Keyword/ hashtag')
file_name = st.text_input('Name your file')
submit = st.form_submit_button(label = 'Next')
# In[4]:
# Creating columns under Streamlit form
col1, col2 = st.columns(2)
with col1:
with st.form('CSV Download'):
down1 = st.radio('Do you want to Download CSV?', ['Yes', 'No'])
number1 = st.slider(label='How many Tweets to produce', min_value=0, max_value=500, step = 10)
submitted1 = st.form_submit_button('Submit 1')
with col2:
with st.form('JSON Downlaod'):
down2 = st.radio('Do you want to Download JSON?', ['Yes', 'No'])
number2 = st.slider(label='How many Tweets to produce', min_value=0, max_value=500, step = 10)
submitted2 = st.form_submit_button('Submit 2')
# In[5]:
if col1 == 'Yes':
# byte object into a PDF file
with open("input.csv", "wb") as f:
base64_pdf = base64.b64encode(input_csv.read()).decode('utf-8')
f.write(base64.b64decode(base64_csv))
f.close()
else:
print('Try Again')
# In[6]:
if col2 == 'Yes':
# byte object into a PDF file
with open("input.json", "wb") as f:
base64_pdf = base64.b64encode(input_json.read()).decode('utf-8')
f.write(base64.b64decode(base64_json))
f.close()
else:
print('Try Again')
# In[7]:
# Scraping required items from the hashtag
scraper = sntwitter.TwitterSearchScraper('#python')
tweets = []
for i, tweet in enumerate(scraper.get_items()):
data = [
tweet.date,
tweet.id,
tweet.url,
tweet.rawContent,
tweet.user.username,
tweet.replyCount,
tweet.retweetCount,
tweet.lang,
tweet.source,
tweet.likeCount
]
tweets.append(data)
if i > 500:
break
# In[8]:
# Converting the data into a DataFrame and assigning it to a variable
table = pd.DataFrame(tweets, columns = ["date", "id", "url", "rawContent", "username", "replyCount", "retweetCount", "lang", "source", "likeCount",])
# display the output after parsing
st.write(table)
# In[9]:
if len(table) > 0:
output_df = st.dataframe(table)
# In[12]:
# Adding Streamlit Download Button
st.download_button("Download CSV",
"input.csv",
"text.csv",
key = 'download-csv',)
# In[ ]: