Python Tweepy получает все твиты на основе геокода - PullRequest
0 голосов
/ 30 мая 2020

Я пытаюсь получить все твиты в определенном радиусе вокруг заданных координат. Сценарий действительно работает, но возвращается ноль записей. Странно то, что точно такой же код работал у меня несколько дней go, а теперь нет, и я застрял: (

import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import pandas as pd
import tweepy

#Twitter credentials for the app
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_key= 'xxx'
access_secret = 'xxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)

#Create list for column names
COLS = ['id','created_at','lang','original text','user_name', 'place', 'place type', 'bbx', 'coordinates']

geo='48.136353, 11.575004, 25km'

def write_tweets(keyword):

    #create dataframe from defined column list
    df = pd.DataFrame(columns=COLS)

    #iterate through pages with given condition
    #using tweepy.Cursor object with items() method
    for page in tweepy.Cursor(api.search, q=keyword,
                                  include_rts=False,
                                  geocode=geo).pages():

                for tweet in page:
                    #creating string array
                    new_entry = []

                    #storing all JSON data from twitter API
                    tweet = tweet._json    

                    #Append the JSON parsed data to the string list:

                    new_entry += [tweet['id'], tweet['created_at'], tweet['lang'], tweet['text'], 
                                  tweet['user']['name']]

                    #check if place name is available, in case not the entry is named 'no place'
                    try:
                        place = tweet['place']['name']
                    except TypeError:
                        place = 'no place'
                    new_entry.append(place)

                    try:
                        place_type = tweet['place']['place_type']
                    except TypeError:
                        place_type = 'na'
                    new_entry.append(place_type)

                    try:
                        bbx = tweet['place']['bounding_box']['coordinates']
                    except TypeError:
                        bbx = 'na'
                    new_entry.append(bbx)

                    #check if coordinates is available, in case not the entry is named 'no coordinates'
                    try:
                        coord = tweet['coordinates']['coordinates']
                    except TypeError:
                        coord = 'no coordinates'
                    new_entry.append(coord)

                    # wrap up all the data into a data frame
                    single_tweet_df = pd.DataFrame([new_entry], columns=COLS)
                    df = df.append(single_tweet_df, ignore_index=True)

                    #get rid of tweets without a place
                    df_cleaned = df[df.place != 'no place']


    print("tweets with place:")
    print(len(df[df.place != 'no place']))

    print("tweets with coordinates:")
    print(len(df[df.coordinates != 'no coordinates']))

    df_cleaned.to_csv('tweets_'+geo+'.csv', columns=COLS,index=False)

#declare keywords as a query
keyword='*'

#call main method passing keywords and file path
write_tweets(keyword)

Геокод действительно должен работать вот так.

Есть у кого-нибудь идеи?

...