python - 错误代码 400

标签 python anaconda tweepy

我正在尝试下载一些推文用于研究目的代码在几天前运行良好。

错误信息

> Traceback (most recent call last):
> 
>   File "<ipython-input-1-10547a7aea4c>", line 1, in <module>
>     runfile('F:/twitter_locations_n_scraper/02_twitterscrapping.py', wdir='F:/twitter_locations_n_scraper')
> 
>   File
> "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
> line 668, in runfile
>     execfile(filename, namespace)
> 
>   File
> "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
> line 108, in execfile
>     exec(compile(f.read(), filename, 'exec'), namespace)
> 
>   File "F:/twitter_locations_n_scraper/02_twitterscrapping.py", line
> 70, in <module>
>     for item in cursor.items(1000): # Remove the limit to 1000
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py",
> line 49, in __next__
>     return self.next()
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py",
> line 197, in next
>     self.current_page = self.page_iterator.next()
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py",
> line 108, in next
>     data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py",
> line 250, in _call
>     return method.execute()
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py",
> line 234, in execute
>     raise TweepError(error_msg, resp, api_code=api_error_code)
> 
> TweepError: Twitter error response: status code = 400

我确实尝试了假设的解决方案

api.update_status('Test')

但这对我不起作用,我确实更改了 key ,但我仍然面临同样的错误,重新安装了两次 Tweepy 包,但错误没有消失。

感谢您帮助解决错误,提前致谢。

更新:我遇到错误的代码

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

path="F:\\twitter_locations_n_scraper\\"


ckey = '*****'
csecret = '******'
atoken = '*****'
asecret = '*****'

def toDataFrame(tweets):
    # COnvert to data frame
    DataSet = pd.DataFrame()

    DataSet['tweetID'] = [tweet.id for tweet in tweets]
    DataSet['tweetText'] = [tweet.text.encode('utf-8') for tweet in tweets]
    DataSet['tweetRetweetCt'] = [tweet.retweet_count for tweet in tweets]
    DataSet['tweetFavoriteCt'] = [tweet.favorite_count for tweet in tweets]
    DataSet['tweetSource'] = [tweet.source for tweet in tweets]
    DataSet['tweetCreated'] = [tweet.created_at for tweet in tweets]
    DataSet['userID'] = [tweet.user.id for tweet in tweets]
    DataSet['userScreen'] = [tweet.user.screen_name for tweet in tweets]
    DataSet['userName'] = [tweet.user.name for tweet in tweets]
    DataSet['userCreateDt'] = [tweet.user.created_at for tweet in tweets]
    DataSet['userDesc'] = [tweet.user.description for tweet in tweets]
    DataSet['userFollowerCt'] = [tweet.user.followers_count for tweet in tweets]
    DataSet['userFriendsCt'] = [tweet.user.friends_count for tweet in tweets]
    DataSet['userLocation'] = [tweet.user.location for tweet in tweets]
    DataSet['userTimezone'] = [tweet.user.time_zone for tweet in tweets]
    DataSet['Coordinates'] = [tweet.coordinates for tweet in tweets]
    DataSet['GeoEnabled'] = [tweet.user.geo_enabled for tweet in tweets]
    DataSet['Language'] = [tweet.user.lang for tweet in tweets]
    tweets_place= []
    #users_retweeted = []
    for tweet in tweets:
        if tweet.place:
            tweets_place.append(tweet.place.full_name)
        else:
            tweets_place.append('null')
    DataSet['TweetPlace'] = [i for i in tweets_place]
    #DataSet['UserWhoRetweeted'] = [i for i in users_retweeted]

    return DataSet

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,'access_token_key':atoken, 'access_token_secret':asecret}
#auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
auth = tweepy.AppAuthHandler('******', '*******')

api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

if (not api):
    print ("Can't Authenticate")
    sys.exit(-1)
else:
    df=pd.read_csv(path+"00_all_location_India_full.csv")

    print ("Scraping data now") # Enter lat and long and radius in Kms
    for i in range(0,len(df)):
        x="%s,%s,30km"%(df['latitude'][i],df['longitude'][i]) #,q='motox4', since='2018-08-16',until='2018-08-18',
        cursor = tweepy.Cursor(api.search,since='2018-08-23',until='2018-08-24',lang='en',count=1000)
        results=[]
        print (i)
        for item in cursor.items(1000): # Remove the limit to 1000
            results.append(item)
        DataSet = toDataFrame(results)
        DataSet['latitude']=df['latitude'][i]
        DataSet['longitude']=df['longitude'][i]
        DataSet['radius']=30
        del DataSet['Coordinates']
        DataSet.to_csv(path+'cities_aug23.csv',encoding='utf-8',index=False,mode="a")

最佳答案

根据 Twitter 官方 API 页面。 代码 400 表示以下错误, 因此您的问题是由以下原因之一引起的,请一一检查:

-attachment_url 参数无效对应于 HTTP 400。提供的 URL 值不是可以附加到此推文的 URL。

-Bad authentication data 对应于 HTTP 400。该方法需要身份验证,但未提供或完全无效。

-parameter is invalid 对应于 HTTP 400。提供的 URL 值不是 URL 可以附加到此推文。

-Bad Request 请求无效或无法以其他方式提供。随附的错误消息将进一步解释。 未经身份验证的请求被视为无效,并将产生此响应。

- 上传多张图片时不允许使用 GIF 动画。对应 HTTP 400。一条推文只能附加一个动画 GIF

- 媒体 ID 验证失败。对应于 HTTP 400。与推文一起提交的媒体 ID 存在问题。

-未找到媒体 ID。对应于 HTTP 400。未找到附加到推文的媒体 ID。

-给定的 URL 无效。对应于 HTTP 400。无法处理推文中包含的 URL。这可能是因为无法转换非 ASCII URL 或其他原因。

关于python - 错误代码 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045599/

相关文章:

python - 用不同的语言对程序进行单元测试是否可以接受?

python - 在spyder中启动内核时发生错误

python - 如何在 tweepy 上搜索一条推文中的多个单词?

python - 'charmap' 编解码器无法编码字符

python - 将 SSL 验证与 Tweepy 结合使用

python - 在 Python 中格式化正则表达式

python - 如何将 optparse 选项与 python 中的变量名称集成

python - psycopg2:如何在 python 脚本中执行真空 postgresql 查询

python - 找不到文件 python.h

keras - 属性错误 : module 'theano' has no attribute 'ifelse'