Tweet scraping, writing and sentiment analysis using tweepy and textblob in python - Python for Data Analytics

Python Programs | Python Tricks | Solution for problems | Data Cleaning | Data Science

Tweet scraping, writing and sentiment analysis using tweepy and textblob in python


Tweepy is open-sourced, hosted on GitHub and enables Python to communicate with Twitter platform and use its API. Tweepy supports OAuth authentication. Authentication is handled by the tweepy.AuthHandler class.

TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

TextBlob's polarity attribute tells the attitude of phrase on the scale [-1.0 to 1.0] where -1.0 stands for negative and 1.0 stands for positive.
TextBlob's subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

Here is simple way how to access twitter data and write it into CSV file  and perform sentiment analysis using textblob library.


import csv
import tweepy
from textblob import TextBlob

#consumer key and consumer secret key you get from twitter when you create
# a new app at http://apps.twitter.com
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_skey = 'xxxxxxxxxxxxxxxxxxxxxxx'

#access token and access secret key you get from twitter when you create 
#a new app at http://apps.twitter.com
access_token = '54xxxxx-KJxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_sk = 'VNxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxo'

To get your own consumer key, consumer secret key, access token and access secret key please goto:
  • http://apps.twitter.com login using your own twitter credential
  • Create a new app button there and enter app name, app description and check the I agree hit submit you'll get the necessary details after successful submission
  • Remeber when giving app name at twitter dec page never include tweet word and it also demand for app name to be unique just like twitter userid

#creating OAuthHandler by passing consumer key and consumer secret key
auth = tweepy.OAuthHandler(consumer_key,consumer_skey)
auth.set_access_token(access_token,access_token_sk)

#passing the auth to tweepy API which provide gateway to tweets
api = tweepy.API(auth)

#opening a csv file
csvFile = open('result.csv', 'a')
csvWriter = csv.writer(csvFile)

#receiving keyword you want to search for
public_tweets = api.search(input("Topic you want to analyse for: "))

#running a for loop to iterate over tweets and printing one row at a time
for tweet in public_tweets:
 #write in a csv file
 csvWriter.writerow([tweet.created_at,tweet.text.encode('utf-8')])
 print(tweet.text)
 analysis = TextBlob(tweet.text)
 print(analysis.sentiment)


which will generate a fantastic csv file like this.


7 comments:


  1. The blog is so interactive and Informative , you should write more blogs like this Data Science Online course

    ReplyDelete
  2. thank you for sharing post.
    web programming tutorial
    welookups

    ReplyDelete
  3. I like your blog, I read this blog please update more content on python, further check it once at python online training

    ReplyDelete
  4. Scraping data from websites holds endless advantages for businesses. Hiring professionals to scrape data from website can offer value addition to multiple industry verticals in a multitude of ways.
    scraper bot

    data extraction services

    web crawling services

    web scraping services

    ReplyDelete
  5. am getting this error on saving csv
    NameError Traceback (most recent call last)
    in
    1 #opening a csv file
    2 csvFile = open('result.csv', 'a')
    ----> 3 csvWriter = csv.writer(csvFile)
    4
    5 #receiving keyword you want to search for

    NameError: name 'csv' is not defined

    how can i deal with it

    ReplyDelete