[英]How to save results to csv using python scraper?
我发现此python代码通过自定义搜索查询来刮擦Twitter:
https://github.com/tomkdickinson/Twitter-Search-API-Python/blob/master/TwitterScraper.py
我想将这段代码的结果存储到一个csv文件中。
我尝试在for循环的第245行附近添加csv编写器,该循环根据我的搜索查询打印出tweet,但csv文件的结果为空白
def save_tweets(self, tweets):
"""
Just prints out tweets
:return: True always
"""
for tweet in tweets:
# Lets add a counter so we only collect a max number of tweets
self.counter += 1
if tweet['created_at'] is not None:
t = datetime.datetime.fromtimestamp((tweet['created_at']/1000))
fmt = "%Y-%m-%d %H:%M:%S"
myCsvRow = log.info("%i [%s] - %s" % (self.counter, t.strftime(fmt), tweet['text']))
fd = open('document.csv','a')
fd.write(myCsvRow)
fd.close()
return True
另外,在第170行附近的代码中有一条注释提到:
@abstractmethod
def save_tweets(self, tweets):
"""
An abstract method that's called with a list of tweets.
When implementing this class, you can do whatever you want with these tweets.
"""
如何使用此类保存推文?
您的问题似乎是这样的:
myCsvRow = log.info("%i [%s] - %s" % (self.counter, t.strftime(fmt), tweet['text']))
查看您正在使用的GitHub页面上的代码,我可以看到log
是一个python记录器。 log.info
的目的是将给定的字符串写在某个地方(例如:控制台,文件或这些位置或其他位置的任意组合)。 它不返回值,因此myCsvRow
将为空。
您想要的是更有可能的:
myCsvRow = "%i [%s] - %s" % (self.counter, t.strftime(fmt), tweet['text'])
虽然,有一些注意事项:
(1)您不在逗号之间插入逗号,这在CSV中很常见(CSV =逗号分隔值),并且
(2)当您的一个字段是一个可能包含逗号的文本字段时,尝试写一个csv行实际上有点冒险。 如果您只是天真地按原样写出文本,则推文中的逗号本身会导致任何解释CSV的人都认为该行中还有其他CSV字段。 幸运的是,python带有一个csv库,它将帮助您避免此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.