Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用TwythonStreamer从twitterapi获取全文字段值_Python_Twitter_Twython - Fatal编程技术网

Python 如何使用TwythonStreamer从twitterapi获取全文字段值

Python 如何使用TwythonStreamer从twitterapi获取全文字段值,python,twitter,twython,Python,Twitter,Twython,尝试通过以下代码获取完整推文。我知道您想将参数tweet_mode设置为值“extended”,但因为我不是这里的标准查询,所以我不知道它适合哪里。对于文本字段,我总是用“…”和URL截断部分文本。使用此配置,您将如何获得完整的推文: from twython import Twython, TwythonStreamer import json import pandas as pd import csv def process_tweet(tweet): d = {} d[

尝试通过以下代码获取完整推文。我知道您想将参数tweet_mode设置为值“extended”,但因为我不是这里的标准查询,所以我不知道它适合哪里。对于文本字段,我总是用“…”和URL截断部分文本。使用此配置,您将如何获得完整的推文:

from twython import Twython, TwythonStreamer
import json
import pandas as pd
import csv

def process_tweet(tweet):
    d = {}
    d['hashtags'] = [hashtag['text'] for hashtag in tweet['entities']['hashtags']]
    d['text'] = tweet['text']
    d['user'] = tweet['user']['screen_name']
    d['user_loc'] = tweet['user']['location']
    return d
    
    
# Create a class that inherits TwythonStreamer
class MyStreamer(TwythonStreamer):     

    # Received data
    def on_success(self, data):

        # Only collect tweets in English
        if data['lang'] == 'en':
            tweet_data = process_tweet(data)
            self.save_to_csv(tweet_data)

    # Problem with the API
    def on_error(self, status_code, data):
        print(status_code, data)
        self.disconnect()
        
    # Save each tweet to csv file
    def save_to_csv(self, tweet):
        with open(r'tweets_about_california.csv', 'a') as file:
            writer = csv.writer(file)
            writer.writerow(list(tweet.values()))

# Instantiate from our streaming class
stream = MyStreamer(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET'], 
                    creds['ACCESS_TOKEN'], creds['ACCESS_SECRET'])
# Start the stream
stream.statuses.filter(track='california', tweet_mode='extended')

tweet\u mode=extended
参数对v1.1流媒体API没有影响,因为所有tweet都以扩展和默认(140)格式发送

如果Tweet对象具有值
truncated:true
,则有效负载中将有一个额外的元素-
extended\u Tweet
。这是存储
全文
值的地方


请注意,此答案仅适用于v1.1 Twitter API,在v2中,所有推文文本在流式API中默认返回(Twython当前不支持v2)。

参数
Tweet\u mode=extended
对v1.1流式API没有影响,因为所有推文都以扩展和默认(140)格式发送

如果Tweet对象具有值
truncated:true
,则有效负载中将有一个额外的元素-
extended\u Tweet
。这是存储
全文
值的地方


请注意,此答案仅适用于v1.1 Twitter API,在v2中,所有推文文本在流式API中默认返回(Twython当前不支持v2)。

此选项工作正常,但它不在Twitter()的官方文档中。因此,事实上,官方文档位于多个位置,而主文档不是最新的。此选项工作正常,但它不在Twitter()的官方文档中。因此,事实上,官方文档位于多个位置,而主文档不是最新的。