【问题标题】:Error when scraping Instagram media, by adding at the end of URL (?__a=1)抓取 Instagram 媒体时出错,在 URL 末尾添加 (?__a=1)
【发布时间】:2022-06-11 11:47:06
【问题描述】:

有时在尝试抓取 Instagram 媒体时,通过在 URL 末尾添加 (?__a=1)

前: https://www.instagram.com/p/CP-Kws6FoRS/?__a=1

返回的响应

{
    "__ar": 1,
    "error": 1357004,
    "errorSummary": "Sorry, something went wrong",
    "errorDescription": "Please try closing and re-opening your browser window.",
    "payload": null,
    "hsrp": {
        "hblp": {
            "consistency": {
                "rev": 1005622141
            }
        }
    },
    "lid": "7104767527440109183"
}

为什么会返回此响应,我应该如何解决此问题?另外,我们还有其他方法来获取视频和照片的 URL 吗?

【问题讨论】:

  • 可能只是暂时的问题。它现在对我有用,来自西班牙:-------- {"graphql":{"shortcode_media":{"__typename":"GraphVideo","id":"2593557762631173202","shortcode":" CP-Kws6FoRS","dimensions":{"height":750,"width":750},"gating_info":null,"fact_check_overall_rating":null --------
  • 它在某些设备上工作,而另一些则不工作,我不知道这是怎么回事
  • 同样的问题,但前提是我通过了身份验证...
  • 意大利,这里。同样的问题,不适用于 firefox 和 chrome、auth 和 not auth、单个帖子查询或完整配置文件。

标签: instagram screen-scraping instagram-api


【解决方案1】:

ig修改了方法, 使用了新方法:

GET https://i.instagram.com/api/v1/tags/web_info/?tag_name=${tags}

下一页:

POST https://i.instagram.com/api/v1/tags/${tags}/sections/
body: 
{
include_persistent: 0
max_id: ${The last request contained this field}
next_media_ids[]: ${The last request contained this field}
next_media_ids[]: ${The last request contained this field}
page: ${The last request contained this field}
surface: grid
tab: recent
}

【讨论】:

  • 如何使用此方法获取帖子(视频或图片)详细信息?
【解决方案2】:

我也有你的问题。我目前正在使用替代解决方案来找到最终解决方案。 我设计了一个离线 api 来将链接转换为媒体 ID。要使用它,请按如下方式提交请求: http://api-bot.ir/api/insta/media_id/?url=https://www.instagram.com/p/CP-Kws6FoRS/ 代替此链接,放置任何其他链接,您将收到一个媒体 ID。当然,我强调这个是线下的,我可以指导你知道具体的点赞数等发帖信息。因此,如果您需要更多帮助,请告诉我。祝你好运。

【讨论】:

    【解决方案3】:

    我相信我可以使用以下方法找到解决方法:

    • https://i.instagram.com/api/v1/users/web_profile_info/?username={username} 获取用户的信息和最近的帖子。来自响应的data.user 与来自https://i.instagram.com/{username}/?__a=1graphql.user 相同。
    • https://instagram.com/p/{post_shortcode}的HTML响应中从<meta property="al:ios:url" content="instagram://media?id={media_id}">中提取媒体ID。
    • https://i.instagram.com/api/v1/media/{media_id}/info 使用提取的媒体 ID 获得与 https://instagram.com/p/{post_shortcode}/?__a=1 相同的响应。

    有几点很重要:

    • 脚本中使用的user-agent 很重要。我发现在开发工具中重新发送请求时生成的一个 Firefox 返回了"Sorry, something went wrong" 错误。
    • 此解决方案使用 Firefox 配置文件中的 cookie。在运行此脚本之前,您需要在 Firefox 中登录 Instagram。如果您愿意,可以将 Firefox 切换到 Chrome。
    cookiejar = browser_cookie3.chrome(domain_name='instagram.com')
    

    这是完整的脚本。让我知道这是否有帮助!

    import os
    import pathlib
    import string
    from datetime import datetime, timedelta
    from urllib.parse import urlparse
    import bs4 as bs
    import browser_cookie3
    from google.auth.transport import requests
    import requests
    
    # setup.
    username = "<username>"
    output_path = "C:\\some\\path"
    headers = {
        "User-Agent": "Mozilla/5.0 (Linux; Android 9; GM1903 Build/PKQ1.190110.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3770.143 Mobile Safari/537.36 Instagram 103.1.0.15.119 Android (28/9; 420dpi; 1080x2260; OnePlus; GM1903; OnePlus7; qcom; sv_SE; 164094539)"
    }
    
    
    def download_post_media(post: dict, media_list: list, number: int):
        output_filename = f"{output_path}/{username}"
        if not os.path.isdir(output_filename):
            os.mkdir(output_filename)
        post_time = datetime.fromtimestamp(int(post["taken_at_timestamp"])) + timedelta(hours=5)
        output_filename += f"/{username}_{post_time.strftime('%Y%m%d%H%M%S')}_{post['shortcode']}_{number}"
        current_media_json = media_list[number - 1]
        if current_media_json['media_type'] == 1:
            media_type = "image"
            media_ext = ".jpg"
            media_url = current_media_json["image_versions2"]['candidates'][0]['url']
        elif current_media_json['media_type'] == 2:
            media_type = "video"
            media_ext = ".mp4"
            media_url = current_media_json["video_versions"][0]['url']
        output_filename += media_ext
        response = send_request_get_response(media_url)
        with open(output_filename, 'wb') as f:
            f.write(response.content)
    
    
    def send_request_get_response(url):
        cookiejar = browser_cookie3.firefox(domain_name='instagram.com')
        return requests.get(url, cookies=cookiejar, headers=headers)
    
    
    # use the /api/v1/users/web_profile_info/ api to get the user's information and its most recent posts.
    profile_api_url = f"https://i.instagram.com/api/v1/users/web_profile_info/?username={username}"
    profile_api_response = send_request_get_response(profile_api_url)
    # data.user is the same as graphql.user from ?__a=1.
    timeline_json = profile_api_response.json()["data"]["user"]["edge_owner_to_timeline_media"]
    for post in timeline_json["edges"]:
        # get the HTML page of the post.
        post_response = send_request_get_response(f"https://instagram.com/p/{post['node']['shortcode']}")
        html = bs.BeautifulSoup(post_response.text, 'html.parser')
        # find the meta tag containing the link to the post's media.
        meta = html.find(attrs={"property": "al:ios:url"})
        media_id = meta.attrs['content'].replace("instagram://media?id=", "")
        # use the media id to get the same response as ?__a=1 for the post.
        media_api_url = f"https://i.instagram.com/api/v1/media/{media_id}/info"
        media_api_response = send_request_get_response(media_api_url)
        media_json = media_api_response.json()["items"][0]
        media = list()
        if 'carousel_media_count' in media_json:
            # multiple media post.
            for m in media_json['carousel_media']:
                media.append(m)
        else:
            # single media post.
            media.append(media_json)
        media_number = 0
        for m in media:
            media_number += 1
            download_post_media(post['node'], media, media_number)
    
    

    【讨论】:

    • 更多关于这个 API (i.instagram.com) 的信息可以在这里找到:stackoverflow.com/questions/43452544/…
    • https://i.instagram.com/api/v1/media/{media_id}/info - 对我不起作用。它返回{"message":"useragent mismatch","status":"fail"}
    • 我可以确认这是使用 javascript fetch 函数。直接通过浏览器访问时会返回上述消息。
    【解决方案4】:

    用户代理:

    Mozilla/5.0 (Linux; Android 9; GM1903 Build/PKQ1.190110.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3770.143 Mobile Safari/537.36 Instagram 103.1.0.15.119 Android (28/9; 420dpi; 1080x2260; OnePlus; GM1903; OnePlus7; qcom; sv_SE; 164094539)
    

    /?__a=1 替代端点;
    但是您应该放置用户代理以使用此端点。
    https://i.instagram.com/api/v1/users/web_profile_info/?username={username}

    data.graphql.user = data.user
    给出相同的结果

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2017-02-02
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多