python - 对于列表中的每个趋势,使用 python-twitter 提取 1000 条最近的推文(并删除/排除转发)
问题描述
使用 python-twitter 库(https://github.com/bear/python-twitter)我正在尝试使用 yahoo Where on earth ID 检索 1000 条最新推文,了解某个位置的前 10 大趋势,但似乎无法弄清楚如何从列表中的趋势中检索推文。
我不知道我是否遗漏了文档中的某些内容,并且在网上找不到任何关于如何从返回的列表中获取特定于趋势的推文以及如何删除转推的示例。
import twitter
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
ACCESS_TOKEN = 'XXXX'
ACCESS_TOKEN_SECRET = 'XXXX'
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
# Define the location id for the UK
WOEID = 23424975
# Define language of tweets
LANG = "en"
# Define type of tweets we are after
TWEETS_TYPE = "recent"
# Define max number of tweets per trend
MAX_STATUSES = 1000
# API config
# API with request rate limited
api = twitter.Api(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
sleep_on_rate_limit=True)
print(api.VerifyCredentials())
# Query the Twitter API for the current top 10 trends in the UK.
uk_trends = api.GetTrendsWoeid(WOEID)
print(uk_trends)
# Return the 1000 most recent tweets for each trend
# This is where I want to retrieve the tweets per trend
for trend in uk_trends:
count = MAX_STATUSES
trend = trend
search_results = api.GetSearch(term=trend, count=count)
print(search_results)
我应该改用 twitter API 本身吗?
解决方案
我能够通过访问 Trend() 对象的 name 属性,将其解析为字符串并遍历代码中返回的结果来解决问题,如下所示:
```
import os
import json
import csv
import twitter
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
import matplotlib.pyplot as plt
ACCESS_TOKEN = 'XXXX'
ACCESS_TOKEN_SECRET = 'XXXX'
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
# Define the location id for the UK
# With Yahoo Where on Earth ID
WOEID = 23424975
# Define language of tweets
LANG = "en"
# Define type of tweets we are after
TWEETS_TYPE = "recent"
# Define max number of tweets per trend
MAX_STATUSES = 10
# API configuration
# API with request rate limited
api = twitter.Api(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
sleep_on_rate_limit=True)
# Check twitter account api details are correct
# print(api.VerifyCredentials())
# Query the Twitter API for the current top 10 trends in the UK.
uk_trends = api.GetTrendsWoeid(WOEID)
print(uk_trends)
# Return the 1000 most recent tweets for each trend
for trend in uk_trends:
'''
Extract name of each trend returned by Trend model
and search required count value of recent tweets per trend
'''
trend = str(trend.name)
count = MAX_STATUSES
search_results = api.GetSearch(term=trend, count=count)
print(search_results)
```
推荐阅读
- python - 我可以使用 Sympy 推导出电报方程吗?
- tensorflow - TensorFlow Hub 模块重用
- python - 如何在 django python 的管理页面中添加一个类?
- mpeg2-ts - mpeg-2 ts PCR 33 位 90 kHz 基础和 9 位 27 MHz 扩展位......这是什么意思?
- angular - 为什么要将“哑”组件提取到 index.ts ngModule 中?
- php - 处理不同的输入日期格式以插入 MySQL
- spring - 从spring创建postgres数据库时关系不存在
- forms - Laravel 集体自定义表单,动作 Controller@update 未定义
- c# - SQL Server(本地数据库)的连接字符串
- appium - com.google.common.collect.Maps$TransformedEntriesMap 无法转换为 org.openqa.selenium.WebElement