I'm trying to extract tweets using the historical Twitter API from Twitter and then copying them onto a text file. However, the text file is not being written to at all.
I've tried writing to a CSV though that has not worked either. It is being run on Python 3.6 and all the libraries are installed. I am not getting any error messages suggesting a problem with the text file.
import tweepy
import sys
import os
import codecs
CONSUMER_KEY = "" # These are removed for obvious reasons!
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
f = codecs.open('C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt', "w", encoding="utf-8")
for tweet in tweepy.Cursor(api.search,
q="brexit",
count=100,
since="2019-04-28",
until="2019-04-29",
lang="en").items():
print(tweet.text)
f.write(tweet.text)
I would expect to have the text from some tweets written to file f, but it is blank after I have stopped the code from running.
Maybe you can first try if you are authorized to write with this minimal example:
file_name = 'C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt'
with open(file_name, 'w') as f:
f.write("Hello World!")
Related
I am trying to print all screen_names found on a search result with Twython.
Here is my current code
#Import the required modules
from twython import Twython
#Setting the OAuth
Consumer_Key = ''
Consumer_Secret = ''
Access_Token = '-'
Access_Token_Secret = ''
#Connection established with Twitter API v1.1
twitter = Twython(Consumer_Key, Consumer_Secret,
Access_Token, Access_Token_Secret)
#Twitter is queried
response = twitter.search(q='Roblox', since='2017-02-25', until='2017-02-26')
for tweet in response["statuses"]:
st = tweet["entities"]["user_mentions"]
screen_name = st[0]["screen_name"]
f = open("output.txt", "a")
f.write(str(screen_name, ",\n"))
print(screen_name)
f.close()
but it is not editing or printing in the log file. Right as I start it, the program stops with no apparent error.
(the codes have been filled in, just removed for the post)
Sooo yeah, pretty much what the title says. Can't seem to tweet foreign letter's to twitter, not quite sure why though, so i'm hoping if anybody can help me there. The foreign letters are "õüöä" Here is my .py file:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, time, sys
argfile = str(sys.argv[1])
#API details
CONSUMER_KEY = 'xx'
CONSUMER_SECRET = 'xx'
ACCESS_KEY = 'xx'
ACCESS_SECRET = 'xx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
str.decode('utf-8')
filename=open(argfile,'r')
f=filename.readlines()
filename.close()
for line in f:
api.update_status(line)
time.sleep(120)
My .txt file where tweets are included:
Üks
Kaks
Kolm
Neli
Viis
Kuus
Seitse
Kaheksa
Üheksa
Kümme
I am trying to:
Grab multiple tweets, find urls inside the tweets, grab the urls I found and save each "url data" to different txt file.
So far I managed to write these parts and I am stuck, can someone please help me?
Grab tweets:
import tweepy
from tweepy import OAuthHandler
import sys
import re
def process_or_store(tweet):
print(json.dumps(tweet))
consumer_key = '***************************'
consumer_secret = '******************************'
access_token = '*******************************'
access_secret = '****************************'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
f = open("C:\\Twitter\Twitts.txt",'w')
print >>f, 'twits'
for status in tweepy.Cursor(api.home_timeline).items(20):
# Process a single status
print(status.text)
f.write(status.text+ '\n')
def extract_urls(fname):
with open(fname) as f:
return re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', f.read())
Data from URL:
import urllib2
url = 'https://***************'
response = urllib2.urlopen(url)
with open('C:\\Twitter\Data_from_urls\ + "url"', 'w') as f:
f.write(response.read())
I wasn't able to serialize Twitter API results until I used jsonpickle (that's code A below), but then I wasn't able to decode the json file (code B below). Code A created one large json object. Please help.
Code A
#!/usr/bin/env python
import tweepy
import simplejson as json
import jsonpickle
consumer_key = "consumer key here"
consumer_secret = "consumer secret here"
access_token = "access token here"
access_token_secret = "access token secret here"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.ModelParser())
jsonFile = open('/location_here/results.json', 'wb')
for status in tweepy.Cursor(api.search, q="keyword").items():
print status.created_at, status.text
frozen = jsonpickle.encode(status)
s = json.loads(frozen)
json.dump(s, jsonFile)
Code B
import jsonpickle
import simplejson as json
in_file = open("/location_here/results.json")
out_file = open("/location_here/data.json", 'wb')
jsonstr = in_file.read()
thawed = jsonpickle.decode(jsonstr)
data = json.loads(thawed)
This gives an error, ValueError: Trailing data.
Thanks.
I'm trying to post a tweet with the tweepy library. I use this code:
import tweepy
CONSUMER_KEY ="XXXX"
CONSUMER_SECRET = "XXXX"
ACCESS_KEY = "XXXX"
ACCESS_SECRET = "XXXX"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('Updating using OAuth authentication via Tweepy!')
But when I run the application, I receive this error:
raise TweepError(error_msg, resp)
TweepError: Read-only application cannot POST.
How can I fix this?
In the application's settings, set your Application Type to "Read and Write". Then renegotiate your access token.
the code works for me with only
api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
You have to set your app to read and write
After that, you'll be able to run your code.
the following python script will tweet a line from a text file. if you want to tweet multiple tweets just put them on a new line separated by a blank line.
import tweepy
from time import sleep
# import keys from cregorg.py
from credorg import *
client = tweepy.Client(bearer_token, consumer_key, consumer_secret, access_token, access_token_secret)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
print("Tweet-TXT-Bot v1.0 by deusopus (deusopus#gmail.com)")
# Open text file tweets.txt (or your chosen file) for reading
my_file = open('tweets.txt', 'r')
# Read lines one by one from my_file and assign to file_lines variable
file_lines = my_file.readlines()
# Close file
my_file.close()
# Tweet a line every 5 minutes
def tweet():
for line in file_lines:
try:
print(line)
if line != '\n':
api.update_status(line)
sleep(300)
else:
pass
except tweepy.errors.TweepyException as e:
print(e)
while True:
tweet()