数据挖掘 , 编程 (Data Mining, Programming)

Getting Twitter data

获取Twitter数据

Let’s use the Tweepy package in python instead of handling the Twitter API directly. The two things we will do with the package are, authorize ourselves to use the API and then use the cursor to access the twitter search APIs.

让我们在python中使用Tweepy包,而不是直接处理Twitter API。 我们将对该软件包执行的两件事是,授权自己使用API​​,然后使用光标访问twitter搜索API。

Let’s go ahead and get our imports loaded.

让我们继续加载我们的导入。

import tweepyimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport numpy as npsns.set()%matplotlib inline

Twitter授权 (Twitter authorization)

To use the Twitter API, you must first register to get an API key. To get Tweepy just install it via pip install Tweepy. The Tweepy documentation is best at explaining how to authenticate, but I’ll go over the basic steps.

要使用Twitter API,您必须首先注册以获得API密钥。 要获取Tweepy,只需通过pip安装Tweepy即可安装。 Tweepy文档最擅长于说明如何进行身份验证,但我将介绍一些基本步骤。

Once you register your app you will receive API keys, next use Tweepy to get an OAuthHandler. I have the keys stored in a separate config dict.

一旦注册您的应用程序,您将收到API密钥,接下来请使用Tweepy获取OAuthHandler。 我将密钥存储在单独的配置字典中。

config = {"twitterConsumerKey":"XXXX", "twitterConsumerSecretKey" :"XXXX"} auth = tweepy.OAuthHandler(config["twitterConsumerKey"], config["twitterConsumerSecretKey"]) redirect_url = auth.get_authorization_url() redirect_url

Now that we’ve given Tweepy our keys to generate an OAuthHandler, we will now use the handler to get a redirect URL. Go to the URL from the output in a browser where you can allow your app to authorize your account so you can get access to the API.

现在,我们已经为Tweepy提供了密钥来生成OAuthHandler,现在将使用该处理程序来获取重定向URL。 在浏览器中从输出转到URL,您可以在其中允许您的应用对帐户进行授权,以便可以访问API。

Once you’ve authorized your account with the app, you’ll be given a PIN. Use that number in Tweepy to let it know that you’ve authorized it with the API.

使用该应用授权您的帐户后,将获得PIN码。 在Tweepy中使用该编号,以使其知道您已使用API​​授权。

pin = "XXXX"auth.get_access_token(pin)

搜索推文 (Searching for tweets)

After getting the authorization, we can use it to search for all the tweets containing the term “British Airways”; we have restricted the maximum results to 1000.

获得授权后,我们可以使用它来搜索包含“英国航空”一词的所有推文; 我们已将最大结果限制为1000。

query = 'British Airways'max_tweets = 10searched_tweets = [status for status in tweepy.Cursor(api.search, q=query,tweet_mode='extended').items(max_tweets)]search_dict = {"text": [], "author": [], "created_date": []}for item in searched_tweets:    if not item.retweet or "RT" not in item.full_text:        search_dict["text"].append(item.full_text)        search_dict["author"].append(item.author.name)        search_dict["created_date"].append(item.created_at)df = pd.DataFrame.from_dict(search_dict)df.head()#     text                                                author      created_date0   @RwandAnFlyer @KenyanAviation @KenyaAirways @U...   Bkoskey     2019-03-06 10:06:141   @PaulCol56316861 Hi Paul, I'm sorry we can't c...   British Airways     2019-03-06 10:06:092   @AmericanAir @British_Airways do you agree wit...   Hat     2019-03-06 10:05:383   @Hi_Im_AlexJ Hi Alex, I'm glad you've managed ...   British Airways     2019-03-06 10:02:584   @ZRHworker @British_Airways @Schmidy_87 @zrh_a...   Stefan Paetow   2019-03-06 10:02:33

语言检测 (Language detection)

The tweets downloaded by the code above can be in any language, and before we use this data for further text mining, we should classify it by performing language detection.

上面的代码下载的推文可以使用任何语言,并且在我们使用此数据进行进一步的文本挖掘之前,我们应该通过执行语言检测对其进行分类。

In general, language detection is performed by a pre-trained text classifier based on either the Naive Bayes algorithm or more modern neural networks. Google’s compact language detector library is an excellent choice for production-level workloads where you have to analyze hundreds of thousands of documents in less than a few minutes. However, it’s a bit tricky to set up and as a result, a lot of people rely on calling a language detection API from third-party providers like Algorithmia which are free to use for hundreds of calls a month (free sign up required with no credit cards needed).

