Python 如何在代码中的列表中添加列表
我曾试图刮Instagram,我已经实现了刮的目标,但我得到的结果是完美的,但我希望它存储在列表中的列表中 代码:-Python 如何在代码中的列表中添加列表,python,python-3.x,python-2.7,loops,for-loop,Python,Python 3.x,Python 2.7,Loops,For Loop,我曾试图刮Instagram,我已经实现了刮的目标,但我得到的结果是完美的,但我希望它存储在列表中的列表中 代码:- Post links = ['https://www.instagram.com/p/BesW08pHfUt', 'https://www.instagram.com/p/BQZyTtej4yj'] for post_link in post_links: _ = API.getMediaComments(get_media_id(post_link
Post links = ['https://www.instagram.com/p/BesW08pHfUt', 'https://www.instagram.com/p/BQZyTtej4yj']
for post_link in post_links:
_ = API.getMediaComments(get_media_id(post_link), max_id = 100)
for c in reversed(API.LastJson['comments']):
comment.append(c["user"]["username"])
我从Instagram的每个帖子链接中得到的评论
'https://www.instagram.com/p/BesW08pHfUt':- 'headhotel', 'famegalore', 'motivationpoem', 'malicioussatan'
'https://www.instagram.com/p/BQZyTtej4yj':- 'monarch_motivation', 'headhotel', 'motivationpoem'
我得到的输出
['headhotel', 'famegalore', 'motivationpoem', 'malicioussatan', 'monarch_motivation', 'headhotel', 'motivationpoem']
我想要的输出
[['headhotel', 'famegalore', 'motivationpoem', 'malicioussatan'], ['monarch_motivation', 'headhotel', 'motivationpoem']]
我知道这有点容易,但我已经在2天内编码了这个刮刀,所以我有点困惑 我不熟悉该API,但我认为您希望执行以下操作:
for post_link in post_links:
_ = API.getMediaComments(get_media_id(post_link), max_id = 100)
sublist = []
for c in reversed(API.LastJson['comments']):
sublist.append(c["user"]["username"])
comment.append(sublist)
这将在外部循环的每次迭代中创建一个新的子列表,内部循环将填充该子列表,然后我们将该子列表附加到主
注释列表中。我们不知道您提到的注释或输出来自何处。您的代码片段描述不足。=API.getMediaComments(获取媒体id(post链接),max\u id=100)
做什么?更准确地说,为什么您要将该调用的返回值存储在\uu
中,然后不使用它?@PM2Ring it从InstagramIf的post链接获取注释如果您不需要调用的返回值,那么就不分配它。只需执行API.getMediaComments(获取媒体id(post\u链接),max\u id=100)
。该代码在后续代码中需要,与我的问题完全无关