Regex, Python & Twitter

Post on 05-Dec-2014

2.302 views 3 download

description

Using regular expressions and python to search twitter like a boss

Transcript of Regex, Python & Twitter

WHAT is REGEX

Regular Expressions

A special text string thatdescribes a search pattern

EXAMPLES

Find email address(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

✔ The Official Standard: RFC 2822

EXAMPLES

Find email address easier

\w[\w._]+@[\w.]+[.]\w+{2,4}

EXAMPLES

Find email address easier“My email address is: stelian@firez.be”

\w[\w._]+@[\w.]+[.]\w+{2,4}

\w[\w._]+@[\w.]+[.]\w+{2,4}“My email address is: stelian@firez.be”

\w[\w._]+@[\w.]+[.]\w+{2,4}

“My email address is: stelian@firez.be”

“My email address is: stelian@firez.be”

EXAMPLES

Find email address easier“My email address is: stelian@firez.be”

\w[\w._]+@[\w.]+[.]\w+{2,4}

\w[\w._]+@[\w.]+[.]\w+{2,4}

\w[\w._]+@[\w.]+[.]\w+{2,4}

“My email address is: stelian@firez.be”

“My email address is: stelian@firez.be”

“My email address is: stelian@firez.be”

WHAT is PYTHON

Life is short, use Python

WHO uses PYTHON

Google

Blender 3D

Civilization 4

WHY PYTHON

Lots of modules

Easy to read syntax

API wrappers

Great community

PYTHON does

click picture to open video

SEARCH TWITTERAPI limitations

150 anonymous calls per hour

350 authenticated calls per hour

results from only the past 7 days

max 1500 results

SEARCH TWITTERRequirements

minimum Python knowledge

Twitter API wrapper (twython)

EXAMPLES

Designers in Antwerpenfrom twython import Twythontwitter = Twython()

search_results = twitter.searchTwitter(q="design", since_id=0, rpp='100', geocode="51.21992,4.39625,10km", page=1)

for tweet in search_results['results']): print tweet[‘id’] print tweet[‘created_at’] print tweet[‘from_user’] print tweet[‘profile_image_url’] print tweet[‘text’]

12

3

456789

10

EXAMPLES

Designers in Antwerpenfrom twython import Twythontwitter = Twython()

user_info = twitter.getUserTimeline(id=tweet['from_user'])[0]['user']

print user_info['name']print user_info['time_zone']print user_info['location']print user_info['description']print user_info['url']

12

3

45678