一つ目のコードが、Twitterから、入力した検索ワードと検索数に応じた、ツイートのURLを取得するコードです。ふたつ目は、取得したURLの重複をなくすコードです。これらを連結する方法がわかりません。
二つのコードを一つのファイルで実行する方法を教えてください。
python
1import tweepy 2 3# 取得した各種キーを格納----------------------------------------------------- 4consumer_key ="" 5consumer_secret ="" 6access_token="" 7access_token_secret ="" 8 9# Twitterオブジェクトの生成 10auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 11auth.set_access_token(access_token, access_token_secret) 12api = tweepy.API(auth) 13 14#対象の設定 15word = (input("検索ワード>>>")) 16count = int(input("検索数>>>")) 17search_result = api.search(q=word, count=count) 18 19#検索 20for result in search_result: 21 #ツイートIDを取得 22 tweet_id = result.id 23 #ユーザーIDを取得 24 screen_id = result.user.screen_name 25 #ツイートURLを出力 26 # print ("twitter.com/{}/status/{}".format(screen_id,tweet_id)) 27 #ツイートURLをtxtファイルに出力 28 with open ('', 'a') as file: 29 file.write ("{},".format(screen_id)) 30 file.write ("twitter.com/{}/status/{}\n".format(screen_id,tweet_id))
python
1lines_seen = set() 2outfile = open("", "w", encoding="utf-8") 3for line in open("", "r", encoding="utf-8"): 4 key, data = line.split(',',1) 5 if key not in lines_seen: 6 outfile.write(data) 7 lines_seen.add(key) 8outfile.close() 9
あなたの回答
tips
プレビュー