【问题标题】:Saving Urdu tweets from tweepy in CSV file将来自 tweepy 的乌尔都语推文保存在 CSV 文件中
【发布时间】:2018-07-16 11:17:10
【问题描述】:

我正在尝试在 python 中使用 tweepy 仅保存乌尔都语推文。我正在使用 3.6 版本的 python。如果我打开文件,乌尔都语的数据不会保存在文件中,我只能看到用户名,而不是乌尔都语的推文。这是我的代码

同样的代码适用于英语。

import re
import io
import csv
import tweepy
from tweepy import OAuthHandler
#from textblob import TextBlob


consumer_key = "xxxxxxxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxx"
access_key = "xxxxxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxxxxxxxxxx"


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access token and secret
auth.set_access_token(access_key, access_secret)
# create tweepy API object to fetch tweets
api = tweepy.API(auth)




def get_tweets(query, count = 300):

    # empty list to store parsed tweets
    tweets = []
    target = io.open("newsfileurdu.csv", 'w', encoding='utf-8')
    # call twitter api to fetch tweets
    q=str(query)
    a=str(q+" اردو")
    b=str(q+" خبریں")
    c=str(q+" خبریں اردو")
    fetched_tweets = api.search(a, count = count)+ api.search(b, count = count)+ api.search(c, count = count)
    # parsing tweets one by one
    print(len(fetched_tweets))

    for tweet in fetched_tweets:

        # empty dictionary to store required params of a tweet
        parsed_tweet = {}
        # saving text of tweet
        parsed_tweet['text'] = tweet.text
        if "http" not in tweet.text:
            line = re.sub("[^A-Za-z]", " ", tweet.text)
            target.write(line+"\n")
    return tweets

    # creating object of TwitterClient Class
    # calling function to get tweets
tweets = get_tweets(query ="", count = 20000)

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    所需推文的语言(在这种情况下为urdu)可以通过将lang="ur" 传递给api.search 函数或使用下面的代码sn-p 来提取:

    api.search(q=<query goes here>, lang ['ur'], tweet_mode='extended', count=<tweets_per_query>)
    

    tweepy 搜索 API 使用 ISO 639-1 代码 指定“lang”参数(因此对于 urdu,它是“ur”)。因此,对于任何所需的语言,只需在 language code here 中搜索并将该代码传递给 lang 参数即可提取推文。

    【讨论】:

    • 位置参数跟在关键字参数之后
    猜你喜欢
    • 2020-04-11
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多