python - 为什么 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”,因此不应返回空文件!
解决方案
我想到了!这是因为我的地理编码字符串中有空格。lat、long 和 radius 之间不应该有空格,只有逗号。
推荐阅读
- node.js - 如何为电子版本 4.0.x 安装 gRPC
- swift - Swift:旋转时 UICollectionViewCell 无法正确调整大小
- powershell - PowerShell foreach 循环,所有迭代的日期相同
- java - 检查样式:JavaDoc
分隔符违规
- javascript - 根据行内容显示特定行表
- python - 使用字符串匹配填充新的数据框列
- excel - 使用 Loop 和 If, ElseIf 导致消息:“找不到”(我在寻找什么)”
- c# - 如果我快速连续运行多个 Azure 搜索索引器,有没有办法访问 Azure SDK 中等待的索引器队列?
- jpa - JPA 如何确保 2 个字段(字符串和布尔值)的唯一性
- angularjs - 出现错误:[$injector:unpr] 未知提供者:dataServiceProvider <- dataService <- firstctrl