我正在尝试将用户和推文数据放入两个单独的文件中,但这显示了权限错误。同样,保存的文本(名称,screen_name,文本)中的字段也无法正确解码。例如:铁包的拉里– 我该如何处理这些字段的解码?
with open('users.csv', 'wb') as file1, open('tweets','wb') as file2:
writer1 = unicodecsv.writer(file1, delimiter = ',', quotechar = '"')
writer1.writerow(["id","id_str","name","screen_name","location","profile_location","url","description",
"protected","verified","statuses_count","friends_count","followers_count","listed_count",
"favourites_count","created_at","utc_offset","time_zone","geo_enabled","lang","profile_image_url_https",
"default_profile","default_profile_image","contributors_enabled","is_translator","is_translation_enabled",
"profile_background_color","profile_background_image_url","profile_background_image_url_https",
"profile_background_tile","profile_image_url","profile_link_color","profile_sidebar_border_color",
"profile_sidebar_fill_color","profile_text_color","profile_use_background_image","has_extended_profile",
"following","follow_request_sent","notifications","translator_type","crawled_at"])
writer2 = unicodecsv.writer(file2, delimiter = ',', quotechar = '"')
writer2.writerow(["user_id","created_at","crawled_at","id","id_str","text","source","truncated","in_reply_to_status_id",
"in_reply_to_status_id_str","in_reply_to_user_id","in_reply_to_user_id_str","in_reply_to_screen_name",
"coordinates","place","retweet_count","favorite_count","favorited","retweeted","lang",
"num_hashtags","num_mentions","num_urls"])
for i in ids:
try:
user = api.get_user(i)
writer1.writerow([user.id,user.id_str,user.name,user.screen_name.decode(),
user.location,user.profile_location,user.url,user.description,
user.protected,user.verified,user.statuses_count,user.friends_count,user.followers_count,
user.listed_count,user.favourites_count,user.created_at,user.utc_offset,user.time_zone,
user.geo_enabled,user.lang,user.profile_image_url_https,user.default_profile,
user.default_profile_image,user.contributors_enabled,user.is_translator,
user.is_translation_enabled,user.profile_background_color,user.profile_background_image_url,
user.profile_background_image_url_https,user.profile_background_tile,user.profile_image_url,
user.profile_link_color,user.profile_sidebar_border_color,user.profile_sidebar_fill_color,
user.profile_text_color,user.profile_use_background_image,user.has_extended_profile,
user.following,user.follow_request_sent,user.notifications,user.translator_type,datetime.now()])
for tweet in Cursor(api.user_timeline, screen_name = i).items():
writer2.writerow([tweet.user.id,tweet.created_at,datetime.now(),tweet.id,tweet.id_str,tweet.text,tweet.source,
tweet.truncated,tweet.in_reply_to_status_id,tweet.in_reply_to_status_id_str,
tweet.in_reply_to_user_id,tweet.in_reply_to_user_id_str,tweet.in_reply_to_screen_name,
tweet.coordinates,tweet.place,tweet.retweet_count,tweet.favorite_count,tweet.favorited,
tweet.retweeted,tweet.lang,len(tweet.entities.get('hashtags')),
len(tweet.entities.get('user_mentions')),len(tweet.entities.get('urls'))])
except tweepy.error.TweepError:
pass
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<ipython-input-23-8e102a30d91e> in <module>
36 tweet.retweeted,tweet.lang,len(tweet.entities.get('hashtags')),
---> 37 len(tweet.entities.get('user_mentions')),len(tweet.entities.get('urls'))])
38 except tweepy.error.TweepError:
~\Anaconda3\lib\site-packages\unicodecsv\py3.py in writerow(self, row)
27 def writerow(self, row):
---> 28 return self.writer.writerow(row)
29
~\Anaconda3\lib\site-packages\unicodecsv\py3.py in write(self, string)
14 def write(self, string):
---> 15 return self.binary.write(string.encode(self.encoding, self.errors))
16
PermissionError: [Errno 13] Permission denied
During handling of the above exception, another exception occurred:
PermissionError Traceback (most recent call last)
<ipython-input-23-8e102a30d91e> in <module>
37 len(tweet.entities.get('user_mentions')),len(tweet.entities.get('urls'))])
38 except tweepy.error.TweepError:
---> 39 pass
PermissionError: [Errno 13] Permission denied