参考:Python判断文件是否存在的三种方法

参考:在python文件中执行另一个python文件

参考:How can I make a time delay in Python?

参考:Twilio SMS Python Quickstart


1. 收集某一区域的实时数据

Name: AUS.py

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream #Variables that contains the user credentials to access Twitter API
access_token = "*****"
access_token_secret = "*****"
consumer_key = "*****"
consumer_secret = "*****" #This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener): def on_data(self, data):
print(data)
return True def on_error(self, status):
print(status) if __name__ == '__main__': #This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l) #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(locations=[112, -44, 154, -9])

在 cmd 上运行代码 python AUS.py > --.txt ,将数据实时存储。

通过上面的代码可以将打印出来的数据直接存储到文本文件中。(类似 print() 可以直接将内容存储)

2.自动发短信功能

由于数据存储到一定量会出现奔溃的情况,因此增加 Twilio 自动发短信功能,遇到奔溃可以实时发短信,实现如下:

文件名: AUS_SMS.py

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from twilio.rest import Client
import time #Variables that contains the user credentials to access Twitter API
access_token = "*****"
access_token_secret = "*****"
consumer_key = "*****"
consumer_secret = "*****" #This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener): def on_data(self, data):
print(data)
return True def on_error(self, status):
print(status) def textMessage(message):
account = '*****'
token = '*****'
myNumber='+*****'
twilioNumber='+*****' client = Client(account, token)
message = client.messages.create(to=myNumber, from_=twilioNumber, body=message) if __name__ == '__main__':
try:
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l) #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(locations=[112, -44, 154, -9])
except:
textMessage("n(*≧▽≦*)n [HELP] Program crashed!!!\nTime: "+time.asctime())

3. 无限运行

可以直接通过 Python 文件来运行 Python 文件,通过建立无线循环可以实现无限收集数据

文件名:main.py

import os
import time while True:
year = str(time.localtime().tm_year)
mon = str(time.localtime().tm_mon)
day = str(time.localtime().tm_mday)
filename = year + '-' + mon.zfill(2) + '-' + day.zfill(2)
i = 0
while os.path.exists(os.getcwd() + '\\' + filename + '.txt'):
i += 1
filename = year + '-' + mon.zfill(2) + '-' + day.zfill(2) + '-' + str(i)
time.sleep(1)
os.system("python AUS_SMS.py > " + filename + '.txt')

按照当天日期进行文件名命名,如果同一天的文件存在,则后面加 1,然后加 2,,,以此类推。。。

通过 os.system() 方法可以实现 cmd 运行 Python 文件的效果。

【402】Twitter Data Collection的更多相关文章

  1. 【DataGuard】部署Data Guard相关参数详解 (转载)

    原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler    有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Gua ...

  2. LeetCode:移除K位数字【402】

    LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...

  3. 【机器学习】Iris Data Set(鸢尾花数据集)

    [机器学习]Iris Data Set(鸢尾花数据集) 注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数 ...

  4. 【转】char data[0]用法总结

    @2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...

  5. 【原创】Mapped Statements collection does not contain value for DaoImpl.method

    问题: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Pers ...

  6. 【转】Hive Data Manipulation Language

    Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Syn ...

  7. 【WinForm】“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常,无法识别的配置节 system.serviceModel

    出现问题的原因: 在本机上没有出现问题,让一个同事测试的时候,在另外一台电脑上出现连接数据库失败,系统不能打开的问题 在网上搜了一下,有说是数据库连接字符串错误的,有说app.config文件配置不匹 ...

  8. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  9. 【原创】System.Data.SQLite内存数据库模式

    对于很多嵌入式数据库来说都有对于的内存数据库模式,SQLite也不例外.内存数据库常常用于极速.实时的场景,一个很好的应用的场景是富客户端的缓存数据,一般富客户端的缓存常常需要分为落地和非落地两种,而 ...

随机推荐

  1. 我用Python爬虫挣钱的那点事

    在下写了10年Python,期间写了各种奇葩爬虫,挣各种奇葩的钱,写这篇文章总结下几种爬虫挣钱的方式. 1.最典型的就是找爬虫外包活儿.这个真是体力活,最早是在国外各个freelancer网站上找适合 ...

  2. C 预编译 宏 声明

  3. Oracle中查询表中数据的上次更新时间

    目前找到的是以下方式,但是这种方式在表的数据量比较大的时候效率会比较慢. select to_char(scn_to_timestamp(max(ora_rowscn)),'YYYY-MM-DD HH ...

  4. ndk学习之c++语言基础复习----面向对象编程

    关于面向对象编程对于一个java程序员那是再熟悉不过了,不过对于C++而言相对java还是有很多不同点的,所以全面复习一下. 类 C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程 ...

  5. python_json模块和pickle模块

    json 优点:所有语言通用:缺点:只能序列化基本的数据类型list/dict/int... json格式中,字符串必须是双引号,字符都是小写. 序列化: import json v = [12,3, ...

  6. 四、vue基础--自定义组件

    1.语法:Vue.component("组件名字",{data,template}),代码如下: a. data: 必须是一个函数,有一个返回值.和vue里面的使用方法一样 b. ...

  7. vue开发记录

    vue开发过程中遇到的一些小问题.小技巧等,会不断更新~ 记录不详细处,欢迎留言

  8. python自动华 (四)

    Python自动化 [第四篇]:Python基础-装饰器 生成器 迭代器 Json & pickle 目录: 装饰器 生成器 迭代器 Json & pickle 数据序列化 软件目录结 ...

  9. 在使用Telnet连接localhost时所遇到的问题:出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件

    1.出现 ‘telnet’ 不是内部或外部命令,也不是可运行的程序或批处理文件.原因:因为本机的Telnet客户端默认是关闭的,所以我们要手动打开解决方案:打开控制面板–>程序–>打开或关 ...

  10. Java进阶知识21 Spring的AOP编程

    1.概述 Aop:(Aspect Oriented Programming)面向切面编程          功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...