Python爬取ins数据,一个简单的外国分享网站

2022-12-05 21:40:10 浏览数 (1)

序言

Instagram(照片墙)是一款运行在移动端上的社交应用,以一种快速、美妙和有趣的方式将你随时抓拍下的图片彼此分享,Facebook公司旗下社交应用

本篇文章主要是如何“高速复制粘贴”Instagram里的视频

环境使用

  • Python 3.8
  • Pycharm

主要模块使用

  • requests

代码编写

请求网站

代码语言:javascript复制
url = 'https://www.instagram.com/graphql/query/?query_hash=69cba40317214236af40e7efa697781d&variables={"id":"25025320","first":12,"after":"QVFDNFFmNWtjVWptaHNHN2Fhd21hdlFOQ1BMbDZQeHMzSVczMnl6T2dUN2U3YU5kYm9jZW1rYXgtUHlyQ0xMS2tjRm9iLXp2WG5Zb2NWSE9LR3laRU9zTw=="}'
headers = {
    'cookie': 'mid=Yr_f0AALAAGON2Eshq1-xqSrwgYv; ig_did=171E68A6-7132-4DE4-BE07-0B21352CE315; ig_nrcb=1; csrftoken=nZWluAdEtRyverD04rZECJdTTpqFUM2v; ds_user_id=53914138896; sessionid=53914138896:dSQkczPUqeBlKA:0:AYdUZgBloqRamafNmehfod2vBZQXVdLGvL66lV55qw; datr=CeW_YlRjzoVC-Kv6Yvqrnda3; fbm_124024574287414=base_domain=.instagram.com; fbsr_124024574287414=44mb43B3CsveUJD_NWVjypxL1Mg5HzmaboYwdPBSOhE.eyJ1c2VyX2lkIjoiMTAwMDgzMDk4MzUzNDY2IiwiY29kZSI6IkFRRFRBSlBZdjJsR294SFpqQ1lTYmhwMUFUYVFCb3JXODhVbVBYX3VHTmR3MEYzNV9wMWtYWGdjenlVQXhxbnJuNTBEMWhIeWVJVVJFc2xhZ0NkY0NBNkwzd3FuWHAxYnphMUlPNTVZbFhVWnRGdndsRGtKRGlaejBNV1kyMS11Y1l6YUY5RmhNeGtTdjUxdU9DZDNKczA5bVZKSUQ3am13ZVB6R1FOVEY5am52bU1KZFBWbGpBTlE0QkNURWduSzBLM3ZnWUpBa0IxRFhTN21hN1VLSTVLU24yYzN4LVZpWk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1UXo0T3dxdmhoV3dSS1psZVJHIiwib2F1dGhfdG9rZW4iOiJFQUFCd3pMaXhuallCQUhiQzR2TU9aQnF1T25FcVA2RU13N3lEYkZaQXpnQkRCQlNqbU1aQXdZU2FaQjVGZEJXWkFXMDBiVG13U0Z1SFd4b2VIalIyOWhDRHA4TExaQW9EODN3UDZmbGdKSjlzelpCQVJqQVJOV1ZtVlR4WkI3TUVNNXpNN3NaQXFzcEVmVVFYb3V5NXF1NDU3ak52UGl2N3hhQmtjWkJLWkE1dXhEYUlJZUtYOGk5cFJoa2dxNUNqM2gyWkJLWVpEIiwiYWxnb3JpdGhtIjoiSE1BQy1TSEEyNTYiLCJpc3N1ZWRfYXQiOjE2NTY3NDQxMTd9; rur="EAG5453914138896541688280184:01f7fbbf803c04403c41e92677764cf18d5c012e079a4cfd0d7b94107375e93be9e6b7fe"',
    'referer': 'https://www.instagram.com/instagram/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36',
    'viewport-width': '2560',
    'x-asbd-id': '198387',
    'x-csrftoken': 'nZWluAdEtRyverD04rZECJdTTpqFUM2v',
    'x-ig-app-id': '936619743392459',
    'x-ig-www-claim': 'hmac.AR1Do_jGsh-QtK6YpzVBvUI6Fb_1qQ7SzZYlfrdsTz3TLjUH',
    'x-requested-with': 'XMLHttpRequest',
}
response = requests.get(url=url, headers=headers, verify=False)

返回数据

代码语言:javascript复制
print(response.json())

解析数据

代码语言:javascript复制
for index in response.json()['data']['user']['edge_owner_to_timeline_media']['edges']:
    try:
        video_id = index['node']['id']
        print(video_id)
        link = f'https://i.instagram.com/api/v1/media/{video_id}/info/'
        json_data = requests.get(url=link, headers=headers).json()
        # pprint.pprint(json_data)
        video_url = json_data['items'][0]['video_versions'][0]['url']
        # 获取二进制数据内容
        video_content = requests.get(url=video_url, headers=headers).content
    except:
        pass

保存数据

代码语言:javascript复制
with open('video\'   video_id   '.mp4', mode='wb') as f:
    f.write(video_content)

效果

0 人点赞