【402】Twitter Data Collection
参考: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的更多相关文章
- 【DataGuard】部署Data Guard相关参数详解 (转载)
原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler 有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Gua ...
- LeetCode:移除K位数字【402】
LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...
- 【机器学习】Iris Data Set(鸢尾花数据集)
[机器学习]Iris Data Set(鸢尾花数据集) 注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数 ...
- 【转】char data[0]用法总结
@2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...
- 【原创】Mapped Statements collection does not contain value for DaoImpl.method
问题: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Pers ...
- 【转】Hive Data Manipulation Language
Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Syn ...
- 【WinForm】“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常,无法识别的配置节 system.serviceModel
出现问题的原因: 在本机上没有出现问题,让一个同事测试的时候,在另外一台电脑上出现连接数据库失败,系统不能打开的问题 在网上搜了一下,有说是数据库连接字符串错误的,有说app.config文件配置不匹 ...
- 【leetcode】352. Data Stream as Disjoint Intervals
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
- 【原创】System.Data.SQLite内存数据库模式
对于很多嵌入式数据库来说都有对于的内存数据库模式,SQLite也不例外.内存数据库常常用于极速.实时的场景,一个很好的应用的场景是富客户端的缓存数据,一般富客户端的缓存常常需要分为落地和非落地两种,而 ...
随机推荐
- 目标检测 — two-stage检测
目前主流的目标检测算法主要是基于深度学习模型,其可以分成两大类:two-stage检测算法:one-stage检测算法.本文主要介绍第一类检测算法,第二类在下一篇博文中介绍. 目标检测模型的主要性能指 ...
- 面对runc逃逸漏洞,华为云容器为您保驾护航
背景信息 基于runc运行时的容器存在安全漏洞,攻击者可以通过恶意容器镜像等方式获取宿主机root执行权限.漏洞CVE-2019-5736的详细信息,请参见 https://cve.mitre.org ...
- SQL server 语句执行分析
- Annoying Present
http://codeforces.com/group/1EzrFFyOc0/contest/1009/problem/C 题意:原本有一个n个0的数组a[],你对它进行m次操作,每次操作让a[j]+ ...
- 计算广告(4)----搜索广告召回(也叫match、触发)
一.搜索广告形态 1.特征工程 特征主要有用户画像(user profile).用户行为(user behavior).广告(ad)和上下文(context)四部分组成,如下所示: 2.平台算法主要分 ...
- 从c到c++<四>
总结一下:内联函数实际上就是用inline修饰的函数,这些函数会在编译时由编译器来将代码展开,而不用像上面第二点提到的人工展开,它的使用场景:代码很短.使用频率高. 具体代码如下: 对于这两者实际上还 ...
- GAN学习指南:从原理入门到制作生成Demo,总共分几步?
来源:https://www.leiphone.com/news/201701/yZvIqK8VbxoYejLl.html?viewType=weixin 导语:本文介绍下GAN和DCGAN的原理,以 ...
- centos 7.3 开放端口并对外开放
查看已打开的端口 # netstat -anp 查看想开的端口是否已开 # firewall-cmd --query-port=666/tcp 若此提示 FirewallD is not runnin ...
- VSCode:配置自动修复eslint
{ //"tfvc.location": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Profess ...
- 021_STM32程序移植之_ESP8266连接onenet
本次教程是使用STM32C8T6通过ESP8266-12F模块将数据传输到ONENET云端去,并且云端能够下发命令给单片机来实现云端控制.本次实验硬件设备:STM32C8T6最小系统,ESP8266- ...