我想提取1000条以上用户评论的每条评论。 到目前为止,我已经试验了Instagram抓取工具((https://github.com/realsirjoe/instagram-scraper),但是没有成功完成循环。
#pip install igramscraper
from igramscraper.instagram import Instagram
from time import sleep
import pandas as pd
instagram = Instagram()
post1 = instagram.get_media_comments_by_id('732428342682579959', 1000)
list=[]
for a in post1['comments']:
dict={}
dict['Comments'] = comment.text
list.append(dict)
pd.DataFrame(list)
上面的代码使我可以从单个帖子中获得1000条评论。 我如何对其他每条帖子都做同样的事情? 我试图将发布媒体ID存储在一个列表中,然后遍历该列表,但这没有用。
#extracting all the media posts
instagram.media = instagram.get_medias("cocacola", 1500)
c = [cc.__dict__ for cc in instagram.media]
import pandas as pd
df = pd.DataFrame(c)
df
#storing the media id into a list called "l"
l=[]
for a in df['link']:
d={}
d["Media_ID"]=get_media_id(a)
l.append(d)
#loop the media IDs to extract all instagram comments
list2=[]
i=0
for a in l:
dict2={}
dict2['Comments']=instagram.get_media_comments_by_id(l[i]['Media_ID'], 10000)
sleep(2)
list2.append(dict2)
list3=[]
for a in dict2['Comments']:
dict3={}
dict3['Comments'] = comment.text
list3.append(dict3)
i=i+1
TypeError:“ NoneType”对象不可下标