我尝试使用Twitter机器人检测工具中的开放API' BotorNot'扫描,如果CSV文件中列出的Twitter帐户是机器人或人类。我在Python中编写了一个基于旧脚本的脚本,该脚本应该读取并扫描文件中的作者屏幕名称并将结果保存在新文件中。
import botornot
import pandas as pd
import tweepy
#twitter authentification
twitter_app_auth = {
'consumer_key': '***',
'consumer_secret': '***',
'access_token': '***',
'access_token_secret': '***',
}
bon = botornot.BotOrNot(**twitter_app_auth)
#REPLACE lines to skip and number of rows to check
skip = 1
number = 20
#read twitter dataset
df = pd.read_csv('C:/Users/lenna/sciebo/Uni/Master/Masterarbeit/Tweets/accounts2check.csv', encoding= 'ISO-8859-1', usecols=['Index', 'Author screen name'], skiprows=range(1, skip), nrows=number)
start = df.iloc[0]['Index']
df_length = df.count
last_row = df.tail(1)
end = last_row.iloc[0]['Index']
print('Start: ')
print(start)
print('End: ')
print(end)
print()
print(df)
#drop duplicates so accounts are not checked twice
#df = df.drop_duplicates(subset='Author screen name', keep='first')
#print(df)
#create Dataframe that will be filled with BotOrNot results later
columns = ['Index', 'ID', 'Screen Name', 'Score', 'Content', 'Friends', 'Language', 'Network', 'Sentiment', 'Temporal', 'User']
score_df = pd.DataFrame(columns=columns)
i = 0
#check accounts with BotOrNot?
for row in df['Author screen name']:
try:
print('')
print("BotOrNot is currently checking " + row)
print(i)
print('/')
print(number)
result = bon.check_account(row)
print(result)
print('')
#create Dataframe from result
bon_df = pd.DataFrame(result)
print(bon_df)
print('')
#get and reorganize cells from bon_df
index = df.iloc[i]['Index']
id = bon_df.iloc[8]['meta']
name = bon_df.iloc[4]['meta']
score = bon_df.iloc[4]['score']
content = bon_df.iloc[0]['categories']
friends = bon_df.iloc[1]['categories']
language = bon_df.iloc[2]['categories']
network = bon_df.iloc[3]['categories']
sentiment = bon_df.iloc[5]['categories']
temporal = bon_df.iloc[6]['categories']
user = bon_df.iloc[7]['categories']
print('')
#add result to score_df
score_df.loc[i] = pd.Series({'Index':index, 'ID':id, 'Screen Name':name, 'Score':score, 'Content':content, 'Friends':friends, 'Language':language, 'Network':network, 'Sentiment':sentiment, 'Temporal':temporal, 'User':user})
i += 1
except tweepy.TweepError:
print("Failed to run the command on that user, Skipping...")
# add result to score_df
score_df.loc[i] = pd.Series(
{'Index':df.iloc[i]['Index'], 'ID': 'NA', 'Screen Name': row, 'Score': 'NA', 'Content': 'NA', 'Friends': 'NA',
'Language': 'NA', 'Network': 'NA', 'Sentiment': 'NA', 'Temporal': 'NA', 'User': 'NA'})
i += 1
#save locally in each row, so the losses won't be so big in case of error
file = 'BotOrNot_Score_Index_' + start.astype(str) + '-' + end.astype(str) + '_skip-' + str(skip) + '_num-' + str(number) + '.csv'
#REPLACE with your path
path = 'C:/Users/lenna/sciebo/Uni/Master/Masterarbeit/Daten/outputs/' + file
print(path)
score_df.to_csv(path, sep=';', encoding='utf-8')
print(score_df)

我收到以下错误消息:
Traceback (most recent call last):
File "C:/Users/lenna/PycharmProjects/lennartbontest/bontest_rp.py", line 48, in <module>
result = bon.check_account(row)
File "C:\Users\lenna\PycharmProjects\lennartbontest\venv\lib\site-packages\botornot\__init__.py", line 123, in check_account
classification = self._check_account(user_data, tweets)
File "C:\Users\lenna\PycharmProjects\lennartbontest\venv\lib\site-packages\botornot\__init__.py", line 111, in _check_account
return bon_resp.json()
File "C:\Users\lenna\PycharmProjects\lennartbontest\venv\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\lenna\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\lenna\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\lenna\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
&#13;
这是CSV文件的屏幕截图:
我不是一位经验丰富的程序员,所以这个错误可能是微不足道的。