Skip to content

Commit d1786de

Browse files
Social Media data - Twitter use case
1 parent f951ff7 commit d1786de

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Unstructured_SocialMedia_Twitter.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {
20+
"id": "view-in-github",
21+
"colab_type": "text"
22+
},
23+
"source": [
24+
"<a href=\"https://colab.research.google.com/github/nathalyAlarconT/mod_datasources/blob/master/Unstructured_SocialMedia_Twitter.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {
30+
"id": "HJtCq5TYaEFE",
31+
"colab_type": "text"
32+
},
33+
"source": [
34+
"\n",
35+
"Inicialmente, debes recabar el token de tu cuenta de twitter, en la sección de aplicaciones:\n",
36+
"\n",
37+
"https://developer.twitter.com/\n",
38+
"\n"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"metadata": {
44+
"id": "kgOfWD5eLq1Y",
45+
"colab_type": "code",
46+
"colab": {}
47+
},
48+
"source": [
49+
"import tweepy\n",
50+
"import csv\n",
51+
"# import pandas as pd\n",
52+
"####input your credentials here\n",
53+
"\n",
54+
"consumer_key = 'WvOxM9QWOGUjdw5YzfsnuDaRc'\n",
55+
"consumer_secret = '6HPusgb4zGMbQkJ2NtC3wdVRuZytyQvZuQfJiZhbcSxYkN2NR0'\n",
56+
"access_token = ''\n",
57+
"access_token_secret = ''\n",
58+
"\n",
59+
"auth = tweepy.OAuthHandler(consumer_key, consumer_secret)\n",
60+
"auth.set_access_token(access_token, access_token_secret)\n",
61+
"api = tweepy.API(auth,wait_on_rate_limit=True)"
62+
],
63+
"execution_count": null,
64+
"outputs": []
65+
},
66+
{
67+
"cell_type": "code",
68+
"metadata": {
69+
"id": "BDj4Cft_L1EM",
70+
"colab_type": "code",
71+
"colab": {}
72+
},
73+
"source": [
74+
"# Vamos a guardar los tweets en un archivo .csv\n",
75+
"csvFile = open('tweets.csv', 'a+')\n",
76+
"#Usamos csv Writer\n",
77+
"csvWriter = csv.writer(csvFile)\n",
78+
"\n",
79+
"# Configuramos los términos de nuestra búsqueda.\n",
80+
"texto_a_buscar = '@elonmusk'\n",
81+
"cantidad_resultados = 10\n",
82+
"fechas_inicial = \"2020-02-01\"\n",
83+
"idioma = \"es\"\n",
84+
"\n",
85+
"\n",
86+
"i = 0\n",
87+
"for tweet in tweepy.Cursor(api.search,q=texto_a_buscar,\n",
88+
" lang=idioma,\n",
89+
" since=fechas_inicial).items(cantidad_resultados):\n",
90+
" i = i + 1\n",
91+
" print(\"\\n \", i,\".------------------------------------------------\")\n",
92+
" print (tweet.created_at, '[', tweet.user.screen_name, ']', '\\n', tweet.text)\n",
93+
"\n",
94+
" # Vamos a guardar el resultado de los tuits obtenidos en un archivo\n",
95+
" csvWriter.writerow([tweet.created_at,tweet.user.screen_name, tweet.text.encode('utf-8')])\n",
96+
"csvFile.close()"
97+
],
98+
"execution_count": null,
99+
"outputs": []
100+
},
101+
{
102+
"cell_type": "code",
103+
"metadata": {
104+
"id": "gGW7vcMD3yvp",
105+
"colab_type": "code",
106+
"colab": {}
107+
},
108+
"source": [
109+
"!head tweets.csv"
110+
],
111+
"execution_count": null,
112+
"outputs": []
113+
},
114+
{
115+
"cell_type": "code",
116+
"metadata": {
117+
"id": "vu0-7wuvMGm-",
118+
"colab_type": "code",
119+
"colab": {}
120+
},
121+
"source": [
122+
"# Analizamos ahora el contenido de un tuit\n",
123+
"\n",
124+
"import json\n",
125+
"# convert to string\n",
126+
"json_str = json.dumps(tweet._json)\n",
127+
"\n",
128+
"#deserialise string into python object\n",
129+
"parsed = json.loads(json_str)\n",
130+
"\n",
131+
"print(json.dumps(parsed, indent=4, sort_keys=True))"
132+
],
133+
"execution_count": null,
134+
"outputs": []
135+
}
136+
]
137+
}

0 commit comments

Comments
 (0)