Функция Tweepy получает 0 твитов для заданной геолокации - PullRequest
0 голосов
/ 21 июня 2020

Я пытаюсь получить 50 твитов о коронавирусе из всех штатов и союзных территорий Индии. Код выглядит следующим образом:

import tweepy 
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor
from time import sleep

auth = OAuthHandler(consumer_key="XXX",consumer_secret="XXX")
auth.set_access_token(key="XXX",secret="XXX")

api=API(auth)

states_and_uts=['Andaman and Nicobar Islands','Andhra Pradesh','Arunachal Pradesh','Assam','Bihar','Chandigarh','Chhattisgarh','Daman and Diu','Delhi','Goa','Gujarat','Haryana','Himachal Pradesh','Jammu and Kashmir','Jharkhand','Karnataka','Kerala','Ladakh','Lakshadweep','Madhya Pradesh','Maharashtra','Manipur','Meghalaya','Mizoram','Nagaland','Odisha','Puducherry','Punjab','Rajasthan','Sikkim','Tamil Nadu','Telangana','Tripura','Uttar Pradesh','Uttarakhand','West Bengal']

location = [(10.2188344, 92.5771329), (15.9240905, 80.1863809), (27.6891712, 96.4597226), (26.4073841, 93.2551303), (25.6440845, 85.906508), (30.7194022, 76.7646552), (21.6637359, 81.8406351), (20.713587, 70.92296517214575), (28.6517178, 77.2219388), (15.3004543, 74.0855134), (22.41540825, 72.03149703699282), (29.0, 76.0), (31.81676015, 77.34932051968858), (33.5574473, 75.06152), (23.4559809, 85.2557301), (14.5203896, 75.7223521), (10.3528744, 76.5120396), (33.9456407, 77.6568576),
            (10.8832771, 72.8171069), (23.9699282, 79.39486954625225), (19.531932, 76.0554568), (24.7208818, 93.9229386), (25.5379432, 91.2999102), (23.2146169, 92.8687612), (26.1630556, 94.5884911), (20.5431241, 84.6897321), (11.9340568, 79.8306447), (30.9293211, 75.5004841), (26.8105777, 73.7684549), (27.601029, 88.45413638680145), (10.9094334, 78.3665347), (17.8495919, 79.1151663), (23.7750823, 91.7025091), (27.1303344, 80.859666), (30.091993549999998, 79.32176659343018), (22.9964948, 87.6855882)]

state_dict={}

for state in states_and_uts:
    state_dict[state]=[]

for state_index in range(len(location)):

    tweets_list = []
    geocode="%.5f"%location[state_index][0]+','+"%.5f" % location[state_index][1]+",10km"
    for tweet in Cursor(api.search,q='corona',result_type="popular",lang='en',geocode=geocode).items(1000):
        retrieved_tweet=tweet.text.encode('utf-8')
        
        if retrieved_tweet not in tweets_list:
            if len(tweets_list)>50:
                break
            else:
                tweets_list.append(retrieved_tweet)

        sleep(0.25)

    print(tweets_list)
    state_dict[states_and_uts[state_index]].extend(tweets_list)

print(state_dict)

Я взял широту и долготу с помощью geopy. Код геопии выглядит следующим образом:

import geopy 
from geopy.geocoders import Nominatim

states_and_uts=['Andaman and Nicobar Islands','Andhra Pradesh','Arunachal Pradesh','Assam','Bihar','Chandigarh','Chhattisgarh','Daman and Diu','Delhi','Goa','Gujarat','Haryana','Himachal Pradesh','Jammu and Kashmir','Jharkhand','Karnataka','Kerala','Ladakh','Lakshadweep','Madhya Pradesh','Maharashtra','Manipur','Meghalaya','Mizoram','Nagaland','Odisha','Puducherry','Punjab','Rajasthan','Sikkim','Tamil Nadu','Telangana','Tripura','Uttar Pradesh','Uttarakhand','West Bengal']

lat_and_long=[]

for state in states_and_uts:

    geolocator=Nominatim()
    location=geolocator.geocode(state)
    print(location.address)
    lat_and_long.append((location.latitude,location.longitude))

print(lat_and_long)

Не знаю почему, но в словаре есть только пустой список. Может кто-нибудь мне с этим помочь?

...