Thursday, July 24, 2014

Creating dictionary from a Twitter account.

Hi there.
Today will write a simple code in python that will scrape unique words from a Twitter account. For creating a targeted dictionary.

First of all, will find some twitter account:

Next, we need to find out, which field we need to get (with inspect tool):

You can see 'class' named 'ProfileTweet-text js-tweet-text u-dir'. That is what we need.

Let's start coding python:
 import requests
 from bs4 import BeautifulSoup

 r = requests.get('')
 soup = BeautifulSoup(r.content)  #raw html data

The URL is loaded, and passed to BeautifulSoup.

Creating the dictionary:
 a = []
 for i in soup.find_all('p',{'class':'ProfileTweet-text js-tweet-text u-dir'}):
    a += i.text.encode('ascii','ignore').split()

 a = set(a)
 print a
 $ set(['sometimes,', 'saying', 'all', 'ever.', 'background', 'Mumford.'...etc])

That's all. Thank you.

1 comment: