写Tweety(python模块)搜索结果到CSV [英] Write Tweety (python module)search results To CSV
问题描述
这里是我使用的简单代码:
Here is the simple code I'm using:
import tweepy
result = tweepy.api.search(q="McDonalds ",rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" )
for tweet in result:
print tweet.text
print tweet.geo
它返回我想要的结果。在我的IDE中,我得到这样的结果:
It returns the results I want. In my IDE I get results like this:
@stevo_k14 I'm going back after mcdonalds haha
None
"@FunnyEvil: If bars can't serve drunk people, Mcdonalds shouldn't be able to serve fat people."
{u'type': u'Point', u'coordinates': [33.9581, -118.1779]}
Mcdonalds for breakfast? Yup i think so :)
None
@JessicaCarrillo You make me want to go out to McDonalds right now!!!!! Lol good morning.
{u'type': u'Point', u'coordinates': [33.9443, -118.0038]}
一些返回没有位置和一些返回位置。我真正想做的是将结果直接写入一个csv文件,关键字,Tweet,Lat,Lon作为我的标题。在这种情况下,关键字将是McDonalds。这也是很好的有一些ifelse语句不写结果,除非他们有位置。
Some return no location and some return location. What I'd really like to do is write the results straight to a csv file with Keyword, Tweet, Lat, Lon as my headers. Keyword would be "McDonalds" in this case. It would also be nice to have some ifelse statement to not write results unless they have location.
我是新编程,所以你的帮助是非常感谢!感谢
I am new to programming so your help is much appreciated! Thanks
推荐答案
您可以使用csv模块并对现有的tweet进行迭代。
You can just use the csv module and iterate over your existing tweet.
import tweepy
import csv
keyword = 'McDonalds '
result = tweepy.api.search(q=keyword,rpp=1000,page=2, geocode= "34.085422,-117.900879,100mi" )
with open('some.csv', 'w') as acsv:
w = csv.writer(acsv)
w.writerow(('Keyword', 'Tweet', 'Lat', 'Lon'))
for tweet in result:
lat, lon = tweet.geo if tweet.geo else ('', '')
w.writerow((keyword, tweet.text, lat, lon))
这篇关于写Tweety(python模块)搜索结果到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!