Get top images of hashtag in Instagram without API

This is the code that I used to get the top images of hashtag without API.no client_id or access token. It works very fine in my localhost. but when I put it on online host (pythoneverywhere). it doesn't !

def hashtagTracker(request):

    if request.GET.get('num1'):
        hashtag = request.GET['num1']
        # print("\033[1m" + "Scraping/analyzing posts for " + hashtag + "..." + "\033[0m")
        page = requests.get("https://www.instagram.com/explore/tags/" + hashtag[1:])
        posts = json.loads(page.text[page.text.find("window._sharedData") + 21: page.text.find("};</script>") + 1])
        postCount = posts["entry_data"]["TagPage"][0]["graphql"]["hashtag"]["edge_hashtag_to_media"]["count"]
       

It seems in the online version the request directed into login portal. while that is not happening in the localhost version. could anyone help me to fix this and make it run on the online host and save my week ?

Thank you and sorry for my English :)

1 answer

  • answered 2020-07-05 08:32 WCJ277

    You can use the requests module to get around the login portal by simply logging in.

    import requests
    url = 'https://www.website.com/login'
    username = 'slim shady'
    password = 'password'
    requests.Session().get(url)
    login_data = dict(USERNAME=username, PASSWORD=password)#the capitalised tags here have to be the ones from the website, you can use chrome console to find the right tags to use
    requests.Session().post(url, data=login_data)
    

    I think running this at the point where your code gets hit with the login page should work, however there's likely to be a couple of hiccups. Try stepping through your existing code and checking the variables to see where the login portal appears.