From: karim hamidou Date: Fri, 2 Nov 2012 10:38:03 +0000 (+0100) Subject: crée le script de scraping X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=410805be704694a740440a3c446022ed96bb4271;p=Projet_IC05.git crée le script de scraping --- 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()