Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 如何通过tweet搜索查询获取用户名_Python - Fatal编程技术网

Python 如何通过tweet搜索查询获取用户名

Python 如何通过tweet搜索查询获取用户名,python,Python,我试图在twitter推特搜索查询中的用户名数据框中添加一列。对代码有什么想法吗 import tweepy import pandas as pd auth = tweepy.OAuthHandler(consumer_key=con_key, consumer_secret=con_secret) auth.set_access_token(acc_token, acc_secret) api = tweepy.API(auth) num_needed = 1000 tweet_li

我试图在twitter推特搜索查询中的用户名数据框中添加一列。对代码有什么想法吗

import tweepy
import pandas as pd

auth = tweepy.OAuthHandler(consumer_key=con_key, consumer_secret=con_secret)
auth.set_access_token(acc_token, acc_secret)

api = tweepy.API(auth)


num_needed = 1000
tweet_list = []
last_id = -1 # id of last tweet seen
while len(tweet_list) < num_needed:
    try:
        new_tweets = api.search(q = 'Python', count = 300, max_id = str(last_id - 1), lang = 'en', tweet_mode = 'extended')
    except tweepy.TweepError as e:
        print("Error", e)
        break
    else:
        if not new_tweets:
            print("Could not find any more tweets!")
            break
        tweet_list.extend(new_tweets)
        last_id = new_tweets[-1].id

[tweet.full_text for tweet in tweet_list]
        
df = pd.DataFrame([tweet.full_text for tweet in tweet_list], columns = ['Tweets'])

导入tweepy
作为pd进口熊猫
auth=tweepy.OAuthHandler(使用者密钥=con密钥,使用者密钥=con密钥)
授权设置访问令牌(acc令牌,acc密钥)
api=tweepy.api(auth)
所需数量=1000
tweet_list=[]
last_id=-1#看到的最后一条推特的id
而len(tweet_列表)
tweet.user中包含的用户名是什么?如果是,则创建第二列,名为:

df['user'] = pd.Series([tweet.user for tweet in tweet_list]
导入tweepy
作为pd进口熊猫
auth=tweepy.OAuthHandler(使用者密钥=con密钥,使用者密钥=con密钥)
授权设置访问令牌(acc令牌,acc密钥)
api=tweepy.api(auth)
所需数量=10
tweet_list=[]
用户列表=[]
屏幕名称列表=[]
last_id=-1#看到的最后一条推特的id
而len(tweet_列表)
这似乎不起作用,这取决于文件的结构。你能分享一个例子吗?
import tweepy
import pandas as pd

auth = tweepy.OAuthHandler(consumer_key=con_key, consumer_secret=con_secret)
auth.set_access_token(acc_token, acc_secret)

api = tweepy.API(auth)


num_needed = 10
tweet_list = []
user_list = []
screen_name_list = []
last_id = -1 # id of last tweet seen
while len(tweet_list) < num_needed:
    try:
        new_tweets = api.search(q = 'Python', count = 300, max_id = str(last_id - 1), lang = 'en', tweet_mode = 'extended')
    except tweepy.TweepError as e:
        print("Error", e)
        break
    else:
        if not new_tweets:
            print("Could not find any more tweets!")
            break
        else:
            for tweet in new_tweets:
                # fetching the screen name 

                screen_name = tweet.author.screen_name
                user_name = tweet.author.name
  
                tweet_text = tweet.full_text
                tweet_list.append(tweet_text)
                user_list.append(user_name)
                screen_name_list.append(screen_name)
        
df = pd.DataFrame()
df["tweets"] = tweet_list
df["user_name"] = user_list
df["screen_name"] = screen_name_list

df