Skip to content

Instantly share code, notes, and snippets.

@ivanidris
Created March 12, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanidris/101bc5c280f54cbcaed5 to your computer and use it in GitHub Desktop.
Save ivanidris/101bc5c280f54cbcaed5 to your computer and use it in GitHub Desktop.
Twitter Search Client
from nltk.twitter import Query
from nltk.twitter import credsfromfile
import pandas as pd
def hrefs_from_text(str):
res = str
for s in str.split():
if s.startswith('http'):
res = str.replace(s, '<a href=\"{0}\">{0}</a>'.format(s))
return res
oauth = credsfromfile()
client = Query(**oauth)
with open('twitter.html', 'w') as html:
html.write('<html><body>')
df = pd.read_csv('keywords.csv')
df = df[df['Flag'] == 'Use']
li_html = '<li>name={0} created={1} favorited={2} retweeted={3} {4}</li>'
for term in df['Term']:
query = 'python {}'.format(term)
html.write('<h1>{}</h1>'.format(query))
html.write('<ol>')
tweets = client.search_tweets(keywords=query + ' http -RT',
lang='en', limit=5)
for t in tweets:
text = t['text']
uname = t['user']['name']
html.write(li_html.format(uname, t['created_at'],
t['favorite_count'],
t['retweet_count'],
hrefs_from_text(text)))
html.write('</ol>')
html.write('</body></html>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment