Instagram生成查询哈希ID Python

时间:2018-12-17 05:03:58

标签: javascript python python-requests instagram

我正在尝试找到一种方法来为instagram上的指定用户生成查询哈希。为了举例说明,我将只使用instagram.com/instagram。我正在尝试做的是找到一种无需硬编码即可获取此查询哈希ID的方法。我曾尝试使用BS4,但无法对其进行解析。谢谢所有提前响应的人。可以通过转到instagram.com/instagram,在其页面上滚动以查看网络标签来复制当前ID,以根据其页面上的照片加载查询。

import requests
import json


class IG:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.session = requests.Session()
        self.session.headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36'}
        self.session.get('https://www.instagram.com/accounts/login/')
        self.session.headers.update({'Referer': 'https://www.instagram.com/accounts/login/', 'x-csrftoken': self.session.cookies['csrftoken']})
        if (json.loads(self.session.post('https://www.instagram.com/accounts/login/ajax/', data= {'username': self.username, 'password': self.password}, allow_redirects=True).content)['authenticated'] == True):
            print("Login successful!")
        else:
            print("Login unsuccesful. Please try again.")



    def getPosts(self, username):

        self.session.get('https://www.instagram.com/' + username)

        self.has_next_page = True

        self.end_cursor = None

        #while (self.has_next_page == True):

        payload = {
            # What I am trying to generate, without hardcoding.
            "query_hash" : "66eb9403e44cc12e5b5ecda48b667d41",

            "id" : "25025320",

            "first" : 12,

            "after": self.end_cursor
        }

        print(self.session.get('https://www.instagram.com/graphql/query/', params = payload).json())










IG = IG('usernameHere', 'passwordHere')

IG.getPosts('instagram')

0 个答案:

没有答案