What does a CRITICAL:root:twint.get:User: Error mean? - python

I'm a beginner with coding.
I'm trying to scrape tweets from a Twitter account.
I'm getting the following error when I run my code: CRITICAL:root:twint.get:User:
Here is the code that I'm running:
import twint
config = twint.Config()
# Search tweets tweeted by user 'BarackObama'
config.Username = "BarackObama"
# Limit search results to 20
config.Limit = 20
# Return tweets that were published after Jan 1st, 2020
config.Since = "2020-01-1 20:30:15"
# Formatting the tweets
config.Format = "Tweet Id {id}, tweeted at {time}, {date}, by {username} says: {tweet}"
# Storing tweets in a csv file
config.Store_csv = True
config.Output = "Barack Obama"
twint.run.Search(config)
Does this error mean it's a problem with Twint, or is there a mistake in my code?
Thank you!

I'm a beginner as well, trying to learn python as a tool for Digital History in Brazil.
I was getting the same problem (only when using c.Username parameter).
I solved running pip3 install --upgrade -e git+https://github.com/twintproject/twint.git#origin/master#egg=twint
I hope it works for you.

The solution is in modifying user.py in the twint source, and replace lines with a try and except statement.
try:
_usr.name = ur['data']['user']['legacy']['name']
except:
_usr.name = ''
try:
_usr.username = ur['data']['user']['legacy']['screen_name']
except:
_usr.username = ''
try:
_usr.bio = ur['data']['user']['legacy']['description']
except:
_usr.bio = ''
try:
_usr.location = ur['data']['user']['legacy']['location']
except:
_usr.location = ''
try:
_usr.url = ur['data']['user']['legacy']['url']
except:
_usr.url = ''

Related

Python How to Program to be Closed After a Certain Time

This question would be a little silly, but I'm very very new to coding. I could not add the solutions I found on the internet to my codes. I would be so glad if you help.
When I open the .py file, the program does not stop automatically. And it running until I close the command window. After I open this file, I want it to run for 5 minutes and then turn itself off. How can I set a time limit for close the program?
import tweepy
import time
auth = tweepy.OAuthHandler('*','*')
auth.set_access_token('*','*')
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()
search = 'books'
nrTweets = 500
for tweet in tweepy.Cursor(api.search, search).items(nrTweets):
try:
print('Tweet Liked')
tweet.favorite()
time.sleep(10)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
You can monitor the running time of your program, and break the for-loop after the time limit.
import time
time_limit_sec = 5 * 60
start_time = time.time()
for tweet in tweepy.Cursor(api.search, search).items(nrTweets):
if (time.time()-start_time) > time_limit_sec:
break
... # your code here

Stuck in download when using Instaloader with Python

I was just getting started with Instaloader but when i tried to download a specific post, my code wouldn't continue
from instaloader import Instaloader, Profile, Post
# Get instance
L = Instaloader()
L.login(username, password)
print("login complete")
post = Post.from_shortcode(L.context, "CEPH-B0M8B9")
L.download_post(post, target='test')
print("test")
It wouldn't print the "test"
And I was also having some difficulties changing the filename as which the post would get saved. In the documentation it says:
target (Union[str, Path]) – Target name, i.e. profile name, #hashtag,
:feed; for filename.
but that wasn't helping me at all :/
I appreciate every answer :D
After looking at the source code i found the problem.
The download_post function does a lot of stuff and you can deactivate them with these lines:
L = Instaloader()
L.post_metadata_txt_pattern = ""
L.download_geotags = False
L.save_metadata = False
L.save_metadata_json = False
L.download_comments = False
The issue why the code wouldn't continue was that the function just took forever to download all comments
Hope this will help and safe some time for someone in the future :)

Python win32com SaveAs2 error when I try to save file

def convert_to_word():
target = pwd + "/open.doc"
source = pwd + "/template.html"
pythoncom.CoInitialize()
app = win32com.client.Dispatch("Word.Application")
pythoncom.CoInitialize()
# try:
app.Documents.Open(source)
app.Documents.SaveAs2(target,FileFormat=0)
app.Documents.Open(source)
app.Selection.WholeStory()
app.Selection.Fields.Unlink()
app.Documents.Save()
# except Exception as e:
# print(e)
# finally:
app.ActiveDocument.Close()
I need to save html file to .doc, but it report a error <unknown>.SaveAs2 which I cant solve.
Can anyone help me ? Thanks
My first answer on stackoverflow ...
I had had same troubles then I have found solution:
Your code seems to be almost OK. You just need to store your opened document to a variable and then use SaveAs2 function on the variable.
my_doc = app.Documents.Open(source)
my_doc.SaveAs2(target, FileFormat=0)

