为什么tweepy.Cursor不应该返回空文件

时间:2018-06-19 22:10:58

标签: python api twitter web-crawler

我正在尝试通过twitter api从荷兰抓取以下推文,但它返回一个空文件,并且未显示任何错误。我知道荷兰的人会说英语,并使用Twitter,所以这没有任何意义。这是我的代码:

设置部分(应该没问题):

import tweepy
import csv
import json
import pandas as pd
from pprint import pprint

key_file = 'keys.json'
# Loading your keys from keys.json (which you should have filled
# in in question 1):
with open(key_file) as f:
    keys = json.load(f)
# if you print or view the contents of keys be sure to delete the cell!
consumer_key = keys["consumer_key"]
consumer_secret = keys["consumer_secret"]
access_token = keys["access_token"]
access_token_secret = keys["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)

这是我编写的用于获取数据的函数:

def tweet_obtainer(file_name,key_word = None, language = None,  tweet_since, tweet_until
               , geo = None, place = None, tweet_mode = 'extended', retweet = 'false', number_of_tweet = 100):

    ds_tweets_save_path = file_name + '.json'
    example_tweets = [t._json for t in tweepy.Cursor(api.search, q= key_word,
        lang= language,
        since= tweet_since,
        until= tweet_until,
        geocode = geo,
        place= place,
        tweet_mode= tweet_mode,
        retweet = retweet#-filter:nativeretweets 
    ).items(number_of_tweet)]#number of tweets

    with open(ds_tweets_save_path, "w") as f:        
        json.dump(example_tweets, f)
    with open(ds_tweets_save_path, "r") as f:
        example_tweets = json.load(f)

    return None

这是我在荷兰实施它的那行,然后我得到一个暴风雪的空文件:

tweet_obtainer("data_netherlands", ["is"],"en","2018-06-01", 
    "2018-06-14",geo = "52.1326, 5.2913, 131km"
)

任何英文推文都应包含“ is”,因此它不应返回空文件!

1 个答案:

答案 0 :(得分:0)

我知道了!这是因为我的地理编码字符串中有空格。纬度,经度和半径之间应该没有空格,只能有逗号。