Python 无法从其他文件启动tweepy推文刮板
我的tweepy锉刀很好用。它收集所有德国推特。 现在它有时会中断。因此,我想用另一个python文件重新启动它。 Tweepy文件名为“Twython_36”,由my Scraper_Starter.py启动:Python 无法从其他文件启动tweepy推文刮板,python,python-3.x,twitter,tweepy,Python,Python 3.x,Twitter,Tweepy,我的tweepy锉刀很好用。它收集所有德国推特。 现在它有时会中断。因此,我想用另一个python文件重新启动它。 Tweepy文件名为“Twython_36”,由my Scraper_Starter.py启动: import threading def printit(): threading.Timer(15.0, printit).start() print("Scraper restartet.") exec(open("./twython_36.py").rea
import threading
def printit():
threading.Timer(15.0, printit).start()
print("Scraper restartet.")
exec(open("./twython_36.py").read())
printit()
现在我得到了这个错误
Traceback (most recent call last):
File "<string>", line 42, in on_status
NameError: name 'langid' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/M/PycharmProjects/Bachelor_Thesis/Scraper_Starter.py", line 10, in <module>
printit()
File "C:/Users/M/PycharmProjects/Bachelor_Thesis/Scraper_Starter.py", line 7, in printit
exec(open("./twython_36.py").read())
File "<string>", line 81, in <module>
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 445, in filter
self._start(async)
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 361, in _start
self._run()
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 294, in _run
raise exception
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 263, in _run
self._read_loop(resp)
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 324, in _read_loop
self._data(next_status_obj)
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 297, in _data
if self.listener.on_data(data) is False:
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 54, in on_data
if self.on_status(status) is False:
File "<string>", line 49, in on_status
NameError: name 'sys' is not defined
只是提出一些想法。但是,您可以使用cron作业,而不是让Python脚本全天候运行。正如错误所说的
NameError:name'sys'未定义
,因此添加缺少的import sys
,谢谢。导入langid和sys后,它可以工作!只是提出一些想法。但是,您可以使用cron作业,而不是让Python脚本全天候运行。正如错误所说的NameError:name'sys'未定义
,因此添加缺少的import sys
,谢谢。导入langid和sys后,它可以工作!
File "C:\Users\M\Anaconda3\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Users\M\Anaconda3\lib\threading.py", line 1180, in run
self.function(*self.args, **self.kwargs)
File "C:/Users/M/PycharmProjects/Bachelor_Thesis/Scraper_Starter.py", line 8, in printit
exec(open("./twython_36.py").read())
File "<string>", line 81, in <module>
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 445, in filter
self._start(async)
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 361, in _start
self._run()
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 294, in _run
raise exception
File "C:\Users\M\Anaconda3\lib\site-packages\tweepy\streaming.py", line 249, in _run
if self.listener.on_error(resp.status_code) is False:
File "<string>", line 52, in on_error
N**ameError: name 'logfile' is not defined**
**# open log file
logfile = open('twython1.log', 'a')**
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
global old_date
global writer
global outfile
new_date = date.today()
if not new_date == old_date:
outfile.close
outfile = codecs.open ("tweets-" + str(new_date) + ".txt", "ab", "utf-8")
writer = csv.writer(outfile,quoting=csv.QUOTE_NONNUMERIC,lineterminator='\n')
old_date = new_date
try:
lang = langid.classify(status.text)[0];
if lang == "de":
writer.writerow( (status.created_at, status.id_str, status.text) )
except Exception, e:
# Catch any unicode errors while printing to console
# and just ignore them to avoid breaking application.
sys.exc_clear()
# pass
**def on_error(self, status_code):
logfile.write(str(time.asctime( time.localtime(time.time()) )) + ' Encountered error with status code:' + str(status_code) + "\n")
return True # Don't kill the stream**