Syntax error in python2 script using ldap module

Learning python (was chosen for its ldap module) for a new script that has been tossed my way. I'm getting a sytntax error when I try using a ldif. I was getting Syntax errors on the attrs I was trying to assign until I moved it further up the script to near the search fields. I'm not exactly sure why I am getting the syntax error:
File "UserGroupModify.py", line 66
attrs = {}
^
SyntaxError: invalid syntax
~/Scripts/Termination-Script$ python2 UserGroupModify.py
File "UserGroupModify.py", line 69
ldif = modlist.addModlist(attrs)
^
SyntaxError: invalid syntax
The code currently looks like the following (including previous things I had tried all with syntax errors of their own when I tried to use them). Getting it to log in and search for the user was easy enough, but modifying the user is where I am having a hard time. The current code is uncommented and is from an example I found online.
#!/usr/bin/env python2
import ldap
import getpass
import ldap.modlist as modlist
## first you must open a connection to the server
try:
#Ignore self signed certs
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
username = raw_input("LDAP Login: ")
passwd = getpass.getpass()
userlook = raw_input("User to lookup: ")
l = ldap.initialize("ldaps://ldap.example.com:636/")
# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s("uid="+username+",ou=people,dc=example,dc=com", ""+passwd+"")
except ldap.LDAPError, e:
print(e)
# The dn of our existing entry/object
dn = "ou=People,dc=example,dc=com"
searchScope = ldap.SCOPE_SUBTREE
searchAttribute = ["uid"]
#retrieveAttributes = ["ou=Group"]
retrieveAttributes = ["ou"]
#searchFilter = "uid=*"
searchFilter = "(uid="+userlook+")"
#mod_attrs = [(ldap.MOD_REPLACE, 'ou', 'former-people' )]
attrs = {}
attrs['member'] = ['uid="+userlook+",ou=former-people,dc=example,dc=com']
try:
#ldap_result_id = l.search(dn, searchScope, searchFilter, retrieveAttributes)
ldap_result_id = l.search(dn, searchScope, searchFilter, retrieveAttributes)
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the individual entry
## The appending to list is just for illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
print(result_data)
# Some place-holders for old and new values
#old={'Group':'l.result(ldap_result_id, 0)'}
#new={'Group':'uid="+userlook+",ou=former-people,dc=example,dc=com'}
#newsetting = {'description':'I could easily forgive his pride, if he had not mortified mine.'}
#print(old)
#print(new)
# Convert place-holders for modify-operation using modlist-module
#ldif = modlist.modifyModlist(old,new)
# Do the actual modification
#l.modify_s(dn,ldif)
#l.modify_s('uid="+userlook+,ou=People,dc=example,dc=com', mod_attrs)
#l.modify_s('uid="+userlook+",ou=People', mod_attrs)
#moved up due to SyntaxError
#attrs = {}
#attrs['member'] = ['uid="+userlook+",ou=former-people,dc=example,dc=com']
# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)
# Do the actual synchronous add-operation to the ldapserver
l.add_s(dn,ldif)
# Its nice to the server to disconnect and free resources when done
l.unbind_s()
except ldap.LDAPError, e:
print(e)
Any direction pointing on what's causing the error would be greatly appreciated. Thanks
It's a syntax error to have try without except. Because there's a whole lot of unindented code before the except, Python doesn't see it as part of the try. Make sure everything between try and except is indented.
You haven't ended your try block by the time you reach this line
ldif = modlist.addModlist(attrs)
since the accompanying except is below. However, you reduced the indentation level and this is causing the syntax error since things in the same block should have the same indentation.

error in python : unexpected EOF while parsing,how to resolve this?

I am running this code to extract to get tweets. But running this code gives me a Syntaxerror : unexpected EOF while parsing. I am new to python so any help is appreciated! Thanks in advance.
from TwitterSearch import *
try:
tso = TwitterSearchOrder()
tso.setKeywords(['xyz', 'abc'])
tso.setLanguage('de') # we want to see German tweets only
tso.setCount(7) # please dear Mr Twitter, only give us 7 results per page
tso.setIncludeEntities(False) # and don't give us all those entity information
ts = TwitterSearch(
consumer_key = 'a',
consumer_secret = 'b',
access_token = 'c',
access_token_secret = 'd'
)
for tweet in ts.searchTweetsIterable(tso):
print('#%s tweeted: %s' % (tweet['user']['screen_name'], tweet['text']))
You need an except block at the end of your script:
except Exception, e:
print str(e)
I would suggest doing something aside from printing the exception and moving on, but this is the basic layout.

Categories

Resources