]> khamidou — GitHub mirrors - Projet_IC05.git/commitdiff
crée le script de scraping
authorkarim hamidou <khamidou@gmail.com>
Fri, 2 Nov 2012 10:38:03 +0000 (11:38 +0100)
committerkarim hamidou <khamidou@gmail.com>
Fri, 2 Nov 2012 10:38:03 +0000 (11:38 +0100)
README.md
scrapper.py [new file with mode: 0644]

index fa772a5a161c7ce47a1fc2b63c7e29a2f2fc5c92..255838cfc33b9d9276a6e0760dec2811ba894a5d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,2 +1,5 @@
-Projet_IC05
-===========
\ No newline at end of file
+Projet IC05
+
+Realisation d'une cartographie des tweets liés à l'élection présidentielle
+américaine. La cartographie fera apparaître les liens entre les membres de
+twitter et les sites webs à l'extérieur du réseau social.
diff --git a/scrapper.py b/scrapper.py
new file mode 100644 (file)
index 0000000..abb7b1d
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+#
+# this script get the results of the twitter api for the keywords in 
+# the list "keywords".
+# the results are then saved in a timestamped file.
+#
+# usage: scrapper output_dir
+
+keywords = ["romney", "obama", "gary johnson", "ryan", "ron paul"]
+
+import os
+import sys
+import json
+import urllib, urllib2
+import datetime
+
+def timeStamped(fname, fmt='{fname}%Y-%m-%d-%H-%M-%S.json'):
+        return datetime.datetime.now().strftime(fmt).format(fname=fname)
+
+def getPage(url):
+    req = urllib2.Request(url)
+    response = urllib2.urlopen(req)
+    return response.read()
+
+def runQuery():
+    queryString = ""
+    for keyword in keywords:
+        if keyword != keywords[-1]:
+            queryString += keyword + " OR "
+        else:
+            queryString += keyword
+    url = "http://search.twitter.com/search.json?q=%s&include_entities=1" % queryString
+    url = url.replace(" ", "%20")
+    with open(timeStamped("search_"), 'w') as outf:
+        outf.write(getPage(url))
+
+os.chdir(sys.argv[1])
+runQuery()