Anleitung -> http://www.cs.york.ac.uk/semeval-2013/t ... readme.txt
Also Python 3.3 installiert und nach kurzem googlen dann auch beautifulsoup4 runtergeladen und per CMD installiert unter Windows.
Im Grunde also nur ein einfaches
Nachdem mir erstmal überall nur Fehler entgegenkamen hab ich gegoogelt und festgestellt, dass die Formatierung komplett für'n Arsch war, also erstmal alles per Hand korrigiert - siehe Code unten.easy_install beautifulsoup4
python download_tweets.py input_file > output_file
Nun wirft mir ein "python tweets.py" aber immernoch einen Syntax-Fehler bei Zeile 61 aus
Code: Alles auswählen
D:\>python tweets.py input_file > tweeter
File "tweets.py", line 61
print "\t".join(fields + [text]).encode('utf-8')
^
SyntaxError: invalid syntax
Kompletter und bereinigter Code:
Code: Alles auswählen
#!/usr/bin/python
import sys
import urllib
import re
import json
from bs4 import BeautifulSoup
import socket
socket.setdefaulttimeout(10)
cache = {}
for line in open(sys.argv[1]):
fields = line.rstrip('\n').split('\t')
sid = fields[0]
uid = fields[1]
#url = 'http://twitter.com/%s/status/%s' % (uid, sid)
#print url
tweet = None
text = "Not Available"
if cache.has_key(sid):
text = cache[sid]
else:
try:
f = urllib.urlopen("http://twitter.com/%s/status/%s" % (uid, sid))
#Thanks to Arturo!
html = f.read().replace("</html>", "") + "</html>"
soup = BeautifulSoup(html)
jstt = soup.find_all("p", "js-tweet-text")
tweets = list(set([x.get_text() for x in jstt]))
#print len(tweets)
#print tweets
if(len(tweets)) > 1:
continue
text = tweets[0]
cache[sid] = tweets[0]
for j in soup.find_all("input", "json-data", id="init-data"):
js = json.loads(j['value'])
if(js.has_key("embedData")):
tweet = js["embedData"]["status"]
text = js["embedData"]["status"]["text"]
cache[sid] = text
break
except Exception:
continue
if(tweet != None and tweet["id_str"] != sid):
text = "Not Available"
cache[sid] = "Not Available"
text = text.replace('\n', ' ',)
text = re.sub(r'\s+', ' ', text)
#print json.dumps(tweet, indent=2)
print "\t".join(fields + [text]).encode('utf-8')