【发布时间】:2019-07-03 03:35:32
【问题描述】:
我正在尝试提取包含特定关键字及其地理位置的所有推文。
例如,我想从 'france' 和 'singapore' 下载所有包含关键字 'iphone' 的英文推文
我的代码
import tweepy
import csv
import pandas as pd
import sys
# API credentials here
consumer_key = 'INSERT CONSUMER KEY HERE'
consumer_secret = 'INSERT CONSUMER SECRET HERE'
access_token = 'INSERT ACCESS TOKEN HERE'
access_token_secret = 'INSERT ACCESS TOKEN SECRET HERE'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
# Search word/hashtag value
HashValue = ""
# search start date value. the search will start from this date to the current date.
StartDate = ""
# getting the search word/hashtag and date range from user
HashValue = input("Enter the hashtag you want the tweets to be downloaded for: ")
StartDate = input("Enter the start date in this format yyyy-mm-dd: ")
# Open/Create a file to append data
csvFile = open(HashValue+'.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,q=HashValue,count=20,lang="en",since=StartDate, tweet_mode='extended').items():
print (tweet.created_at, tweet.full_text)
csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8')])
print ("Scraping finished and saved to "+HashValue+".csv")
#sys.exit()
如何做到这一点。
【问题讨论】:
-
你试过这个问题给出的解决方案了吗?stackoverflow.com/questions/17633378/…
-
@RegiMathew,这就是从特定位置提取推文。我的问题是推文来自哪个位置
-
您愿意接受使用其他方法/库的解决方案吗?或者它必须只在 tweepy 中
-
@Zulfiqaar,是的。我对其他方法持开放态度
-
试试 twitterscraper 模块,query_tweets 是我常用的
标签: python python-3.x pandas tweepy