通常,语言检测由基于Naive Bayes算法或更现代的神经网络的预训练文本分类器执行。 Google的紧凑型语言检测器库是生产级工作负载的绝佳选择,您必须在几分钟之内分析成千上万的文档。 但是,设置起来有点棘手,因此,许多人依赖于从第三方提供商(例如Algorithmia)调用语言检测API ,这些提供商每月可以免费使用数百次呼叫(无需注册即可免费注册)需要信用卡)。

Let’s keep things simple in this example and just use a Python library called Langid which is orders of magnitude slower than the options discussed above but should be OK for us in this example since we are only to analyze about a hundred tweets.

让我们在此示例中保持简单,只使用一个名为Langid的Python库,该库比上面讨论的选项慢几个数量级,但在本示例中应该可以接受,因为我们仅分析大约100条推文。

from langid.langid import LanguageIdentifier, modeldef get_lang(document):    identifier = LanguageIdentifier.from_modelstring(model, norm_probs=True)    prob_tuple = identifier.classify(document)    return prob_tuple[0]df["language"] = df["text"].apply(get_lang)

We find that there are tweets in four unique languages present in the output, and only 45 out of 100 tweets are in English, which are filtered as shown below.

我们发现输出中存在四种独特语言的推文,而100条推文中只有45条是英文,如下所示进行过滤。

print(df["language"].unique())df_filtered = df[df["language"]=="en"]print(df_filtered.shape)#Out:array(['en', 'rw', 'nl', 'es'], dtype=object)(45, 4)

获得情绪来为推特打分 (Getting sentiments to score for tweets)

We can take df_filtered created in the preceding section and run it through a pre-trained sentiments analysis library. For illustration purposes we are using the one present in Textblob, however, I would highly recommend using a more accurate sentiments model such as those in coreNLP or train your own model using Sklearn or Keras.

我们可以采用在上一节中创建的df_filtered并将其通过预训练的情感分析库运行。 为了便于说明,我们使用Textblob中提供的模型,但是,我强烈建议使用更准确的情感模型(例如coreNLP中的模型),或者使用Sklearn或Keras训练自己的模型。

Alternately, if you choose to go via the API route, then there is a pretty good sentiments API at Algorithmia.

或者,如果您选择通过API路线,那么Algorithmia中会有一个相当不错的情绪API 。

from textblob import TextBlobdef get_sentiments(text):    blob = TextBlob(text)#     sent_dict = {}#     sent_dict["polarity"] = blob.sentiment.polarity#     sent_dict["subjectivity"] = blob.sentiment.subjectivity

    if blob.sentiment.polarity > 0.1:        return 'positive'    elif blob.sentiment.polarity < -0.1:        return 'negative'    else:        return 'neutral'def get_sentiments_score(text):    blob = TextBlob(text)    return blob.sentiment.polarity

df_filtered["sentiments"]=df_filtered["text"].apply(get_sentiments)df_filtered["sentiments_score"]=df_filtered["text"].apply(get_sentiments_score)df_filtered.head()#Out:    text                                                author          created_date    language    sentiments  sentiments_score0   @British_Airways Having some trouble with our ...   Rosie Smith     2019-03-06 10:24:57     en  neutral     0.0251   @djban001 This doesn't sound good, Daniel. Hav...   British Airways     2019-03-06 10:24:45     en  positive    0.5502   First #British Airways Flight to #Pakistan Wil...   Developing Pakistan     2019-03-06 10:24:43     en  positive    0.1503   I don’t know why he’s not happy. I thought he ...   Joyce Stevenson     2019-03-06 10:24:18     en  negative    -0.2004   Fancy winning a global holiday for you and a f...   Selective Travel Mgt 												

