Skip to Main Content
Python程序设计:人工智能案例实践
book

Python程序设计:人工智能案例实践

by 保罗 戴特尔, 哈维 戴特尔
August 2021
Intermediate to advanced content levelIntermediate to advanced
450 pages
13h 28m
Chinese
Pearson
Content preview from Python程序设计:人工智能案例实践

16.4.2 将推文存入MongoDB中

首先,我们将展示一个连接到MongoDB数据库的交互式IPython会话,通过Twitter流媒体下载当前的推文,并按推文数量汇总排名前10的参议员。接下来,类TweetListener处理传入的推文并将它们的JSON数据存储在MongoDB中。最后,将通过创建一个交互式Folium地图来继续IPython会话,该地图用于显示我们存储的推文信息。

使用Tweepy通过Twitter进行身份验证

首先,使用Tweepy通过Twitter进行身份验证:

接下来,配置Tweepy API对象,等待应用程序达到Twitter的速率限制:

加载参议员的数据

将使用文件senators.csv(位于ch16示例文件夹的TwitterMongoDB子文件夹中)中的信息跟踪每个美国参议员的推文,文件中包含参议员的双字母州代码、姓名、党派、Twitter句柄和Twitter ID。

Twitter允许通过其Twitter数值ID查询特定用户,但必须提交这些数值的字符串表示。所以,先将senators.csv加载到pandas中,将Twitter ID值转换为字符串(使用序列化方法astype转换类型),显示几行数据。这种情况下设置显示的最大列数为6

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Python算法交易实战

Python算法交易实战

Posts & Telecom Press, Sebastien Donadio
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111678458