我不确定这个问题的根源--最终,我试图检索Instagram帐户(“增强表”)的追随者及其追随者的数量。
数据没有任何问题--不过,我最初尝试使用Gspread,并将其正确地发送到Google表。这似乎是定期发生的。
现在,我只是尝试将信息打印到CSV -下面的脚本将给我正确的结果(不是在CSV中):
import csv
from turtle import pd
import instaloader
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
loader = instaloader.Instaloader()
loader.login("username", "password!")
profile = instaloader.Profile.from_username(loader.context, "strengthinsheets")
followees = profile.get_followees()
for followee in profile.get_followees():
print((followee.full_name, followee.username, followee.followees))但是,为了每天都能工作,我认为最好将所有数据保存到CSV中。我试着用这种方式改变:
import csv
from turtle import pd
import instaloader
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
loader = instaloader.Instaloader()
loader.login("username", "password!")
profile = instaloader.Profile.from_username(loader.context, "strengthinsheets")
followees = profile.get_followees()
for followee in profile.get_followees():
(followee.full_name, followee.username, followee.followees)
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(followee)我没有百分之百地肯定我提取的CSV是正确的,我是根据我在网上找到的文档来编写代码的,但这让人感到困惑(我以前使用.to_csv来提取数据格式,但无法这样做)。我是新来的,但我真的很喜欢这样的文件-这似乎是很容易实现的,但如果我错了,会喜欢从专家那里听到它。
发布于 2022-09-20 04:03:25
我不知道你有什么问题..。
..。但是我会在for-loop中使用writerow()编写它(最后没有char s )。
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow("full_name", "username", "followees") # header
for followee in profile.get_followees():
writer.writerow( (followee.full_name, followee.username, followee.followees) )或者,我首先创建所有行的列表,然后使用writerows() (最后是s )。
all_followees = []
for followee in profile.get_followees():
all_followees.append( (followee.full_name, followee.username, followee.followees) )
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow("full_name", "username", "followees") # header
writer.writerows( all_followees )DataFrame也是如此
all_followees = []
for followee in profile.get_followees():
all_followees.append( (followee.full_name, followee.username, followee.followees) )
df = pd.DataFrame(all_followees, columns=("full_name", "username", "followees"))
df.to_csv('followers.csv', index=False)https://stackoverflow.com/questions/73780427
复制相似问题