1

I'm very much new to python. I need a code which will fetch the tweets and the places from where it is tagged,or geo-tagged tweets.

I Have code which will export the tweets the csv file based o the hash tag given, I want another column added, with specifies from where it is tweeted.

Please help me on this.

The code is as follows,

import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = '-'
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)


csvFile = open('mydoc.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

for tweet in tweepy.Cursor(api.search,q="#iPhoneX",count=100,
                       lang="en",
                       since="2017-01-01").items():
print (tweet.created_at, tweet.text)
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

I'm in very much in need of it.Please help !!!

laxmi
  • 11
  • 2

1 Answers1

0

I think you can do like this:

for tweet in tweepy.Cursor(api.search, q="#iPhoneX", count=100, lang="en",
                           since="2017-01-01").items():
    if tweet.coordinates or tweet.geo:
        print(tweet.id_str, tweet.coordinates, tweet.geo)

You can see the structure of a tweet object (also called status) here. The different fields correspond to:

  • coordinates: "Represents the geographic location of this Tweet as reported by the user or client application. The inner coordinates array is formatted as geoJSON (longitude first, then latitude)"
  • place: "When present, indicates that the tweet is associated (but not necessarily originating from) a Place". This happens when the user tagged a place for the tweet.
  • geo: "Deprecated. Use the coordinates field instead. This deprecated attribute has its coordinates formatted as [lat, long], while all other Tweet geo is formatted as [long, lat]."
vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • whenever I pass coordinates, I'm getting as None for any of the hashtag. – laxmi Dec 29 '17 at 10:32
  • I modified my answer to get a dictionary. Tell me if it does not work. It is totally possible for these fields to be equal to `None`. It means that the tweet has no `coordinates`, nor `place`, nor `geo` because the user did not want to share his location – vvvvv Dec 29 '17 at 10:36
  • coordinates = tweet_json["coordinates"] TypeError: string indices must be integers – laxmi Dec 29 '17 at 10:49
  • Like would there be a chance to get from where the person is tweeting, irrespective of his geo location tagging. – laxmi Dec 29 '17 at 11:08
  • I changed the code. Take a look, it only prints when there is a `coordinates` or `geo` that is not `None`. It happens rarely but it does happen. These 2 fields represent where the person is tweeting if this person has enabled the geolocalisation. If not, then it's `None` and there is nothing that you can do about it ! – vvvvv Dec 29 '17 at 11:19
  • I got 10 results in 10 seconds of run – vvvvv Dec 29 '17 at 11:25