在Python中使用Twitter Rest API批量搜索和下载推文相关推荐

  1. python 微信bot_使用Tweepy在Python中创建Twitter Bot

    python 微信bot by Lucas Kohorst 卢卡斯·科斯特(Lucas Kohorst) 使用Tweepy在Python中创建Twitter Bot (Create a Twitter ...

  2. python调用simulink_[Python-MATLAB] 在Python中调用MATLAB的API

    可以参考官方的说明文档: MATLAB Engine API的使用文档: 原材料: 1.MATLAB 2015a  32位的 2.Python 2.7.13    32位的 安装: 1.运行cmd,切 ...

  3. 利用Python调用ECMWF欧洲中心API批量下载数据

    前段时间由于需要下载ECMWF(欧洲中期天气预报中心)的再分析数据,学习了如何利用Python调用ECMWF欧洲中心API进行批量下载.这种下载ECMWF数据的方法在官网上有非常详细的介绍.我只是对这 ...

  4. Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置

    1 任务需求   首先,我们来明确一下本文所需实现的需求.   现有通过这篇博客(https://blog.csdn.net/zhebushibiaoshifu/article/details/123 ...

  5. 图片抓取_小小爬虫批量抓取微信推文里的图片

    哈喽,大家好,今天给大家分享一个特别特别小的爬虫案例! 爬取微信推文中的图片!!!! 有人说,这有啥用,,,,万一人家推文是放的是以图片的方式放的某个PPT的内容呢,你想把它弄下来,咋整,就是爬取啦. ...

  6. 推特开发者账号申请失败【推特开发者文档系列7】——通过API接口发布、检索推文

    文章转自:https://www.jianshu.com/p/2c208994ff9a 本系列是对推特开发者文档进行的翻译,以便帮助开发人员使用API接口,难免有些地方存在不足,还请谅解. 关于如何获 ...

  7. 爬虫取中间文本_小小爬虫批量抓取微信推文里的图片

    哈喽,大家好,今天给大家分享一个特别特别小的爬虫案例! 爬取微信推文中的图片!!!! 有人说,这有啥用,,,,万一人家推文是放的是以图片的方式放的某个PPT的内容呢,你想把它弄下来,咋整,就是爬取啦. ...

  8. 大制作,1500多行python代码实现各大平台音乐搜索,下载,收听

    python实现各大平台音乐搜索,下载,收听 先上效果图 各大平台的歌随便搜,随便听! 打包后软件地址:https://wws.lanzoui.com/iosS7rlgzmb 密码: 2gm4 (最好 ...

  9. python爬虫入门:在命令行搜索并下载小说

    文章目录 前言 一.生成小说章节目录 1.具体流程 2.效果演示 二.小说下载 1.具体流程 2.效果演示 总结 前言 本篇文章以笔趣阁为例,链接:https://www.biquge7.com,实现 ...

最新文章

  1. 镁光ssd管理工具 linux,在 SSD 上使用 Btrfs 文件系统的相关优化
  2. LilyPad Arduino可穿戴技术和电子织物控制器板简介
  3. OpenCV椭圆拟合ellipse fitting的实例(附完整代码)
  4. Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?
  5. MSP430F5529 DriverLib 库函数学习笔记(十五)SFR 模块
  6. 在Vue中使用JSX,很easy的
  7. 截取json字符串算法
  8. Hibernate之事务处理
  9. 升级macOS Big Sur 无法开机/死机怎么办?
  10. 百度统计挂了,分布式数据库异常引起,数据显示为空!
  11. foobar 2000|foobar2000中文版32/64位下载 v1.3.16
  12. 外贸常用术语_常见国际贸易专业术语有哪些?
  13. 鸿蒙太空是什么意思,“我所居兮,青埂之峰;我所游兮,鸿蒙太空。谁与我逝兮,吾谁与从?渺渺茫茫兮,归彼大荒”的意思...
  14. 英语3500词(八)treat kids as adults主题(2022.1.20)
  15. < Linux > 进程间通信
  16. VC雕虫小技集(四)
  17. Android网页广告植入规避方案
  18. Linux 巡检脚本大全
  19. 【20180808】集训题d3
  20. 2 万字 + 20张图| 细说 Redis 九种数据类型和应用场景

热门文章

  1. 【C语言】str类与men库函数的实现(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)
  2. 常量指针与指针常量的区别(转帖)
  3. 从0到1写RT-Thread内核——临界段的保护
  4. Linux的帧缓冲设备
  5. 三面美团Java岗,尚学堂java马士兵全套
  6. 网易资深Java架构师:疫情对java行业的影响分析
  7. [十二省联考2019]皮配
  8. Mapreduce中maptask过程详解
  9. jQuery表单校验
  10. 51nod 1100:斜率最大