【问题标题】:How do you mimic the infinite scroll on Instagram with Python?你如何用 Python 模拟 Instagram 上的无限滚动?
【发布时间】:2018-06-17 14:21:56
【问题描述】:

我编写了一个小型 Python 程序来抓取 Instagram 个人资料以提取数据并显示各种统计信息。我能够从个人资料上的前 9 张照片中收集数据(或者在初始加载时出现许多照片),但我无法加载其他照片(由于无限滚动机制)。我在网上阅读了关于无限滚动的网页抓取,人们说你需要复制加载额外图像的请求。到目前为止,我一直无法复制该请求,有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: python python-2.7 web-scraping instagram


    【解决方案1】:

    不需要重新编写所有代码,已经编写了很多库来复制所有请求。

    一个这样的库是https://github.com/ping/instagram_private_api

    使用这个库的解决方案,

    from instagram_private_api import Client, ClientCompatPatch
    
    user_name = 'YOUR_USERNAME'
    password = 'YOUR_PASSWORD'
    username_to_scrape = 'USERNAME_TO_SCRAPE'
    
    all_posts = []
    
    api = Client(user_name, password)
    posts = api.username_feed(username_to_scrape)  #Gets the first 12 posts
    # Extract the value *next_max_id* from the above response, this is needed to load the next 12 posts
    
    next_max_id = posts["next_max_id"] 
    
    all_posts = all_posts + posts
    
    # 
    next_page_posts = api.username_feed(track_username, max_id = next_max_id)
    

    这只是一个帮助您入门的简单示例。

    更新:保存和加载 Cookies

    #Saving cookies
    cookies = api.cookie_jar.dump()
    with open("cookies.pkl", "wb") as save_cookies:
        save_cookies.write(cookies)
    
    #Loading cookies
    with open("cookies.pkl", "rb") as read_cookies:
        cookies = read_cookies.read()
    
    #Pass cookies to Client to resume session
    api = Client(user_name, password, cookie = cookies)
    

    【讨论】:

    • 谢谢。这真的很有帮助。出于兴趣,作者提到您应该缓存会话以防止被 Instagram 阻止。你知道怎么做吗,或者我在哪里可以找到这个文档
    • @liamw9 刚刚更新了有关如何保存和加载会话的答案。你也能接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2017-03-10
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多