使用 Python 自动获取分页 instagram

Get pagination automatically instagram with Python

我正在尝试自动化我的脚本(在 Python 中)以自动连续获取 end_cursor。例如:

https://www.instagram.com/explore/tags/plebiscito/?__a=1

之后:

https://www.instagram.com/explore/tags/plebiscito/?__a=1&max_id=J0HWFB4fAAAAF0HWE8Y4AAAAFiYA

之后:

https://www.instagram.com/explore/tags/plebiscito/?__a=1&max_id=J0HWFB4fAAAAF0HWE2jPAAAAFkwA

..... .... ....

如此做,直到最后end_cursor结束。 如果你能帮助我,我将不胜感激,因为我不能。再次感谢您。

PD:我没有使用 API,因为 Sandbox 不允许开发中的应用程序。

更新:End_cursor 在进入 link

时加载的所有内容中

所以,https://www.instagram.com/explore/tags/plebiscito/?__a=1 returns 一堆 JSON 开始像

{"tag": {"media": {"count": 18926, "page_info": {"has_previous_page": false, "start_cursor": "1404693250132394506", "end_cursor": "J0HWFCHOgAAAF0HWE8dgwAAAFiYA", "has_next_page": true}, "nodes": [{"code": "BN-eRGQh8IK", "dimensions": {"width": 750, "height": 538}, "comments_disabled": false, "owner": {"id": "311016089"}, "comments": {"count": 1}, "caption": "#plebiscito", "likes": {"count": 11}, "date": 1481672506, "thumbnail_src": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/c147.0.750.750/15338447_1774364399481982_8165079596765544448_n.jpg?...

解析JSON后,可以抓取end_cursor:

end_cursor = data['tag']['media']['page_info']['end_cursor']

然后检索下一个 URL。

我无法在几秒钟内手动执行此操作到达列表末尾,所以我不知道最后一个 end_cursor 会发生什么。但我确实注意到 has_next_page 键。也许是这样的,那么:

data = json.loads(however_youre_getting_the_data('https://www.instagram.com/explore/tags/plebiscito/?__a=1'))
end_cursors = []
while data['tag']['media']['page_info']['has_next_page']:
    end_cursors.append(data['tag']['media']['page_info']['end_cursor'])
    data = json.loads(however_youre_getting_the_data('https://www.instagram.com/explore/tags/plebiscito/?__a=1&max_id={}'.format(end_cursors[-1])))