首页 > 解决方案 > 为什么 tweepy.Cursor 不应该返回空文件

问题描述

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

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

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”,因此不应返回空文件!

标签: pythonapitwitterweb-crawler

解决方案


我想到了!这是因为我的地理编码字符串中有空格。lat、long 和 radius 之间不应该有空格,只有逗号。


推荐阅读