繁体   English   中英

TypeError:file()最多需要3个参数(给定4个参数)

[英]TypeError: file() takes at most 3 arguments (4 given)

我在Mac上使用Spyder,Spyder上的Python版本是2.7。 几个月前我一直在使用以下代码来抓取推文,但现在我发现它已经不再适用了。 首先,我再也不能使用:

from urllib.request import url open

现在用

from urllib2 import url open

但是,我无法运行下面的代码并得到以下错误:“open with(%s_tweets.csv'%screen_name,'w',newline ='',encoding ='utf-8-sig')as f :TypeError:file()最多需要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

此代码适用于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文件作为二进制文件。

要处理编码和换行,您可以按照此处的描述进行操作:

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"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM