From 410805be704694a740440a3c446022ed96bb4271 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Fri, 2 Nov 2012 11:38:03 +0100 Subject: [PATCH] =?utf8?q?cr=C3=A9e=20le=20script=20de=20scraping?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++-- scrapper.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 scrapper.py diff --git a/README.md b/README.md index fa772a5..255838c 100644 --- 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 index 0000000..abb7b1d --- /dev/null +++ b/scrapper.py @@ -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() -- 2.43.0