2017-02-11 137 views
2

我在Mac上使用Spyder,Spyder上的Python版本是2.7。幾個月前,我一直在使用下面的代碼來刮掉推文,但現在我發現它不再有效。首先,我可以不再使用:TypeError:file()至多需要3個參數(給出4個參數)

from urllib.request import url open 

現在使用

from urllib2 import url open 

但是,我不能運行,下面的代碼,並出現以下錯誤:「打開(「%s_tweets.csv '%SCREEN_NAME, 'W',換行符='」,編碼= 'UTF-8-SIG')爲f:類型錯誤:文件()採用至多3個參數(4給出)」

import sys 
from urllib2 import urlopen 

default_encoding = 'utf-8' 

import tweepy #https://github.com/tweepy/tweepy 
import csv 

#Twitter API credentials 
consumer_key = "" 
consumer_secret = "" 
access_key = "" 
access_secret = "" 

screenNamesList = [] 

def redirect(url): 
page = urlopen(url) 
return page.geturl() 

def get_all_tweets(screen_name): 
#Twitter only allows access to a users most recent 3240 tweets with this method 

#authorize twitter, initialize tweepy 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_key, access_secret) 
api = tweepy.API(auth, wait_on_rate_limit = True) 

#initialize a list to hold all the tweepy Tweets 
alltweets = [] 

#make initial request for most recent tweets (200 is the maximum allowed count) 
new_tweets = api.user_timeline(screen_name = screen_name,count=200) 

#save most recent tweets 
alltweets.extend(new_tweets) 

#save the id of the oldest tweet less one 
oldest = alltweets[-1].id - 1 

#keep grabbing tweets until there are no tweets left to grab 
while len(new_tweets) > 0: 
    #print "getting tweets before %s" % (oldest) 

    #all subsiquent requests use the max_id param to prevent duplicates 
    new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest) 

    #save most recent tweets 
    alltweets.extend(new_tweets) 

    #update the id of the oldest tweet less one 
    oldest = alltweets[-1].id - 1 

    #print "...%s tweets downloaded so far" % (len(alltweets)) 

#transform the tweepy tweets into a 2D array that will populate the csv 
outtweets = [[tweet.id_str, tweet.created_at, tweet.text, tweet.retweet_count, tweet.coordinates, tweet.favorite_count, tweet.author.followers_count, tweet.author.description, tweet.author.location, tweet.author.name] for tweet in alltweets] 

#write the csv 

with open('%s_tweets.csv' % screen_name, 'w', newline='', encoding='utf-8-sig') as f: 
    writer = csv.writer(f) 
    writer.writerow(["id", "created_at", "text", "retweet_count", "coordinates", "favorite_count", "followers_count", "description", "location", "name"]) 
    writer.writerows(outtweets) 

pass 


if __name__ == '__main__': 
#pass in the username of the account you want to download 
for i, user in enumerate(screenNamesList): 
    get_all_tweets(screenNamesList[i]) 
    i+=1 

回答

5

此代碼是針對python 3的,其中open獲取新參數:

  • 編碼
  • 換行符

在Python 2,只有3個參數的可能:

open(name[, mode[, buffering]])

buffering是不是你想要的。其他人無處可尋。

您可以通過使用

with open('%s_tweets.csv' % screen_name, 'wb') as f: 

打開二進制補丁手柄csv模塊的「空行」錯誤解決這一點。使用python 3(只有「舊」版本,最新版本不需要),你必須通過newline="",因爲你不能以二進制格式打開一個csv文件。

爲了處理編碼和換行符,你可以只是做描述here

from io import open 

,並留下您的代碼保持不變的其餘部分。好吧,你幾乎必須爲你的標題添加前綴unicode,如下所示:

writer.writerow([u"id", u"created_at", u"text", u"retweet_count", u"coordinates", u"favorite_count", u"followers_count", u"description", u"location", u"name"]) 
+0

非常好,謝謝 - 它有效。但是,我能做些什麼來在某處添加「encoding ='utf-8-sig'」。或者,我應該嘗試在Python 3中運行Spyder嗎? – bayrah

+0

檢查我的編輯。我認爲你可以用'io'模塊來處理python 2.7。 –

+0

謝謝。它給出了錯誤:write()參數1必須是unicode,而不是str之後的「writer.writerow([」id「,」created_at「,」text「,」retweet_count「,」coordinates「,」favorite_count「,」followers_count「 ,「description」,「location」,「name」]) 「 – bayrah

相關問題