karlssonkalle
karlssonkalle

Reputation: 61

Can't scrape more than 12 posts on public Instagram account

I want to scrape all posts from a public instagram account using Python for a study I'm conducting at my university. However, I'm starting to feel dismayed since I can't manage to extract more than 12 posts from Instagram.

Selenium does its job of scrolling the page and I've gotten beautifulsoup to parse the data I want in an adequate way, albeit only for the first twelve posts. Thus far I've tried a few different approaches but starting to feel stuck. I've looked over several tutorials and threads here such as:

How do I scrape a full instagram page in python?

Web Scraping with Selenium Python [Twitter + Instagram]

https://michaeljsanders.com/2017/05/12/scrapin-and-scrollin.html

https://edmundmartin.com/scraping-instagram-with-python/

Thankful for all and any response!

Best regards, Kalle.

Code I've tried. Example 1:

from bs4 import BeautifulSoup
import ssl
import json
import time

from selenium import webdriver
from datetime import datetime


class Insta_Image_Links_Scraper:

def getlinks(self, user, url):
    print('[+] Downloading:\n')
    c = webdriver.Chrome()
    c.get("https://www.instagram.com/frank_the_carden/")
    lenOfPage = c.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
    match=False
    while(match==False):
            lastCount = lenOfPage
            time.sleep(2)
            lenOfPage = c.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
            if lastCount==lenOfPage:
                    match=True



    soup = BeautifulSoup(c.page_source, 'lxml')
    body = soup.find('body')
    script = body.find('script')
    page_json = script.text.strip().replace('window._sharedData =', '').replace(';', '')

    data = json.loads(page_json)
    print('Scraping posts for user ' + user+"...........")
    for post in data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']:
        timestamp = post['node']['taken_at_timestamp']
        likedby = post['node']['edge_liked_by']['count']
        comments = post['node']['edge_media_to_comment']['count']
        isVideo = post['node']['is_video']
        caption = post['node']['edge_media_to_caption']

        print('Post on :',datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
        print('Liked by :',likedby)
        print('comments :',comments)
        print('caption :',caption)

def main(self):
    self.ctx = ssl.create_default_context()
    self.ctx.check_hostname = False
    self.ctx.verify_mode = ssl.CERT_NONE

    with open("accounts.txt") as f:
        self.content = f.readlines()
    self.content = [x.strip() for x in self.content]
    for user in self.content:
        self.getlinks(user,
                      'https://www.instagram.com/'
                      + user + '/')


if __name__ == '__main__':
    obj = Insta_Image_Links_Scraper()
    obj.main()

Example 2:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import json
from datetime import datetime

c = webdriver.Chrome()

c.get("https://www.instagram.com/frank_the_carden/")
time.sleep(1)

elem = c.find_element_by_tag_name("body")

no_of_pagedowns = 20

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1

soup = BeautifulSoup(c.page_source, 'html.parser')
body = soup.find('body')
script = body.find('script')
page_json = script.text.strip().replace('window._sharedData =', '').replace(';', '')

data = json.loads(page_json)
for post in data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']:
            timestamp = post['node']['taken_at_timestamp']
            likedby = post['node']['edge_liked_by']['count']
            comments = post['node']['edge_media_to_comment']['count']
            isVideo = post['node']['is_video']
            caption = post['node']['edge_media_to_caption']

            print('Post on :',datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
            print('Liked by :',likedby)
            print('comments :',comments)
            print('caption :',caption)

Example 3:

import time
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from datetime import datetime
import requests
import urllib3


browser = webdriver.Chrome()

media_url = 'https://www.instagram.com/graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={"id":"%s","first":50,"after":"%s"}'
browser = webdriver.Chrome()

# first get https://instagram.com to obtain cookies
browser.get('https://www.instagram.com/frank_the_carden/')
browser_cookies = browser.get_cookies()

# set a session with cookies
session = requests.Session()
for cookie in browser_cookies:
    c = {cookie['name']: cookie['value']}
    session.cookies.update(c)

# get response as JSON
response = session.get(media_url % ('5719699176', ''), verify=False).json()
time.sleep(1)

elem = browser.find_element_by_tag_name("body")

no_of_pagedowns = 20

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1

soup = BeautifulSoup(browser.page_source, 'html.parser')
body = soup.find('body')
script = body.find('script')
page_json = script.text.strip().replace('window._sharedData =', '').replace(';', '')
data = json.loads(page_json)
for post in data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']:
            timestamp = post['node']['taken_at_timestamp']
            likedby = post['node']['edge_liked_by']['count']
            comments = post['node']['edge_media_to_comment']['count']
            isVideo = post['node']['is_video']
            caption = post['node']['edge_media_to_caption']

            print('Post on :',datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
            print('Liked by :',likedby)
            print('comments :',comments)
            print('caption :',caption)

Example 4:

from random import choice
import json
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver

browser = webdriver.Chrome()

browser.get("https://www.instagram.com/frank_the_carden/")

# Selenium script to scroll to the bottom
lenOfPage = browser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
match=False
while(match==False):
                lastCount = lenOfPage
                time.sleep(1)
                lenOfPage = browser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
                if lastCount==lenOfPage:
                    match=True

_user_agents = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
]

class InstagramScraper:

    def __init__(self, user_agents=None, proxy=None):
        self.user_agents = user_agents
        self.proxy = proxy

    def __random_agent(self):
        if self.user_agents and isinstance(self.user_agents, list):
            return choice(self.user_agents)
        return choice(_user_agents)

    def __request_url(self, url):
        try:
            response = requests.get(url, headers={'User-Agent': self.__random_agent()}, proxies={'http': self.proxy,
                                                                                                 'https': self.proxy})
            response.raise_for_status()
        except requests.HTTPError:
            raise requests.HTTPError('Received non 200 status code from Instagram')
        except requests.RequestException:
            raise requests.RequestException
        else:
            return response.text

    @staticmethod
    def extract_json_data(html):
        soup = BeautifulSoup(html, 'html.parser')
        body = soup.find('body')
        script_tag = body.find('script')
        raw_string = script_tag.text.strip().replace('window._sharedData =', '').replace(';', '')
        return json.loads(raw_string)

    def profile_page_metrics(self, profile_url):
        results = {}
        try:
            response = self.__request_url(profile_url)
            json_data = self.extract_json_data(response)
            metrics = json_data['entry_data']['ProfilePage'][0]['graphql']['user']
        except Exception as e:
            raise e
        else:
            for key, value in metrics.items():
                if key != 'edge_owner_to_timeline_media':
                    if value and isinstance(value, dict):
                        value = value['count']
                        results[key] = value
                    elif value:
                        results[key] = value
        return results

    def profile_page_recent_posts(self, profile_url):
        results = []
        try:
            response = self.__request_url(profile_url)
            json_data = self.extract_json_data(response)
            metrics = json_data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']["edges"]
        except Exception as e:
            raise e
        else:
            for node in metrics:
                node = node.get('node')
                if node and isinstance(node, dict):
                    results.append(node)
        return results

from pprint import pprint

k = InstagramScraper()
results = k.profile_page_recent_posts('https://www.instagram.com/frank_the_carden/')
pprint(results)

Upvotes: 6

Views: 2732

Answers (3)

Vito Hamza
Vito Hamza

Reputation: 21

I've been looking for an answer just like you and I've found the best way to do this is by using these steps:

First Using requests library and paste from an Instagram query

https://www.instagram.com/graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={%22id%22:%22<profile_id>%22,%22first%22:<num_ofpost>,%22after%22:%22<end_cursor>%22}

<profile_id>: your Instagram profile ID. You can scrape it by using /?__a=1 at the end of your profile link. And look for this data directory:

['data']['user']['edge_owner_to_timeline_media']['edges'][0]['node']['owner']['id']

<num_ofpost>: how many posts you wanted to display per-JSON Query. Maximum by 50. If you wanted to get more, use the second step

<end_cursor>: this sort of hash indicates if there's a next page to the post. The directory is:

['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor']

then when you successfully get all the needed data you can use this code to retain the JSON format

import json
import request
profilq = request.get('https://www.instagram.com/graphql/query/?query_hash=42323d64886122307be10013ad2dcc44&variables={%22id%22:%22<profile_id>%22,%22first%22:<num_ofpost>,%22after%22:%22<end_cursor>%22}')
data = profilq.json()

Second Use recursive to help you gain post. Since one query can only withstand 50 posts, then you need to create some sort of recursive function to re-request the JSON and put it in the appropriate table.

Bit of notes Sometimes there's an index error committed by a blank caption. You can eradicate this using try and except. I like to use the exception for IndexError and replace the caption with string

try:
  your code 
except IndexError:
  caption = '*NO CAPTION PROVIDED*'

I tested the query link by 7 December 2020. You can look at my GitHub link here if you want to paste how I've done it tho..

Upvotes: 1

Centhorn
Centhorn

Reputation: 11

You can get json containing user's posts with this query template www.instagram.com/graphql/query/?query_id=17888483320059182&variables=%7B%22id%22%3A%22<user_id>%22%2C%22first%22%3A<num_of_posts>%7D

Check this for more. Think it may help https://github.com/MohanSha/InstagramResearch

Upvotes: 0

Borislav Stoilov
Borislav Stoilov

Reputation: 3677

I would directly call the instagram graph ql api, as you are doing in 'Example 3'. I had a working code, but they changed the way query_hash is generated and I can't get it working, but you are probably facing the same issue also.

Apart from that I am currently scraping instagram data using this python client. But you will need to provide instagram credentials for it to work.

Upvotes: 1

Related Questions