Instapy: Script scrolling through images but not correctly capturing them for interactions

Created on 6 Feb 2018  ยท  190Comments  ยท  Source: timgrossmann/InstaPy

I usually run the bot using tags. Say on 'ocean' tag if I tell the bot to do 200 interactions, it scrolls through the browser till it has enough data for 200 images and starts the interactions.
However, I am seeing this issue today where the bot scrolls through the images, but only does interactions for a small amount of images, eg. 25 images even though I specified 200 images in the input.

discussion wontfix

Most helpful comment

Due to report of @cmengler I think possible problem occured for @Tachenz @clevebounce and @cmengler is default_load,
some pages may load less pics by default, or small sizes of display may cause it
that's why I have prepared a new design to work without pre-defined default_load

  1. add this line at top of like_util.py _near other imports_
from .util import format_number
  1. replace your get_links_for_tag() definition in like_util.py with new
  2. add _a new definition_ to the end of like_util.py : get_links()

_Now, it should solve your problems_

Else than default_load, the procedure did not change that much, but if anybody else, too, have issues with old design, please use this new one (I recommend to use new one, it has been much smarter)

All 190 comments

@drogon34 that is exactly what I have be expriencing

https://github.com/timgrossmann/InstaPy/issues/1340

Same here

Ou, I did not mean to reference that one there, was another way around :) But yes, I am getting this one as well since yesterday.. 20-30 displays as loaded (while it actually scrolls through way more).

I made some tests with different accounts (account1, account2) experiencing differences:

The log of account1 shows that the bot is having problems loading the images:

session.like_by_tags(tags, amount=40)
-------
INFO:instapy.instapy:Tag [1/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [2/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [3/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [4/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [5/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [6/6]
INFO:instapy.instapy:[1/27]
...

session.follow_by_tags(tags, amount=20)
-------
INFO:instapy.instapy:Tag [1/6]
INFO:instapy.instapy:[1/20]
...
INFO:instapy.instapy:Tag [2/6]
INFO:instapy.instapy:[1/20]
...
INFO:instapy.instapy:Tag [3/6]
INFO:instapy.instapy:[1/20]
...
(and it is still running)

as you can see the bot only loads 27 images even though I specified an amount of 40.

Different to account2 where it is functioning normally:

session.follow_by_tags(tags, amount=60)
-------
INFO:InstaPy.instapy.instapy:Tag [1/7]
INFO:InstaPy.instapy.instapy:[1/60]
...
INFO:InstaPy.instapy.instapy:Tag [2/7]
INFO:InstaPy.instapy.instapy:[1/60]
...
INFO:InstaPy.instapy.instapy:Tag [3/7]
INFO:InstaPy.instapy.instapy:[1/60]
...
(it's still running as well, I'll keep you up to date)

where you can see that the first 3 tags are loaded completely normally.

Some helpful facts for the accounts (maybe to identify an experimental Instagram update which is only delivered to some accounts as @uluQulu mentioned):

account1: about 1300 followers, active for years, 266 posts and about 600 following accounts (depends on how active the bot was)

account2: completely new, only 6 followers or so, 6 posts to fill the feed a bit and no following accounts (when not counting the InstaPy followed).

It's really strange that this only affects account1 since I'm running both scripts form a server located in Germany (like me).

it is right, there is something wrong while encountering multiple users with different behaviour
except that, i tested on two really old accaunts, while older one got accurate amount, interestingly :-)
now I see that it is not static, the prior accaunt got fixed amounts in new attempts, this is a very flexible issue :\

If you also encountered such problems, share your log! To make it anonymous and smaller, use https://regexr.com/ with this expression: .*((\[|Tag).*)
It filters your log-file for the statements you see in my comment above.

you're a pro @lucanello ;)
It's just not that different than your output, I've just started to troubleshoot, if something valuable happens I will share it with you :-)

@uluQulu thank you for shedding more light on the issue...

so it seems it is account related?

about 1060 followers, active for years, 532 posts and about 1850 following accounts

my logs are pretty much same as yours

session.like_by_tags(tags, amount=200)

INFO:instapy.instapy:Tag [1/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [2/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [3/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [4/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [5/6]
INFO:instapy.instapy:[1/27]
...
INFO:instapy.instapy:Tag [6/6]
INFO:instapy.instapy:[1/27]

problem is solved @clevebounce
it brings a new design, im gonna share it after clearing up the code

@uluQulu wow, that is ver efficient .. thank you so much

I have a feeling this problem, https://github.com/timgrossmann/InstaPy/issues/1353 ,is related as it both issues happened at the same time

maybe, I'll try to have a look at it, some time
i have currently fixed the like_by_tags feature
but I wonder, if there is any other feature, too, that is also facing the same wrong amount issue, obviously?

@uluQulu Thanks for jumping in and taking a look. I see this issue on both my old account (3yrs) as well as on my secondary account (1yr). I'm located in US.
Get the same logs as @lucanello and @clevebounce

Get UPDATED solution HERE

OLD design :
Instagram has changed their page display from full list of scrolled links to dynamic links in _scope_, so every time you scroll page there is only average of 12 page links in scope (in tests, it ranged from 9 to 15),
Previously (and in some accaunts, still) we could see entire list of links (from top to the end) after scrolling page.. but now we can't, so we must grab each page's link then scroll _again_ continiously..

Then I also checked lately, instagram opens up average of 3 (from tests, ranged from 1 to 5) new picture rows (each containing, 3 pics) per scroll down,.. so, in 5 of scrolls we will open a scope of average 12 new links which makes (12*3 = 36 pics)

that's all, you must use the standalone scrolling & grabbing rather than previous one time scroll & grab
I've worked out solution for the like_by_tags feature, where you will just delete old scroller and replace with new one
To apply the fix, open up the get_links_for_tags function in like_util.py file and follow these 2 steps: (or just replace you get_links_for_tag() function with this)
first : delete _lines_ from 185 to 214
then
second: replace _lines_ from 215 to 233 with

# Get links
if skip_top_posts:
    main_elem = browser.find_element_by_xpath('//main/article/div[2]')
else:
    main_elem = browser.find_element_by_tag_name('main')
total_links = 0
links = []
filtered_links = 0
try_again = 0
default_load = 21 if not skip_top_posts else 12

while filtered_links < amount:
    if amount >= default_load:  #if amount is less to be fit in a default screen, don't scroll
        if filtered_links >= default_load:   #grab already loaded pics by default
            for i in range(3):
                browser.execute_script(
                    "window.scrollTo(0, document.body.scrollHeight);")
                sleep(1.5)
    link_elems = main_elem.find_elements_by_tag_name('a')
    if not link_elems:   #this tag does not have `Top Posts` or it really is empty..
        main_elem2 = browser.find_element_by_xpath('//main/article/div[1]')
        link_elems = main_elem2.find_elements_by_tag_name('a')
    total_links += len(link_elems)
    try:
        if link_elems:
            new_links = [link_elem.get_attribute('href') for link_elem in link_elems
                if link_elem and link_elem.text in media]
            for new_link in new_links:
                links.append(new_link)
            links_all = links   #uniqify links while preserving order
            s = set()
            links = []
            for i in links_all:
                if i not in s:
                    s.add(i)
                    links.append(i)
            if len(links) == filtered_links:
                try_again += 1
                if try_again > 1 :
                    logger.info("This tag has less pictures than intended..")
                    break
            else:
                filtered_links = len(links)
                try_again = 0
            if filtered_links < default_load and amount > filtered_links:
                logger.info("This tag has so less pictures than expected...")
                break
        else:
            logger.warning("This tag does not contain a picture")
            break

    except BaseException as e:
        logger.error("link_elems error {}".format(str(e)))
        break

@drogon34 @clevebounce @Tachenz @lucanello @ArdySan2012
Although written exactly for like_by_tags feature, this is a general approach that can be used with (or even without) a modification in other features that switched from full scroll lists to dynamic scroll lists in scope, similar to this one...

Wow amazing! Great work @uluQulu!
Make sure to write a new pull request so we can all fix this by updating our InstaPy.
I believe the scroller also needs to be updated for like_by_location.

Thanks @lucanello
But I'm afraid I can't do that, cos I already have a pull request in there..
_And I don't understand github & git stuff to open a new branch and push new changes to there, then open new PR.._
so I can't do that until my current PR is approved or disapproved (so that I remove it) :/

thanks for notifying that, we need to get full list of must-be-updated scrollers to fix entirely

tbh scrollers' content are really out-dated, even the working ones...

I got the same problem. I'm not sure if you can fork it twice, but I think our two pull requests will be merged soon which gives us also the possibility to finally put InstaPy into our Python library.
So that we can move on with fixing problems that occur due to Instagram updates.

Thank you @uluQulu for that fast turnaround! I just tested and it's working like a charm.
I am assuming Instagram is going to push these changes for all accounts soon and pretty much everyone will be impacted. Hope we can get your PR merged by then!
@timgrossmann Thoughts ?

And it works like a charm indeed!

didn't work for me.. only the first tag returns the full amount of 200 I specified.. but the rest return 12

maybe the tags has less pictures than intended according to the log.. let me test further

@uluQulu I think the problem may not be completely solved, like I mentioned earlier.. I used the most popular tags on instagram. Inserted 12 tags each with the amount 200 and only 2 tags eventually found 200, the rest 12

@clevebounce
which feature are you using? It is written exactly for like_by_tags

Lately I've tested again and one modification took place for favor of accuracy,

default_load = 12 if skip_top_posts else 3

will be

default_load = 21 if not skip_top_posts else 12

by default, instagram opens up 21 total posts without a scroll, while 9 of them are top posts

so, please update these values and add that not before skip_top_posts (my mistake)...

@clevebounce you may try this this, open up like_by_tags with a famous tag, put amount over than 27 and watch browser activity, see if browser is trying to scroll, maybe window.scrollTo doesn't work for you and if it can't scroll, _you will continue to see that error..._

thanks a million for looking into it..
I will try out your new modification and report back.

This is the error before your modification

Traceback (most recent call last):
File "quickstart.py", line 46, in
session.like_by_tags(['holiday', 'love', 'vsco', 'instapic', 'instadaily', 'vibes', 'motivation', 'travel', 'instamood', 'travelblog', 'instagram', 'travelgram'], amount=200)
File "/Users/User/Downloads/InstaPy-master/instapy/instapy.py", line 692, in like_by_tags
self.logger)
File "/Users/User/Downloads/InstaPy-master/instapy/like_util.py", line 478, in check_link
"return window._sharedData.entry_data."
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 546, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'entry_data' of undefined
(Session info: chrome=64.0.3282.140)
(Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64)

@clevebounce
it is another issue of entry_data
I don't know what you have in line 478 of like_util.py, but somewhere near it you are _executing a script_ which encounters an error that must be handled...
I've posted a solution here with details (will replace one line only, very easy to do!)

if this is/was the error you were getting, it has nothing to do with the new scroller design, nor latest modification

@uluQulu the new modification did not work for me. Again, only the first tag returns the full amount. The rest all 12.

At the same time, I checked my browsers as you have suggested and they are scrolling.

@clevebounce
I have no idea why it is happening, did you apply the main fix correctly?
that scenario means it scrolls down and gets the same links or gets no new links- that, pic links did not update or tag contains less pics,..
send a copy of like_util, I'll have a look
in worse case, Instagram has a different scrolling scope behaviour in your accaunt

@uluQulu thx!

@uluQulu it seems .py files are not allowed to be attached here. So let me copy it below for your reference.

BTW, I was thinking if it is a scrolling problem, it doesn't make sense as the first tag returned the full amount. Only the follow tags returned 12


import re
import random

"""Module that handles the like features"""
from math import ceil
from re import findall
from selenium.webdriver.common.keys import Keys

from .time_util import sleep
from .util import update_activity
from .util import add_user_to_blacklist
from .util import click_element

def get_links_from_feed(browser, amount, num_of_search, logger):
"""Fetches random number of links from feed and returns a list of links"""

browser.get('https://www.instagram.com')
# update server calls
update_activity()
sleep(2)

for i in range(num_of_search + 1):
    browser.execute_script(
        "window.scrollTo(0, document.body.scrollHeight);")
    sleep(2)

# get links
link_elems = browser.find_elements_by_xpath(
    "//article/div[2]/div[2]/a")

total_links = len(link_elems)
logger.info("Total of links feched for analysis: {}".format(total_links))
links = []
try:
    if link_elems:
        links = [link_elem.get_attribute('href') for link_elem in link_elems]
        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        for i, link in enumerate(links):
            print(i, link)
        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~")

except BaseException as e:
    logger.error("link_elems error {}".format(str(e)))

return links

def get_links_for_location(browser,
location,
amount,
logger,
media=None,
skip_top_posts=True):

"""Fetches the number of links specified
by amount and returns a list of links"""
if media is None:
    # All known media types
    media = ['', 'Post', 'Video']
elif media == 'Photo':
    # Include posts with multiple images in it
    media = ['', 'Post']
else:
    # Make it an array to use it in the following part
    media = [media]

browser.get('https://www.instagram.com/explore/locations/' + location)
# update server calls
update_activity()
sleep(2)

# clicking load more
body_elem = browser.find_element_by_tag_name('body')
sleep(2)

abort = True
try:
    load_button = body_elem.find_element_by_xpath(
        '//a[contains(@class, "_1cr2e _epyes")]')
except:
    try:
        # scroll down to load posts
        for i in range(int(ceil(amount/12))):
            browser.execute_script(
                "window.scrollTo(0, document.body.scrollHeight);")
            sleep(2)
    except:
        logger.warning(
            'Load button not found, working with current images!')
    else:
        abort = False
        body_elem.send_keys(Keys.END)
        sleep(2)
        # update server calls
        update_activity()
else:
    abort = False
    body_elem.send_keys(Keys.END)
    sleep(2)
    click_element(browser, load_button) # load_button.click()
    # update server calls
    update_activity()

body_elem.send_keys(Keys.HOME)
sleep(1)

# Get links
if skip_top_posts:
    main_elem = browser.find_element_by_xpath('//main/article/div[2]')
else:
    main_elem = browser.find_element_by_tag_name('main')

link_elems = main_elem.find_elements_by_tag_name('a')
total_links = len(link_elems)
links = [link_elem.get_attribute('href') for link_elem in link_elems
         if link_elem.text in media]
filtered_links = len(links)

while (filtered_links < amount) and not abort:
    amount_left = amount - filtered_links
    # Average items of the right media per page loaded
    new_per_page = ceil(12 * filtered_links / total_links)
    if new_per_page == 0:
        # Avoid division by zero
        new_per_page = 1. / 12.
    # Number of page load needed
    new_needed = int(ceil(amount_left / new_per_page))

    if new_needed > 12:
        # Don't go bananas trying to get all of instagram!
        new_needed = 12

    for i in range(new_needed):  # add images x * 12
        # Keep the latest window active while loading more posts
        before_load = total_links
        body_elem.send_keys(Keys.END)
        # update server calls
        update_activity()
        sleep(1)
        body_elem.send_keys(Keys.HOME)
        sleep(1)
        link_elems = main_elem.find_elements_by_tag_name('a')
        total_links = len(link_elems)
        abort = (before_load == total_links)
        if abort:
            break

    links = [link_elem.get_attribute('href') for link_elem in link_elems
             if link_elem.text in media]
    filtered_links = len(links)

return links[:amount]

def get_links_for_tag(browser,
tag,
amount,
logger,
media=None,
skip_top_posts=True):
"""Fetches the number of links specified
by amount and returns a list of links"""
if media is None:
# All known media types
media = ['', 'Post', 'Video']
elif media == 'Photo':
# Include posts with multiple images in it
media = ['', 'Post']
else:
# Make it an array to use it in the following part
media = [media]

browser.get('https://www.instagram.com/explore/tags/'
            + (tag[1:] if tag[:1] == '#' else tag))
# update server calls
update_activity()
sleep(2)

# clicking load more
body_elem = browser.find_element_by_tag_name('body')
sleep(2)

abort = True


# Get links
if skip_top_posts:
    main_elem = browser.find_element_by_xpath('//main/article/div[2]')
else:
    main_elem = browser.find_element_by_tag_name('main')
total_links = 0
links = []
filtered_links = 0
default_load = 21 if not skip_top_posts else 12

while filtered_links < amount:
    if amount >= default_load:  #if amount is less to be fit in a default screen, don't scroll
        if filtered_links >= default_load:   #grab already loaded pics by default
            for i in range(5):
                browser.execute_script(
                    "window.scrollTo(0, document.body.scrollHeight);")
                sleep(1)
    link_elems = main_elem.find_elements_by_tag_name('a')
    total_links =+ len(link_elems)
    try:
        if link_elems:
            new_links = [link_elem.get_attribute('href') for link_elem in link_elems
                if link_elem and link_elem.text in media]
            for new_link in new_links:
                links.append(new_link)
            links =  list(set(links))   #delete duplicated links
            if len(links) == filtered_links:
                logger.info("This tag has less pictures than intended..")
                break
            else:
                filtered_links =+ len(links)
            if filtered_links < default_load and amount > filtered_links:
                logger.info("This tag has so less pictures than expected...")
                break
        else:
            logger.warning("This tag does not contain a picture")
            break

    except BaseException as e:
        logger.error("link_elems error {}".format(str(e)))
        break

while (filtered_links < amount) and not abort:
    amount_left = amount - filtered_links
    # Average items of the right media per page loaded
    new_per_page = ceil(12 * filtered_links / total_links)
    if new_per_page == 0:
        # Avoid division by zero
        new_per_page = 1. / 12.
    # Number of page load needed
    new_needed = int(ceil(amount_left / new_per_page))

    if new_needed > 12:
        # Don't go bananas trying to get all of instagram!
        new_needed = 12

    for i in range(new_needed):  # add images x * 12
        # Keep the latest window active while loading more posts
        before_load = total_links
        body_elem.send_keys(Keys.END)
        # update server calls
        update_activity()
        sleep(1)
        body_elem.send_keys(Keys.HOME)
        sleep(1)
        link_elems = main_elem.find_elements_by_tag_name('a')
        total_links = len(link_elems)
        abort = (before_load == total_links)
        if abort:
            break

    links = [link_elem.get_attribute('href') for link_elem in link_elems
             if link_elem.text in media]
    filtered_links = len(links)

return links[:amount]

def get_links_for_username(browser,
username,
amount,
logger,
randomize=False,
media=None):

"""Fetches the number of links specified
by amount and returns a list of links"""
if media is None:
    # All known media types
    media = ['', 'Post', 'Video']
elif media == 'Photo':
    # Include posts with multiple images in it
    media = ['', 'Post']
else:
    # Make it an array to use it in the following part
    media = [media]

logger.info('Getting {} image list...'.format(username))

# Get  user profile page
browser.get('https://www.instagram.com/' + username)
# update server calls
update_activity()

body_elem = browser.find_element_by_tag_name('body')

try:
    is_private = body_elem.find_element_by_xpath(
        '//h2[@class="_kcrwx"]')
except:
    logger.info('Interaction begin...')
else:
    if is_private:
        logger.warning('This user is private...')
        return False

abort = True

try:
    load_button = body_elem.find_element_by_xpath(
        '//a[contains(@class, "_1cr2e _epyes")]')
except:
    try:
        # scroll down to load posts
        for i in range(int(ceil(amount/12))):
            browser.execute_script(
                "window.scrollTo(0, document.body.scrollHeight);")
            sleep(2)
    except:
        logger.warning(
            'Load button not found, working with current images!')
    else:
        abort = False
        body_elem.send_keys(Keys.END)
        sleep(2)
        # update server calls
        update_activity()
else:
    abort = False
    body_elem.send_keys(Keys.END)
    sleep(2)
    click_element(browser, load_button) # load_button.click()
    # update server calls
    update_activity()

body_elem.send_keys(Keys.HOME)
sleep(2)

# Get Links
main_elem = browser.find_element_by_tag_name('main')
link_elems = main_elem.find_elements_by_tag_name('a')
total_links = len(link_elems)
links = []
filtered_links = 0
try:
    if link_elems:
        links = [link_elem.get_attribute('href') for link_elem in link_elems
                 if link_elem and link_elem.text in media]
        filtered_links = len(links)

except BaseException as e:
    logger.error("link_elems error {}}".format(str(e)))

if randomize:
    # Expanding the pooulation for better random distribution
    amount = amount * 5

while (filtered_links < amount) and not abort:
    amount_left = amount - filtered_links
    # Average items of the right media per page loaded
    new_per_page = ceil(12 * filtered_links / total_links)
    if new_per_page == 0:
        # Avoid division by zero
        new_per_page = 1. / 12.
    # Number of page load needed
    new_needed = int(ceil(amount_left / new_per_page))

    if new_needed > 12:
        # Don't go bananas trying to get all of instagram!
        new_needed = 12

    for i in range(new_needed):  # add images x * 12
        # Keep the latest window active while loading more posts
        before_load = total_links
        body_elem.send_keys(Keys.END)
        # update server calls
        update_activity()
        sleep(1)
        body_elem.send_keys(Keys.HOME)
        sleep(1)
        link_elems = main_elem.find_elements_by_tag_name('a')
        total_links = len(link_elems)
        abort = (before_load == total_links)
        if abort:
            break

    links = [link_elem.get_attribute('href') for link_elem in link_elems
             if link_elem.text in media]
    filtered_links = len(links)

if randomize:
    # Shuffle the population index
    links = random.sample(links, filtered_links)

return links[:amount]

def check_link(browser,
link,
dont_like,
ignore_if_contains,
ignore_users,
username,
like_by_followers_upper_limit,
like_by_followers_lower_limit,
logger):

browser.get(link)
# update server calls
update_activity()
sleep(2)

"""Check if the Post is Valid/Exists"""
post_page = browser.execute_script(
    "return window._sharedData.entry_data.PostPage")
if post_page is None:
    logger.warning('Unavailable Page: {}'.format(link.encode('utf-8')))
    return True, None, None, 'Unavailable Page'

"""Gets the description of the link and checks for the dont_like tags"""
graphql = 'graphql' in post_page[0]
if graphql:
    media = post_page[0]['graphql']['shortcode_media']
    is_video = media['is_video']
    user_name = media['owner']['username']
    image_text = media['edge_media_to_caption']['edges']
    image_text = image_text[0]['node']['text'] if image_text else None
    owner_comments = browser.execute_script('''
  latest_comments = window._sharedData.entry_data.PostPage[0].graphql.shortcode_media.edge_media_to_comment.edges;
  if (latest_comments === undefined) latest_comments = Array();
  owner_comments = latest_comments
    .filter(item => item.node.owner.username == '{}')
    .map(item => item.node.text)
    .reduce((item, total) => item + '\\n' + total, '');
  return owner_comments;
'''.format(user_name))
else:
    media = post_page[0]['media']
    is_video = media['is_video']
    user_name = media['owner']['username']
    image_text = media['caption']
    owner_comments = browser.execute_script('''
  latest_comments = window._sharedData.entry_data.PostPage[0].media.comments.nodes;
  if (latest_comments === undefined) latest_comments = Array();
  owner_comments = latest_comments
    .filter(item => item.user.username == '{}')
    .map(item => item.text)
    .reduce((item, total) => item + '\\n' + total, '');
  return owner_comments;
'''.format(user_name))

if owner_comments == '':
    owner_comments = None

"""Append owner comments to description as it might contain further tags"""
if image_text is None:
    image_text = owner_comments
elif owner_comments:
    image_text = image_text + '\n' + owner_comments

"""If the image still has no description gets the first comment"""
if image_text is None:
    if graphql:
        image_text = media['edge_media_to_comment']['edges']
        image_text = image_text[0]['node']['text'] if image_text else None
    else:
        image_text = media['comments']['nodes']
        image_text = image_text[0]['text'] if image_text else None
if image_text is None:
    image_text = "No description"

logger.info('Image from: {}'.format(user_name.encode('utf-8')))

"""Find the number of followes the user has"""
if like_by_followers_upper_limit or like_by_followers_lower_limit:
    userlink = 'https://www.instagram.com/' + user_name
    browser.get(userlink)
    # update server calls
    update_activity()
    sleep(1)
    num_followers = browser.execute_script(
        "return window._sharedData.entry_data."
        "ProfilePage[0].user.followed_by.count")
    browser.get(link)
    # update server calls
    update_activity()
    sleep(1)
    logger.info('Number of Followers: {}'.format(num_followers))

    if like_by_followers_upper_limit and \
       num_followers > like_by_followers_upper_limit:
            return True, user_name, is_video, \
                'Number of followers exceeds limit'

    if like_by_followers_lower_limit and \
       num_followers < like_by_followers_lower_limit:
            return True, user_name, is_video, \
                'Number of followers does not reach minimum'

logger.info('Link: {}'.format(link.encode('utf-8')))
logger.info('Description: {}'.format(image_text.encode('utf-8')))

"""Check if the user_name is in the ignore_users list"""
if (user_name in ignore_users) or (user_name == username):
    return True, user_name, is_video, 'Username'

if any((word in image_text for word in ignore_if_contains)):
    return True, user_name, is_video, 'None'

dont_like_regex = []

for dont_likes in dont_like:
    if dont_likes.startswith("#"):
        dont_like_regex.append(dont_likes + "([^\d\w]|$)")
    elif dont_likes.startswith("["):
        dont_like_regex.append("#" + dont_likes[1:] + "[\d\w]+([^\d\w]|$)")
    elif dont_likes.startswith("]"):
        dont_like_regex.append("#[\d\w]+" + dont_likes[1:] + "([^\d\w]|$)")
    else:
        dont_like_regex.append(
            "#[\d\w]*" + dont_likes + "[\d\w]*([^\d\w]|$)")

for dont_likes_regex in dont_like_regex:
    quash = re.search(dont_likes_regex, image_text, re.IGNORECASE)
    if quash:
        quashed = (quash.group(0)).split('#')[1]
        iffy = (re.split(r'\W+', dont_likes_regex))[3]
        inapp_unit = ('Inappropriate! ~ contains \'{}\''.format(quashed) if quashed == iffy else
                          'Inappropriate! ~ contains \'{}\' in \'{}\''.format(iffy, quashed))
        return True, user_name, is_video, inapp_unit

return False, user_name, is_video, 'None'

def like_image(browser, username, blacklist, logger):
"""Likes the browser opened image"""
like_elem = browser.find_elements_by_xpath(
"//a[@role='button']/span[text()='Like']/..")
liked_elem = browser.find_elements_by_xpath(
"//a[@role='button']/span[text()='Unlike']")

if len(like_elem) == 1:
    # sleep real quick right before clicking the element
    sleep(2)
    click_element(browser, like_elem[0])

    logger.info('--> Image Liked!')
    update_activity('likes')
    if blacklist['enabled'] is True:
        action = 'liked'
        add_user_to_blacklist(
            browser, username, blacklist['campaign'], action, logger
        )
    sleep(2)
    return True
elif len(liked_elem) == 1:
    logger.info('--> Already Liked!')
    return False
else:
    logger.info('--> Invalid Like Element!')
    return False

def get_tags(browser, url):
"""Gets all the tags of the given description in the url"""
browser.get(url)
# update server calls
update_activity()
sleep(1)

graphql = browser.execute_script(
    "return ('graphql' in window._sharedData.entry_data.PostPage[0])")
if graphql:
    image_text = browser.execute_script(
        "return window._sharedData.entry_data.PostPage[0].graphql."
        "shortcode_media.edge_media_to_caption.edges[0].node.text")
else:
    image_text = browser.execute_script(
        "return window._sharedData.entry_data."
        "PostPage[0].media.caption.text")

tags = findall(r'#\w*', image_text)
return tags

@uluQulu Looks like there is another error happening now โ€œnot enough images in the tagโ€ (even if there is plenty actually)

@Tachenz Try setting the amount to less than 100, around 70-90 or so and check it again. I was facing that too, I just reduced the amount to less than 100 and it worked fine. Give it a shot.

Yeah, but it was working with 300 only yesterday with Mr @uluQulu fix. I wonder what changed...

It does not work... when running the script with your fix, the script simply crashes.

Edit reply for @Tachenz: it does crash no more, I had copied the suggested code into the file using Notepad++ so I got probably some mistakes by indenting the code.
However, the script now runs correctly, but I cannot like more than 12 pictures.

Please find attached here my "like_util.py" file.

like_util.zip

Run it in CMD and post the crash errror your are getting. I bet it is indent error or smthing (as it worked for so many people here).

On other note, if to run 2 tags in one session - second is always โ€œnot enough images errorโ€ :(

@uluQulu It feels like the mechanism of loading images initially for the first hashtag - and then for the seconds one - are different somehow...
If you run 2 hashtags, first one loads images, while the second always gives an error of "not enough pictures" - even if running just 100.

Another important thing is - this error appears almost immediately when bot starts on "hashtag 2/2". Meaning, there is just something wrong with how that is initiated - it does not get any time to scroll anything down...

Hi @Tachenz
I guess it's the very next update xD
since I have not faced such a situation, yet (tested with 400 likes per tags and works fine), I don't know what is the pic link scope at your tag page layout...
but I have a new idea, much better, going to try it soon
@clevebounce it's clear that you are the first one who met this layout update, your accaunt is in the spot of changes ;) (also, saw your like_util.py, it seems fix applied correctly.._btw why don't you use pastebin.com for pasting such huge code_)

Who can provide a few information to me at our Slack ? (those having less pics in second (or in all) tags)

@riccardorighettiphoto you've got indentation mistakes in that pinned zip file, please apply indentation as it is in the fix

I tested twice with 3 tags to be liked (100 likes for every tag). First two tags analyzed 100 pictures, the third only 36. In case you wanna test and confirm on your own side.

Happy sunday. Bye.

Only trying to like by one Tag. Using Ubuntu Server 16.04 (So no GUI), Python 3.5 and patched the code from https://github.com/timgrossmann/InstaPy/issues/1365#issuecomment-363586896
Log says:

INFO [***]  Logged in successfully!
INFO [***]  Tag [1/1]
INFO [***]  --> b'followforfollow'
INFO [***]  This tag does not contain a picture

script:

from instapy import InstaPy

insta_username = '**********'
insta_password = '**********'

# set headless_browser=True if you want to run InstaPy on a server
session = InstaPy(username=insta_username,
                      password=insta_password,
                      nogui=True)
session.login()

    # settings
session.set_upper_follower_count(limit=900)
session.set_do_comment(True, percentage=100)
session.set_comments([u':heart_eyes:  :heart_eyes:  :heart_eyes:', u'Follow for Follow? :heart:' ])
session.set_do_follow(enabled=True, percentage=100, times=100)

    # actions
session.like_by_tags(['followforfollow'], amount=800)

    # end the bot session
session.end()

When I'm using a hashtag with smaller amounts of results, it works. For example with the Tag: got

Thanks for notifying @FranklinClinton
I have to say, if you have visited a tag without Top Posts as followforfollow is, it's will have different page elements that's what making problem. (_is this new or there were previously, tags without Top Posts?_)
Now, _if there is no Top Posts it will re-check again in other element,_
so, before the total_links += len(link_elems) line, add these three lines :

if not link_elems:   #this tag does not have `Top Posts` or it really is empty..
    main_elem2 = browser.find_element_by_xpath('//main/article/div[1]')
    link_elems = main_elem2.find_elements_by_tag_name('a')

This is what I am getting:

python3 quickstart.py
Traceback (most recent call last):
File "quickstart.py", line 1, in
from instapy import InstaPy
File "/Users/bt/InstaPy/instapy/__init__.py", line 2, in
from .instapy import InstaPy
File "/Users/bt/InstaPy/instapy/instapy.py", line 22, in
from .like_util import check_link
File "/Users/bt/InstaPy/instapy/like_util.py", line 260
return links[:amount]
^
SyntaxError: 'return' outside function

hi @ohoscar
Usually it's an indentation syntax error. Check around the error.
Or pastebin your get_links_for_tag() function from like_util.py, so that we can help

hey @uluQulu, thanks! Ill see whats going on there..
Also, when I delete lines from 185 to 214, that clearly shifts everything up.. thats ok.. right?

Yes @ohoscar
Also, when you decide not to use this approach, just revert it back to the defaults in main repo...

@uluQulu I added those lines, but it still does not work. Strange..
Take a look (Pastebin will expire in 24 hours, don't worry):
https://pastebin.com/VXwP5cQr

You've patched wrong place, mate
@FranklinClinton please, see the updated fix above (hint: search for div[1])

Also, I see there are some =+, you should replace them with +=...

@uluQulu Is this patched correctly?>

def get_links_for_tag(browser,
tag,
amount,
logger,
media=None,
skip_top_posts=True):
"""Fetches the number of links specified
by amount and returns a list of links"""
if media is None:
# All known media types
media = ['', 'Post', 'Video']
elif media == 'Photo':
# Include posts with multiple images in it
media = ['', 'Post']
else:
# Make it an array to use it in the following part
media = [media]

browser.get('https://www.instagram.com/explore/tags/'
            + (tag[1:] if tag[:1] == '#' else tag))
# update server calls
update_activity()
sleep(2)

# clicking load more
body_elem = browser.find_element_by_tag_name('body')
sleep(2)

abort = True
try:

# Get links
if skip_top_posts:
    main_elem = browser.find_element_by_xpath('//main/article/div[2]')
else:
    main_elem = browser.find_element_by_tag_name('main')
total_links = 0
links = []
filtered_links = 0
default_load = 21 if not skip_top_posts else 12

while filtered_links < amount:
    if amount >= default_load:  #if amount is less to be fit in a default screen, don't scroll
        if filtered_links >= default_load:   #grab already loaded pics by default
            for i in range(5):
                browser.execute_script(
                    "window.scrollTo(0, document.body.scrollHeight);")
                sleep(1)
    link_elems = main_elem.find_elements_by_tag_name('a')
    if not link_elems:   #this tag does not have `Top Posts` or it really is empty..
        main_elem2 = browser.find_element_by_xpath('//main/article/div[1]')
        link_elems = main_elem2.find_elements_by_tag_name('a')
    total_links += len(link_elems)
    try:
        if link_elems:
            new_links = [link_elem.get_attribute('href') for link_elem in link_elems
                if link_elem and link_elem.text in media]
            for new_link in new_links:
                links.append(new_link)
            links =  list(set(links))   #delete duplicated links
            if len(links) == filtered_links:
                logger.info("This tag has less pictures than intended..")
                break
            else:
                filtered_links += len(links)
            if filtered_links < default_load and amount > filtered_links:
                logger.info("This tag has so less pictures than expected...")
                break
        else:
            logger.warning("This tag does not contain a picture")
            break

    except BaseException as e:
        logger.error("link_elems error {}".format(str(e)))
        break


while (filtered_links < amount) and not abort:
    amount_left = amount - filtered_links
    # Average items of the right media per page loaded
    new_per_page = ceil(12 * filtered_links / total_links)
    if new_per_page == 0:
        # Avoid division by zero
        new_per_page = 1. / 12.
    # Number of page load needed
    new_needed = int(ceil(amount_left / new_per_page))

    if new_needed > 12:
        # Don't go bananas trying to get all of instagram!
        new_needed = 12

    for i in range(new_needed):  # add images x * 12
        # Keep the latest window active while loading more posts
        before_load = total_links
        body_elem.send_keys(Keys.END)
        # update server calls
        update_activity()
        sleep(1)
        body_elem.send_keys(Keys.HOME)
        sleep(1)
        link_elems = main_elem.find_elements_by_tag_name('a')
        total_links = len(link_elems)
        abort = (before_load == total_links)
        if abort:
            break

    links = [link_elem.get_attribute('href') for link_elem in link_elems
             if link_elem.text in media]
    filtered_links = len(links)

return links[:amount]

@uluQulu Instructions unclear; Penis got stuck in the toaster. Lol.
It works now, thank you very much! For everybody else see https://pastebin.com/MZ4eA1Df
Will expire in 24 hours so be fast.

It is very clear @FranklinClinton there is only one line of total_links += len(link_elems) and you couldnt find it :D

@dwai03
yes, I guess so

@ohoscar @uluQulu I fixed ohoscar's problem just by selecting the whole code that I replaced and than pressing TAB so the code moved and now it's not giving me an error but it's still only liking 24 pictures.

Fixed my issue with @FranklinClinton's file. Downloded, erased old one, renamed. I finally see the [1/100]

Thank you!

hi @urbizupi
considering that you applied the fix correctly, there is something wrong for some users that still get less links,
what is your browser and it's version? i need troubleshoot information to fix it cos it doesn't happen for me, yet

@Tachenz @riccardorighettiphoto @urbizupi
apply this to get correct amount of pic links

four modifications

  1. replaced this line
filtered_links += len(links)

to

filtered_links = len(links)
  1. added try_again variable to re-evaluate pic links in case of Pages did not load any new pic at required time
  2. increased sleep time to 1.5 seconds per scroll
  3. and decreased scroll times to 3

Now average time to get links of 300 pics is 1.7 minutes, and around 50 seconds for 100 pics

_Updated the fix above, get new changes from it or replace your get_links_for tag with this_

@clevebounce can you try if it works now
tested on both versions- 64.0.3282.140 and 66.0.3345.0 (Canary) of Chrome, working well
if you still have problems, it's in IG's side (due to updates), share details to help fix it, too!

Hi @uluQulu,

Your code works perfectly and I finally have the script back working again. Thank you!

Although, one small slight issue. The code no longer likes tags in chronological order! It just seems to pick random pictures under the # and likes them. Any ideas?

Hi @Fellowes52
Glad that works for you :-)
I used plain set() to uniqify links but didn't realize that it doesn't preserve order..
please, replace this line :

links =  list(set(links))   #delete duplicated links

with

links_all = links   #uniqify links while preserving order
s = set()
links = []
for i in links_all:
    if i not in s:
        s.add(i)
        links.append(i)

I will update the fix now, thanks for reminding!

Good man, works like a charm again. Perfect. Thank you ๐Ÿ‘

Hello @uluQulu, thanks for your trick.
I had just amended the code with your suggestions here and completed a run on three tags and 100 pictures analyzed for all of them (300 pics totally). It looks like working perfectly now.
Tomorrow I am gonna try another robust run.
Just one thing I did not understand about your linked comment: you stated that for getting 300 links it takes 1.7 seconds, what did you refer to? My script just run took one hour to complete.

Hi @riccardorighettiphoto
that's fantastic!
thanks for taking the initiative and giving feedback,, you rock!
but whew it's 1.7 minutes (102 seconds)! your attention to detail puts you at the top ;)
basically, in like_by_tags() feature, we do get links (from tag's page) at given amount and then start doing stuff using those links one by one, per tag..
so for one tag, getting the links of 300 pictures takes about 1.7 minutes...

@uluQulu
Ok, so you didn't refer to the time to complete the run (analyzing 300 pics, choose which to like/comment or not, which users to follow and so...) but only the time to take RAW information which are used thereafter in the process, right?
As I wrote before, my job took 1 hour to complete (300 pics), which is a quite different time compared than your stated 1.7 minutes. :)
Bye.

@riccardorighettiphoto
I don't get it, it is practically impossible to take an hour,
you probably took 3 tags with amount of 100 per each, so InstaPy will refer to get_links_for_tag() function when starting a new tag, so, I say again, I will take about 50 seconds for getting 100 pic links and then will start to do actions.. :-)

@uluQulu 64.0.3282.140 64-bit is the version of chrome I'm running. I'll try redownloading it as well as redownloading instapy and applying the fix again.

P.S.: I also dont have the get_lins_for_tags function in my like_util.py file.

hi @urbizupi
great discovery! I mistyped get_links_for_tags as get_lins_for_tags xD (fixed now :)
Good luck!

@uluQulu mine seems to be stuck in an infinite loop
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
INFO [chan.cleve] This tag has less pictures than intended..
I

@uluQulu thank you for your help and the other people who is also helping and contribuying to the proyect.

just a quick question, which code I should copy now in order to fix the scrolling, because i saw different modifications and I'm confused and which .py file i should rewrite.

thanks

@FranklinClinton I think i'm having the same issues as you :( . Can you do the pastebin again? instructions unclear, i don't want to die haha.

@Luisetty
I've shared the raw code and also shared get_links_for_tag() function in in 2 places, this is third
I also told that _I've updated the main fix above_, I can't really follow the multifarious intricacies you have pointed ๐Ÿ“ƒ
Take a few seconds and read the updated main fix, it says that you will edit in like_util.py and cos of my like_util.py has other changes unrelated to this fix I did not share it. Just replace your get_links_for_tag() function with pastebinned text and it is enough to try out my approach ๐Ÿ‘

@clevebounce
_It's often a bit of an anticlimax when it actually doesn't work as expected, looking forward to a good feedback to find out the problem for you_
and in fact it can't possibly loop that repeatedly, after trying once again (_when no new links opened up_) it will break the loop and return all catched links to the like_by_tags() feature, so you can maximum see one of that error per tag
Did you apply correctly?

@uluQulu Finnaly! Thank you so much! I actually used the technique where you replace get_links_for_tag() with the code from pastebin.

@uluQulu thank you! yes the pastebin was enough! now I need to check unfollow issues! I'm still new at python but learning! awesome!

@uluQulu I am not sure what I did wrong. It is still looping infinitely on my fourth try...

I pastebin my file here to allow you to see whether it is indentation error.. but I checked the indentation line for line twice... wonder what I missed... now the bot can't run at all for me after the new modification ..

https://pastebin.com/5cvqHu8W

@clevebounce
indent 4 spaces back (or a tab) the lines from 218 to 236

im getting Error: SyntaxError: 'return' outside function
dont know what i copied wrong am not good at this.

hi @jukedd
you prob have an indent error
try to replace your get_links_for_tag() definition in like_util.py with this
or pastebin your file to have a closer look

@uluQulu thanks ๐Ÿ˜ , you are an angel ... by fixing the indent error as you have suggest on the block, the bot seems to be up and running again...

however, I ran into other errors and would like to keep you updated ... is there a problem with line 427?

Traceback (most recent call last):
File "quickstart.py", line 46, in
session.like_by_tags(['japan', 'travelling', 'travel', 'trip', 'love', 'moments', 'instamood', 'escape', 'instadaily', 'instacool', 'instagram', 'vacationtime', 'girls', 'vibes'], amount=200)
File "/Users/User/Downloads/InstaPy-master/instapy/instapy.py", line 692, in like_by_tags
self.logger)
File "/Users/User/Downloads/InstaPy-master/instapy/like_util.py", line 427, in check_link
"return window._sharedData.entry_data.PostPage")
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 546, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'entry_data' of undefined
(Session info: chrome=64.0.3282.167)
(Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64)

Mr @uluQulu after couple days of no issues on loading images - the error of "not enough images in the tag" is back again. Always happens on the second tag with this error:
img_3021

marvelous @clevebounce !
you're having a temporary outage error
have a look at my approach to solve it here

@Tachenz
you've probably removed abort=True variable while applying the fix
please, use the latest code (_just replace your get_links_for_tag() definition with it_)
if problem still persists, please let me know :-)

@uluQulu thank you again.
I ran thru your proposed solution to this problem.
I am not exactly sure where to insert:
from selenium.common.exceptions import WebDriverException

At the same time, again, I am not sure about what to replace.
Do you have a file I could copy and paste?
Sorry, I am new to this programming thing...๐Ÿ˜…

@clevebounce
it is best to import things at top of the file

around the line of 427 in like_util.py there is a line starts like post_page = browser.execute_script(
you will replace it with the lines I posted in that issue ...

if you still cannot do it, pastebin your like_util.py I will do it for you (_my like_util.py and check_link has other modifications, too, so if I share, you will have conflicts in the future with main repo_ :)

@uluQulu thank you again for offering your help ๐Ÿ˜ƒ

This is my pastebin https://pastebin.com/Gq7ZRrFm

@clevebounce
It's pleasure, we learn by helping each other :-)
Patched both of the places that can throw WebDriverException when outage occurs

@clevebounce, @uluQulu just quick question, the new patched (16th February) it seems to be working with more than 25 tags, in the like_util.py file
but got this error

from logs:

File "quickstart.py", line 47, in
session.like_by_tags(['newyork'], amount=150, media='Photo')
File "InstaPy/instapy/instapy.py", line 851, in like_by_tags
self.logfolder)
TypeError: like_image() takes 4 positional arguments but 5 were given

I checked the line 851 from instapy.py and looks like this

           if not inappropriate:
                                liked = like_image(self.browser,
                                            user_name,
                                         self.blacklist,
                                             self.logger,
                                          self.logfolder)

my question is , should I delete the self.logfolder? or is a typo or indent issue?

ok I confirmed its working now, from the new pastebin @uluQulu fixed for @clevebounce Patched is missing on the line 569 in the like_util.py def like_image(browser, username, blacklist, logger):

to add the "logfolder" in that fixes the issue.

def like_image(browser, username, blacklist, logger,logfolder):

thanks @uluQulu you are a hero!

When Im learning python
http://i0.kym-cdn.com/entries/icons/original/000/008/342/ihave.jpg

@Luisetty
It would be missing, since I have only modified cleve's file to add new lines in 2 places for handling entry_data errors, are you using cleve's like_util? ๐Ÿ˜‰

Thanks for the fix @uluQulu :) -- just a side note if others find similar situation, I had to change default_load to 6 in order to build my list of links to my target amount of 200 per tag. When it was left as 12 it was only building the links to amount of 8 per tag.

  • Ubuntu 16.04 LTS
  • Python 2.7.12
  • ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881)

Hello,
cannot understand why, anyway (probably after havving applied the @uluQulu "outage error" fix?) , it is some days I am randomly getting analyzed not the number of pictures expected (100), but less, way far less. For instance, I had just run on the tag #boudoirmodel (which has currently more than 70k posts) and got only 3 posts. This morning I had only 6 posts for tag #lingeriemodel (while yesterday for the same tag I got 21).

hi @cmengler thanks for pointing it out, i did took default_load number from my tests, I shall change design cos not all of the time pages load same amount of pics (21 x 12), soon will share another approach

@riccardorighettiphoto right, if it brings problems for you, you should revert back from outage error fix, just _get original file from main repo_

@uluQulu
I cannot be sure it is due that fix. The would not like to revert back to the original file from repo because I had applied other fixes (for sure the one of this discussion about "script scrolling"). :(

edit: Just had tried to revert back to the original file, then applied the fix, but for the tag #boudoirmodel I got only 6 posts, with this warning message: "This tag has so less pictures than expected". :(

@uluQulu thank you for being so helpful :)

I have tried the like_util file you had so kindly prepared for me. Unfortunately, it did not work for me.

I had 12 tags. Only the first returns 200 as the amount I have specified. The rest is all 12 and one of them 9

Due to report of @cmengler I think possible problem occured for @Tachenz @clevebounce and @cmengler is default_load,
some pages may load less pics by default, or small sizes of display may cause it
that's why I have prepared a new design to work without pre-defined default_load

  1. add this line at top of like_util.py _near other imports_
from .util import format_number
  1. replace your get_links_for_tag() definition in like_util.py with new
  2. add _a new definition_ to the end of like_util.py : get_links()

_Now, it should solve your problems_

Else than default_load, the procedure did not change that much, but if anybody else, too, have issues with old design, please use this new one (I recommend to use new one, it has been much smarter)

Hi @uluQulu,

I am getting the same error as @Tachenz @clevebounce and @cmengler

I see you have been amazing and issued a fix to this again! If i throw up my like_util.py in Pastebin would you be able to fix my file, as per the comment above. I just don't know this time what to delete and what to overwrite and don't fancy breaking the whole code.

https://pastebin.com/m29TpjhY

@Fellowes52
I put full functions (_or definitions.._) for ease of fix, but eh ๐Ÿš— _<< follow the car_

Excellent job once again @uluQulu! your new smart approach works very well! :)

INFO [xxx] desired amount: 200 | top posts [disabled]: 9 | possible posts: 90130
INFO [xxx] [1/200]

Nice one @uluQulu worked like a charm.

Hi @uluQulu,

Thank you!

Hey @uluQulu Thank you - testing the new one (from 22 hours ago) tonight! Will report here :)

@uluQulu

Got the following error running your new file

File "/Users/User/Downloads/InstaPy-master/instapy/instapy.py", line 699, in like_by_tags
self.logger)
TypeError: like_image() missing 1 required positional argument: 'logfolder'

@clevebounce
self.logfolder is missing in your instapy.py file and I think I didn't share any instapy.py :-)
so check around line 699 in like_by_tags() method at instapy.py file and add that missing self.logfolder, _it is a line similar to this_:

liked = like_image(self.browser,
                                           user_name,
                                           self.blacklist,
                                           self.logger,       # dont forget the comma here
                                           self.logfolder)      #you should  add this missing `self.logfolder`

Hi @uluQulu , thanks a ton for all your help!

_Quick question_: In your new implementation, I ran the script with 'sunrise' tag and 100 interactions, it picked up the right number but some of the images were from 2017 and some were few minutes old. Do we know if the interactions are happening by date/time or in a random order ?

thank you @drogon34
_previously one of the guys here asked me that it takes in random order, so then I changed it to Top-to-Down as is in the tag page.._

out of your request I've decided to go the extra mile and check it again!
TESTs:
first compared 45 pics by taking screenshots while scrolling then matched them againist program's interaction order, _it took links as they are in tag page from top to down_ (same for top posts and non-top posts)

then checked your sunrise tag, although it has possibly 45 million posts I saw that after _a few scrolls_ there are some posts from 2017 and even saw a one from 2016, other from 2015.. (I didn't see such old posts in one of my fav nikontop tag, _it's clear that IG has a different strategy_ to show posts than _datetime order_)

I came through with a result that links are taken from the tag's page in the order they are from top to down as expected

Hi @uluQulu , thanks for that explanation. I tested with several other tags and did not encounter this issue, so it might be due to some tags just loading really old images, and not tied to the way the bot functions.

@uluqulu Worked like a charm, that new version.

This function wonโ€™t work for the like by the user followers, by chance? (That one also returns โ€œnot enoughโ€ users error, but must work completely differently, probably)

Hi @Tachenz thanks for the original feedback!

_i think, only like_by_tags() and follow_by_tags() features use get_links_for_tag() function for getting links and other features use different methods to get links that's why it happened in your example..._

now that being sure it works for all, I will apply the latest approach to all eligible features and then pull a request

by the time it would be great if some of you could report known misbehaving features that return inaccurate amount of links, e.g. i heard of like_by_locations() and follow_user_followers() ...

@uluQulu

I inserted the self.logfolder according to your instruction and ran the bot. Only the first tag returned the desired amount of 200, all the rest 12 and they are big tags:

INFO [cleve] Tag [4/26]
INFO [cleve] --> b'travel'
INFO [cleve] desired amount: 200 | top posts [disabled]: 9 | possible posts: 259602341
INFO [cleve] Insufficient amount of links ~ trying again: 1
INFO [cleve] Insufficient amount of links ~ trying again: 2
INFO [cleve] Insufficient amount of links ~ trying again: 3
INFO [cleve]
'travel' tag POSSIBLY has less images than desired...
INFO [chan.cleve] [1/12]
INFO [chan.cleve] https://www.instagram.com/p/BfZuqUYn0J8/?tagged=travel
INFO [chan.cleve] Image from: b'sonny0321'
INFO [chan.cleve] Number of Followers: 700
INFO [chan.cleve] Link: b'https://www.instagram.com/p/BfZuqUYn0J8/?tagged=travel'
INFO [chan.cleve] Description: b'Can this be my kitchen???\n.\n.\n.\n.\n#portsmouth #mood #moodygrams #portsmouthnh #portsmouthlove #kitchen #dreamy #wine #french #frenchfood #frenchcuisine #brunch #nhseacoast #newhampshire #newengland #lostboy #wanderer #wandering #wanderlust #wanderlusting #travel #travelbug #wanderluster #travelgram #travelingram #instasparrow'
INFO [chan.cleve] --> Image Liked!
INFO [chan.cleve] --> Not commented
INFO [chan.cleve] --> Not following

I'll try to reset my machine and try a 2nd time now and report back

@uluQulu Same as poster above... That must be a new thing (yet again - have they hired @timgrossmann to change how things work every week? :D)
222

As @clevebounce stated "_first tag gets correct, and all others wrong amount..."_
Just checked, and nothing has changed but that can be a new update which did not hit my accaunts, yet
if it really gets wrong amount in all of the tags, there is an update most probably

along all these, there is a case that IG behaves badly to _spammed tags_ and _some tags_ with so huge data of hundreds of millions of posts as it sometimes quits to load images in them (_even though you wait 2 minutes_)

That is right, this happens to tag#2 usually. Oh that IG.. :D

@Tachenz @clevebounce
I got some hints that it really is an update, before hits more users, can you _clear_ these below to maybe find a solution

  1. does it happen only for the second tag or for all tags after the first tag
  2. does it happen everytime or sometimes?

anybody else also have this situation? (_would be great if any of you having this issue share full like_util.py_)

  1. emm... Never tried more than 2, definitely the 2nd one - but will try to qeue 3 and see.
  2. 2/2 so everytime for now, though not much data :P

@Tachenz
thanks for the insight,
why don't you add a break point to test quickly?

1. find def like_by_tags in instapy.py file
2. modify it to add 4 more lines like this

except NoSuchElementException:
                self.logger.error('Too few images, skipping this tag')
                continue

for i, link in enumerate(links):
    self.logger.info('[{}/{}]'.format(i + 1, len(links)))
    self.logger.info(link)

    try:

will be

except NoSuchElementException:
    self.logger.error('Too few images, skipping this tag')
    continue

zx = 0       #1st modified line

for i, link in enumerate(links):
    self.logger.info('[{}/{}]'.format(i + 1, len(links)))
    self.logger.info(link)

    zx += 1       #2nd modified line
    if zx > 2:       #3rd modified line
        break        #4th modified line

    try:

_Now it will get the desired amount of links from your tags and will only visit 2 of them (cos breaks after zx>2) and then will move to the next tag and find again desired amount,..repeatedly..._

this skips the time you will wait until the tag finishes visiting e.g. 200 pics (having amount=200) and visits only 2 of them (cos breaks after zx>2..)

after finishing tests, just remove all of those 4 modified breakpoint lines

@uluQulu

1) it happens for all tags after the first tag
2) it happens everytime

@uluQulu

I inserted the links as you have suggested for the break point test..

Surprisingly, for 5 tags that I tested, each returned 200

Difficult to understand

@uluQulu

My test reveals that using the same set of tags..

Case 1:
If I use your break point test, the bot returns the full amount of 200 specified for the series of tags

Case2:
The bot only returns 200 for the first tag in the same set, and the rest 12

So could there be a time interval issue?

If I add codes to reset to bot to make it act like the first tag everytime, will that solve the issue?

@uluQulu Sorry! On the go these days. Break point inserted, will report in the morning!
PS. 200 for 3 tags with the break!

PSS. Any magical chance you could look at similar error (not enough users to interact with, skipping to next profile) - when trying to interact with someone else followers? :)

@Tachenz @clevebounce
after the last update, there is no pre-defined number (_like we had 12 and 21 before_) in the scroller and then IG's always returning number 12 is somehow mystic (12 pics = 3x4 = 4 rows of picutres)
basically, first of all you can exceed the amount of overall requests to the server or amount of scroll requests or amount of like requests..
but the funny is even after you would exceed that requests, it returns 4 rows again
here can be one thing, it changes page element style and you get default 4 rows (3 row of top posts & 1 recent posts) and it stops scrolling
unless we get some robust understanding of its concrete cause, it will fail to find a reliable solution
for now, you can make it sleep after some amount of requests but i again saying it is not an effective solution
or you can use QS to put actions to sleep after peaks, this will just ease the process above, but not again fully solves returning correct amount of links...

_@Tachenz I wish I could look, have you posted a new issue for it?_

along the way, if you (who have been affected by the latest problem) could provide some feedback about the cause of it, your ideas, we can find a solution faster

@uluQulu Thank you for keeping at it!
The second run with the break you've introduced above - went differently. Again 12/12 images for tags 2 and 3. It also shows this error before doing 12/12:

  • Insufficient amount of links ` trying again 1
  • Insufficient amount of links ` trying again 2
  • Insufficient amount of links ` trying again 3

This second issue. Created one here - https://github.com/timgrossmann/InstaPy/issues/1493 (probably has to do with scrolling as well, but of course different).
Btw - I need to say that your solutions are a cool masterclass in python debugging :P Need to make a course out of it!

wow......you are inspiring! :P thank you @Tachenz _but you guys made it happen! keep up feedbacking_ :-)

Relating the max 12 or 21 links (_literally, unable to scroll from start_) problem to the activity frequency which is possibly affected by latest updates, I've added some new lines to bypass that limitage

Can you try this? @Tachenz @clevebounce

  1. add new variable put_sleep after nap=1.5 line
nap = 1.5
put_sleep = 0
  1. find and modify if try_again > 2 statement like this
    you will replace
if try_again > 2:   #you can try again as much as you want by changing this number
    logger.info("\n'{}' tag POSSIBLY has less images than desired...".format(tag[1:] if tag[:1] == '#' else tag))
    break

with

if try_again > 2:   #you can try again as much as you want by changing this number
    if put_sleep < 1:
        if filtered_links <= 21 :
            logger.info("Cor! Did you send too many requests? ~ let's rest some")
            sleep(600)
            put_sleep += 1
            browser.execute_script("location.reload()")
            try_again = 0
            sleep(10)
    else:
        logger.info("'{}' tag POSSIBLY has less images than desired...".format(tag[1:] if tag[:1] == '#' else tag))
        break

I've updated the affected gist I shared before, you may get the updated source code from it, too

_since I did guess this with blind debugging cos of that problem has yet not occured in my accaunts, it may not be an effective solution_

This change will put the program to sleep 600 seconds if it cannot scroll (_once it gets only maximum of 12 or 21 links, less than desired amount_) and then reload page and try again
_And if_ it works, we may decrease the sleep to _e.g._ 427 seconds or below (_must be tested_)

@uluQulu

This is this the result after making the above changes:

INFO [2018-02-26 04:09:05] [cleve] Tag [2/5]
INFO [2018-02-26 04:09:05] [cleve] --> b'escape'
INFO [2018-02-26 04:09:13] [cleve] desired amount: 200 | top posts [disabled]: 9 | possible posts: 3765800
INFO [2018-02-26 04:09:27] [cleve] Insufficient amount of links ~ trying again: 1
INFO [2018-02-26 04:09:41] [cleve] Insufficient amount of links ~ trying again: 2
INFO [2018-02-26 04:10:06] [chan.cleve] Insufficient amount of links ~ trying again: 3
INFO [2018-02-26 04:10:09] [chan.cleve] Cor! Did you send too many requests? ~ let's rest some
INFO [2018-02-26 04:20:26] [chan.cleve] Session ended - 2018-02-26 04:20:26
INFO [2018-02-26 04:20:26] [chan.cleve] --------------------

Traceback (most recent call last):
File "quickstart.py", line 32, in
session.like_by_tags(['wanderlust', 'escape', 'love, 'instagram', 'instadaily'], amount=200)
File "/Users/User/Downloads/InstaPy-master/instapy/instapy.py", line 812, in like_by_tags
skip_top_posts)
File "/Users/User/Downloads/InstaPy-master/instapy/like_util.py", line 227, in get_links_for_tag
links.extend(get_links(browser, tag, logger, media, main_elem))
File "/Users/User/Downloads/InstaPy-master/instapy/like_util.py", line 605, in get_links
link_elems = element.find_elements_by_tag_name('a')
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 240, in find_elements_by_tag_name
return self.find_elements(by=By.TAG_NAME, value=name)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 535, in find_elements
{"using": by, "value": value})['value']
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=64.0.3282.167)
(Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64)

@clevebounce
it happened cos reloading page did not finish in 3 seconds at your side and once page is not fully reloaded, it cannot find page elements
increase timeout after reload a bit

browser.execute_script("location.reload()")
try_again = 0
sleep(3)   #modify this lines

will be

browser.execute_script("location.reload()")
try_again = 0
sleep(10)   #or can put 15

@uluQulu
changed sleep to 15 as you have suggested. Here is the result:

INFO [2018-02-28 04:47:41] [cleve] Tag [2/8]
INFO [2018-02-28 04:47:41] [cleve] --> b'vacationtime'
INFO [2018-02-28 04:47:50] [cleve] desired amount: 200 | top posts [disabled]: 9 | possible posts: 3514214
INFO [2018-02-28 04:48:02] [cleve] Insufficient amount of links ~ trying again: 1
INFO [2018-02-28 04:48:20] [cleve] Insufficient amount of links ~ trying again: 2
INFO [2018-02-28 04:48:45] [cleve] Insufficient amount of links ~ trying again: 3
INFO [2018-02-28 04:48:48] [cleve] Cor! Did you send too many requests? ~ let's rest some
INFO [2018-02-28 05:00:11] [chan.cleve] Session ended - 2018-02-28 05:00:11
INFO [2018-02-28 05:00:11] [chan.cleve] --------------------

Is there anyway to settle for a lower amount if the link is insufficient or skip tag completely instead of bot stopping completely?

Apologies gentlemen, I've completely dropped out from the topic, travels-travels. Trying the new version now!

I have noticed the same issue recently. (seems a little bit too late) I'm glad a lots of work has been done by @uluQulu , cheerfully to see everybody is some involved in this process. I will be more than happy to help open a PR for this fix whenever the newest solution comes up.

hi @clevebounce
CONSIDERING that it did terminate the program without any error in the console output, I've put entire while statement into try/except block to catch that error and raise it as happened and _updated the gist I've shared above with the new change._
Please, examine it and output the traceback it will return so that we understand what terminates the program to solve it โ˜บ
OR IF it did already output any error in the console, post that full traceback cos it is the only way to find out solutions

hi @uluQulu Thank again

here is the result:

File "/Users/User/Downloads/InstaPy-master/instapy/instapy.py", line 831, in like_by_tags
self.logger)
File "/Users/User/Downloads/InstaPy-master/instapy/like_util.py", line 507, in check_link
browser.get(link)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 323, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
(Session info: chrome=64.0.3282.186)
(Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.13.3 x86_64)

try:
inappropriate, user_name, is_video, reason = (
check_link(self.browser,
link,
self.dont_like,
self.ignore_if_contains,
self.ignore_users,
self.username,
self.like_by_followers_upper_limit,
self.like_by_followers_lower_limit,
self.logger)

line 831 is the self.logger line

error happens in the middle of running the first tag

@clevebounce
Alright
The latest error occured inside check_link() function which has nothing to do with our modifications, _it happens sometimes, the TimeoutException, when selenium cannot navigate to a page..._

Can you try again to catch that error which terminated program in the 2nd tag?

Try running a few more times, if it still persists on the same TimeoutException, let me know to handle it, too

Hello,
I applied this procedure but I get this error:
cannot import name formatNumber
this happens on a CentOS 7 server and Python 2.7.5

Any idea?

hi @riccardorighettiphoto
I've just checked, there is a fresh commit (#1571) that renamed formatNumber to format_number
You and all others who used this fix must apply this change by hand

I've also updated the post above with this little change

@uluQulu
Thanks a lot! You help and support is always outstanding!

Bye.

I can only second to that!! :)

On Sat, Mar 10, 2018 at 7:41 PM, Riccardo notifications@github.com wrote:

@uluQulu https://github.com/uluqulu
Thanks a lot! You help and support is always outstanding!

Bye.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/timgrossmann/InstaPy/issues/1365#issuecomment-372059491,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AiZwPhAdsrcBTnhB_jI56utoI5BV3ADqks5tdCyEgaJpZM4R6bID
.

agree :)

How come this scrollfix hasn't been added to the lastest version for weeks? :( After every update, need to edit the file to fix the 27 likes.

@GitNees Yes I have the same problem here. What's the fix? has anyone opened the PR to merge for this work?

I am also having the same issue.

@timgrossmann please merge this in the master branch.

@timgrossmann yes please merge this in the master branch

Hi @Gitnees
A few guys previously also asked for a PR
I have to say that these days IG is updating its content scope design and it is applied to some users while others are not affected soon
That's why I did not recommend to merge it with main before ends up with a stable solution
What is best? -_post problems in details as soon as happens so that we find the solution_

I remember there is a live problem that has been reported by @clevebounce (StaleElementReferenceException) and yet I didn't get positive feedback about it's state

Once there are no problems, you should think about PRโ˜บ
_EDIT: TimeoutException is not the issue, I just wanted to say timeout for StaleElementReferenceException_

Guys, finally I think the new changes affected me yesterday not sure. on my test account suddenly I was logged out and apparently my password was changed but this wasn't true. Probably something new? and Instapy was running apparently without being logged? that was weird.

Regarding to the browsing and loading images I fixed the 27 images using the new instapy files from the github but I replaced the like_util.py with all the previous fixes from @uluQulu and everything is working again. So i didn't have any issues with the (TimeoutException) error.

it doesn't crash at the beginning. But i'm still noticing different changes, should we wait a few days?

@Luisetty I knew exactly what was happend to you on this part

Guys, finally I think the new changes affected me yesterday not sure. on my test account suddenly I was logged out and apparently my password was changed but this wasn't true. Probably something new? and Instapy was running apparently without being logged? that was weird.

It because your cookie jus expired, based off my two acocunt, mine expired two days ago. InstaPy seems like didn't realize that was the case,it pretending running fine. but in retrospective, it is not running at all. because the cookie is expired, in this case, the quick fix is to delete your old cookie and then it will re-generate a new one.

Long term solution will be automatically re-create cookie every three months.

Hi @Luisetty
It's another issue and I think @CharlesCCC is right
For this problem, please be specific: did it work fine before applyig the fix, too?

Also did you ever encounter that StaleElementReferenceException in the same manner as @clevebounce stated above?
Cos it is possible that that change may not have yet hit your accaunt(s)

Who has had or is having StaleElementReferenceException, try out the latest updated files (in the same gist) and please feedback any result, without feedback it is literally not possible to solve issues

Yes it is best to wait, at least, this problem cleared out before the move๐Ÿ’ช
I should have cleared out again, our current problem is StaleElementReferenceException and it should be fixed after I edited last time, the other TimeoutException is not an issue of this fix
Keep up feedbacking, we will solve thisโ˜บ

Traceback (most recent call last):
File "start.py", line 19, in
session.login()
File "/home/instapy/instapy.py", line 271, in login
self.followed_by = log_follower_num(self.browser, self.username, self.logfol der)
File "/home/instapy/print_log_writer.py", line 11, in log_follower_num
"return window._sharedData.""entry_data.ProfilePage[0]."
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriv er.py", line 627, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriv er.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorha ndler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot re ad property 'followed_by' of undefined
(Session info: headless chrome=64.0.3282.167)
(Driver info: chromedriver=2.34.522913 (36222509aa6e819815938cbf2709b484973553 7c),platform=Linux 4.9.0-5-amd64 x86_64)

@uluQulu My test seems to be returning the same results as before. However, as I am travelling for a while now, I have not really had the time and mind to test it more comprehensively.

@xxxZedxxx make sure you get the latest version of the Instapy, and for future reference, please read other issue first, there already have tons of people report the same issue as you do, which caused a lots of duplicated issues. the issue is resolved already.

@clevebounce it seems you have not done stuff in this post๐Ÿ‘† (also this ๐Ÿ‘ˆ)
Basically, for this situation it doesn't need a comprehensive test, please reread those comments and post the full traceback for troubleshooting๐Ÿ‘

@xxxZedxxx it has been solved, a few weeks back, I have posted a way to handle the entry_data error you are having, in #1338
_also, that problem has nothing to do with this issue, it's just a temporary outage error from FB servers..._

EDIT: I've just checked latest commits and see that @CharlesCCC has posted a graphql approach to solve entry_data error and it is merged with main repo. At this point, @xxxZedxxx pulling the latest commits should solve the problem for youโ˜บ (_wonder @CharlesCCC, how stable is the graphql approach?_)
BTW @CharlesCCC congrats for being a collaborator๐ŸŽ‰

I can't believe this still isn't merged into the main code.

@uluQulu it fairly stable. (maybe more stable than before?) I really can't tell the comparsion, but haven't notice any issue or downside yet. It really Instagram.com changed their API to graphql, they kind forced us to whatever their provided.

This is My experience with the above fixes:
Works really well with 3 different account. Chromedriver, ubuntu 16.04 Server. Got different scripts, I am not using multiple tags in one python script. I have different scripts, each containing one tag with like amount of 800. Works great. Is there anyone who is still having trouble with that?

@FranklinClinton I use Instapy on my raspberry with firefox and geckodriver. Instypy like only 27 Images.

@bjo3rns did you update your code with uloQulu's code?
Why isnt that fix pulled into main version?
I also wrote my own way fixing this and I could push it up too, but would rather if uluQulu did his.

when does this error get fixed officially??

Hi @bjo3rns if you have modified correctly you would not get 27 links, some other error but not it. Try to apply the fix correctly๐Ÿ™Œ

Alright guys, as I said in recent posts, we've got one live issue but do you think we should really PR this in this state?

Note that that issue is not diagnosed well and did happen only to @clevebounce who has reported lots of good feedback that's why I wait for clearance.

@uluQulu I have to agree with you. I will have to check everything again to ensure I did not miss any coding.
I am for PR this one so that everyone can try

@uluQulu is it possible you can share with me a complete folder your changes and the latest updates so that I could try again?

I am still travelling at the moment and have no time to sit down and run thru stuff thoroughly now.

@clevebounce
I have no any up-to-date folder cos I do not use InstaPy or any other automation software anymore and do rarely visit IG
There are no any changes in the fix after you reported an error where I added some lines. But you can send default (original) file (like_util.py) and I will modify for you

@bjo3rns chill ๐Ÿ“Ž!

@uluQulu thank you.

@uluQulu Thanks for your fix. When are you going to make a PR so the problem is solved for everyone?

welcome @lucanello, we've got one live issue I have heard from 2 ppl, if it really is an issue (off new update), almost every user will be affected by it after some time regarding the IG's beta update policy that's why it must be solved before pushed to masses but without a feedback it seems impossible,.

Got another error. Looked around and could not find it anywhere else
(if it is, im sorry... going on very little sleep!) ย  ย ย ย 
All was working well but, then...

Traceback (most recent call last):
File "quickstart.py", line 30, in
session.like_by_tags(['ourcamplife','camping'], amount=100)
File "/Users/a /Py/instapy/instapy.py", line 855, in like_by_tags
skip_top_posts)
File "/Users/a /Py/instapy/like_util.py", line 227, in get_links_for_tag
links.extend(get_links(browser, tag, logger, media, main_elem))
File "/Users/a /Py/instapy/like_util.py", line 601, in get_links
link_elems = element.find_elements_by_tag_name('a')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 240, in find_elements_by_tag_name
return self.find_elements(by=By.TAG_NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 535, in find_elements
{"using": by, "value": value})['value']
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85),platform=Mac OS X 10.12.6 x86_64)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "quickstart.py", line 35, in
traceback.print_exc(exc)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 159, in print_exc
print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 100, in print_exception
type(value), value, tb, limit=limit).format(chain=chain):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 497, in __init__
capture_locals=capture_locals)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 332, in extract
if limit >= 0:
TypeError: '>=' not supported between instances of 'StaleElementReferenceException' and 'int'

Everything is running fine for me. The guys with the issues stopped posting I guess?

hi @ohoscar thanks for the great feedback!
_Actually, it is the same error that happened with the 2 other guys and just after reading your report and matching it with @clevebounce's comments I realized the problem was my mistake!_
In the last modifications I added a line to refresh page after big sleep and _in selenium_ when you are doing the explicit refresh, the links object (main_elem in this case) will not longer be valid and will throw the StaleElementReferenceException when referred.

@ohoscar @clevebounce @SkylimiTY
to solve, we will get that element again every time after the refresh by adding this new line:

main_elem = (browser.find_element_by_xpath('//main/article/div[1]') if not link_elems else
                                          browser.find_element_by_xpath('//main/article/div[2]') if skip_top_posts else
                                           browser.find_element_by_tag_name('main'))

I have updated the _gist_ containing the little _change_ above ๐Ÿ‘†

@Tachenz @clevebounce you can try out the fresh _file_ to see if it solves your problem now :)
(_I think it would be the same problem with others, too..._)

Hey @uluQulu, thanks for the quick response..
So, i implemented your fix.. worked pretty well for a while but now, there's another issue.
The script is happy liking away until it reaches a new tag. It finds the posts, but then it spots for a while... as you can see by the tamp stamps.

I did, however, scroll down on another tag manually and it seemed to have helped the interaction and it resumed faster.

Just thought id share

INFO [2018-03-23 14:30:44] [] Tag [12/14]
INFO [2018-03-23 14:30:44] [] --> b''
INFO [2018-03-23 14:30:53] [] desired amount: 100 | top posts [disabled]: 9 | possible posts: 5071166
INFO [2018-03-23 14:31:05] [] Insufficient amount of links ~ trying again: 1
INFO [2018-03-23 14:31:22] [] Insufficient amount of links ~ trying again: 2
INFO [2018-03-23 14:31:48] [] Insufficient amount of links ~ trying again: 3
INFO [2018-03-23 14:31:51] [] Cor! Did you send too many requests? ~ let's rest some
NFO [2018-03-23 14:43:07] [] [1/100]
INFO [2018-03-23 14:43:07] [] https://www.instagram.com/
INFO [2018-03-23 14:43:10] [] Image from: b''
INFO [2018-03-23 14:43:14] [] Number of Followers: 110
INFO [2018-03-23 14:43:14] [] Link: b''

thanks @ohoscar for actively reporting
There was a problem with the activity frequency which was possibly affected by latest IG updates, where I added a breaktime to bypass that limitage (_find the 2 lines below in like_util.py_)

logger.info("Cor! Did you send too many requests? ~ let's rest some")
sleep(600)   #breaktime line

For today 10 minutes (600 seconds) is the max optimal time interval to reset your IG scrollers activity. And it allows around 85~100 unique scrolls per that interval.
For us, reliability is more important than speed, that's why that breaktime is vital.

But you can test with less time intervals than 600 seconds, e.g. just modify that breaktime line to 427 seconds and see if it works _(returning the accurate amount of links is the key)_

logger.info("Cor! Did you send too many requests? ~ let's rest some")
sleep(427)   #modified breaktime from 600 to 427 seconds

Waiting for the results ๐Ÿ˜‹

What did I do wrong?
Log:
INFO [2018-03-24 20:39:39] [username] Session started - 2018-03-24 20:39:39
INFO [2018-03-24 20:40:12] [username] Logged in successfully!
INFO [2018-03-24 20:40:15] [username] Session ended - 2018-03-24 20:40:15
INFO [2018-03-24 20:40:15] [username] --------------------

Quickstart:
from instapy import InstaPy

insta_username = ''
insta_password = ''

session = InstaPy(username=insta_username,
password=insta_password,
headless_browser=True,
nogui=True,
multi_logs=True)
session.login()

session.set_user_interact(amount=3,
randomize=True,
percentage=80,
media='Photo')

session.like_by_feed(amount=20,
randomize=False,
unfollow=False,
interact=False)

session.end()

hi @denisemenov
In this page we have dealt with the issue of get_links_for_tag function which is used only for like_by_tags and follow_by_tags features. And the fix provided here does not affect other features (_such as like_by_feed feature you used in the post above_).

Although I could help you if you would put that quickstart into try/except block and raise the thrown exception, so that we find out the problem, e.g.

from instapy import InstaPy
try:
    un = 'ceylonTea'
    pwd = '555walnutJam'
    session = InstaPy(un, pwd)
    session.login()
    ## some SETtings
    ##some ACTions
except:
    raise
finally:
    session.end()

It will be better to start a new issue cos of being unrelated to the problem in this page

A fresh install of InstaPy gave me the same issue a lot of people are having (bot only liking 27 pictures per hashtag). Applying @uluQulu's fix (https://github.com/timgrossmann/InstaPy/issues/1365#issuecomment-366526517) gave me the selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'followed_by' of undefined error.

A fresh install of InstaPy (deleting chromedrivers & reinstalling them, reinstalling python packages, recloning the project), and then applying @uluQulu's fix again did the trick. Might help for anyone having the same issue.

@Arawasu
You should update chromedriver version. I had the same error last evening.
Bye.

@Arawasu, the error you've seen is from print_log_writer.py, and that problematic line should be solved after #1610 was merged (see also #1338)
I've said lots of times, this fix does not affect ANY other function & feature except the way of grabbing links for like_by_tags and follow_by_tags
Always apply the fix manually, it is very easy in 3 simple steps and avoid out-dated like_util.py files that was shared for some people...

Also, keeping web browser & it's selenium driver up-to-date is always crucial

Hello,
I am running the last version of InstaPy, I don't have commits to pull down (just before you ask me). I am also running chrome and chromedriver at these versions:

(Session info: headless chrome=65.0.3325.181) (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 3.10.0-693.21.1.el7.x86_64 x86_64)

When apply the fix created by @uluQulu (see this comment) I get this error:

Traceback (most recent call last):
  File "quickstart.py", line 39, in <module>
    session.like_by_tags(['motorsportphotography', 'sportphotography', 'racing'], amount=100)
  File "/home/richard99/instapy/instapy/instapy.py", line 890, in like_by_tags
    self.logger)
  File "/home/richard99/instapy/instapy/like_util.py", line 495, in check_link
    "return window._sharedData.entry_data."
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 627, in execute_script
    'args': converted_args})['value']
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'followed_by' of undefined
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 3.10.0-693.21.1.el7.x86_64 x86_64)

The fix was applied in a separate branch, but when I checkout master I don't have any issue and the script run to the end.
Any idea?

Well @riccardorighettiphoto
First of all, apply the fix again (_everybody who uses this fix also should update step 2_) cos I just updated _one statement_ in get_links_for_tag function
And see thread #1338, there is a solution for your problem

"_The fix was applied in a separate branch, but when I checkout master I don't have any issue and the script run to the end._" - Are you sure? Cos this fix has no any conflict with the problem you are getting, the only thing we changed here is the way of grabbing links.

Also, never use any possibly out-dated like_util.py shared for some people, try to apply those simple 3 steps on the up-to-date file manually :)

Am I sure? Sure indeed! I cloned InstaPy on March 28th from scratch, last commit was 4 days ago. So I am up to date. Then applied your fix, this afternoon. I'll try again...

P.s.: Just tried on VPS 1 and even on a second VPS. It does not run on my side. I have to revert the fix.

@riccardorighettiphoto
I just realized why it did not happen with official files would be cos of it does not do so many interactions than ~27 and that's why it does not meet the temporary outage error you are getting...

@uluQulu thank you so much for helping us all out with this.

i implemented your updated solution https://github.com/timgrossmann/InstaPy/issues/1365#issuecomment-366526517

however, i'm getting this error -

Traceback (most recent call last):
File "quickstart.py", line 31, in
session.like_by_tags(['eventstyling','designspiration'], amount=200)
File "/home/yg2dras1/Instapy/instapy/instapy.py", line 987, in like_by_tags
'--> Image not liked: {}'.format(reason.encode('utf-8')))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 49: ordinal not in range(128)
INFO [2018-04-05 02:09:46] [username] Session ended - 2018-04-05 02:09:46
INFO [2018-04-05 02:09:46] [username] --------------------

any ideas how to solve please...

Hi guys!
Can anyone help with modifying def get_links_for_location ? Bot still like only 27 pic...

@uluQulu nevermind, it's now working perfectly! i don't understand why it didn't work last night, but when I ran it today, the code worked. THANK YOU!

Hi guys

thanks @yg2dras1 it's a pleasure to help
I think the caption in some of the user's posts included some characters which python couldn't convert to utf-8, try some more, if it persists with that error, we can find a way to handle it (_also, that issue is not related to this fix_)

sure, it is very easy! @dimahadgi probably, you will use the same fix for get_links_for_tag (_if you can't apply, open a new _Issue_ and mention my username, I will help_)

Thanks!
Created #1825

Hi guys! Long time no activity in here, so I thought everything works properly and just commited a PR including get_links_for_tag function shared in this page at #1904.

Since it affects only like_by_tags feature, if you have the latest revision of that function and still have any problems, please give a feedback to solve it for all!

Cheers!

Sometimes "get links for tag" can not download all links (maybe internet access issue)
So all works perfect, thanks again @uluQulu =)

hi Dmitrity,
Last time I saw your like_util.py file at #1825, you were using OLD (_one of the very earliest_) fix shared in this page for get_links_for_tag function.
Make sure you are using LATEST fix _for that function_ as it is in this gist.

@uluQulu
I have done all the updates and new codes. Not sure why the first tag I get 75 likes from 75 and from there I get 24ish likes only. Thanks againg for all you hard work.

Thanks @uluQulu !!
Just applied your LATEST fix.
Need to test it(maybe 1 week)

Hey guys,
The exact modifications for like_by_tags feature found in this page are already merged with master ๐ŸŽˆ, _you don't need to apply patches manually, any more_
@cbyrt, make sure your like_util.py file (_particularly,_ get_links_for_tag _function_) is identical with the one in master, then if you still have that problem, please post it in a new thread, and mention my username, this page has become so huge :D ~ _I will help_
@dimahadgi, good luck!

Some times I see this:

D:\TEMP\InstaPy-master>set PYTHONIOENCODING=UTF-8

D:\TEMP\InstaPy-master>py quickstart.py

DevTools listening on ws://127.0.0.1:12209/devtools/browser/6e7d9ab8-cd21-488a-bc45-384ae60bd4b3
INFO [2018-05-06 11:50:37] [dkhadzhy]  Session started - 2018-05-06 11:50:37
WARNING [2018-05-06 11:50:37] [dkhadzhy]  Sorry, Record Activity is not working on Windows. We're working to fix this soon!
INFO [2018-05-06 11:51:05] [dkhadzhy]  Logged in successfully!
INFO [2018-05-06 11:51:06] [dkhadzhy]  Location [1/1]
INFO [2018-05-06 11:51:06] [dkhadzhy]  --> b'225219858/dnipropetrovsk-ukraine/'
INFO [2018-05-06 11:51:30] [dkhadzhy]  Insufficient amount of links ~ trying again: 1
INFO [2018-05-06 11:51:47] [dkhadzhy]  Insufficient amount of links ~ trying again: 2
INFO [2018-05-06 11:52:12] [dkhadzhy]  Insufficient amount of links ~ trying again: 3
INFO [2018-05-06 11:52:15] [dkhadzhy]  Cor! Did you send too many requests? ~ let's rest some
INFO [2018-05-06 12:01:25] [dkhadzhy]  Insufficient amount of links ~ trying again: 1
INFO [2018-05-06 12:01:42] [dkhadzhy]  Insufficient amount of links ~ trying again: 2
INFO [2018-05-06 12:02:05] [dkhadzhy]  Insufficient amount of links ~ trying again: 3
INFO [2018-05-06 12:02:08] [dkhadzhy]  '225219858/dnipropetrovsk-ukraine/' location POSSIBLY has less images than desired...
.....................
INFO [2018-05-06 12:07:03] [dkhadzhy]  [24/24]
INFO [2018-05-06 12:07:03] [dkhadzhy]  https://www.instagram.com/p/BibhWR8FibE/?taken-at=225219858
[9888:10364:0506/120703.539:ERROR:latency_info.cc(164)] Display::DrawAndSwap, LatencyInfo vector size 102 is too big.
INFO [2018-05-06 12:07:06] [dkhadzhy]  Image from: b'vitusyka94'
INFO [2018-05-06 12:07:10] [dkhadzhy]  Number of Followers: 108
INFO [2018-05-06 12:07:10] [dkhadzhy]  Link: b'https://www.instagram.com/p/BibhWR8FibE/?taken-at=225219858'
INFO [2018-05-06 12:07:10] [dkhadzhy]  Description: b'No description'
INFO [2018-05-06 12:07:17] [dkhadzhy]  --> Image Liked!
INFO [2018-05-06 12:07:19] [dkhadzhy]  --> Not commented
INFO [2018-05-06 12:07:20] [dkhadzhy]  --> Not following
INFO [2018-05-06 12:07:21] [dkhadzhy]  Liked: 14
INFO [2018-05-06 12:07:21] [dkhadzhy]  Already Liked: 0
INFO [2018-05-06 12:07:21] [dkhadzhy]  Inappropriate: 10
INFO [2018-05-06 12:07:21] [dkhadzhy]  Commented: 0
INFO [2018-05-06 12:07:21] [dkhadzhy]  Followed: 0
[14268:9632:0506/120721.916:ERROR:broker_win.cc(57)] Error reading broker pipe: โ•ฉั€ัั€ั‹ ัโˆšั‹ ั‡ั€ัŠะโˆšะ„. (0x6D)
INFO [2018-05-06 12:07:24] [dkhadzhy]  Session ended - 2018-05-06 12:07:24
INFO [2018-05-06 12:07:24] [dkhadzhy]  --------------------

full_log.txt
like_util.txt
quickstart.txt

That's a great feedback @dimahadgi,
But well, this page has been so huge and is directly related to like_by_tags feature.
Cos of you have used like_by_locations feature in the situation above, let's discuss it in its own thread- #1825.

same here when using Authentication proxy

Unfortunately still got this messages time to time:

INFO [2018-07-23 16:07:41] [dkhadzhy]  desired amount: 600  |  top posts [disabled]: 9  |  possible posts: 466327
INFO [2018-07-23 16:07:54] [dkhadzhy]  Insufficient amount of links ~ trying again: 1
INFO [2018-07-23 16:08:12] [dkhadzhy]  Insufficient amount of links ~ trying again: 2
INFO [2018-07-23 16:08:37] [dkhadzhy]  Insufficient amount of links ~ trying again: 3
INFO [2018-07-23 16:08:41] [dkhadzhy]  Cor! Did you send too many requests? ~ let's rest some
Unable to read VR Path Registry from C:\Users\โ•จะคโ•จโ••โ•จโ•โ•จโ–‘\AppData\Local\openvr\openvrpaths.vrpath
[11756:11056:0723/161758.485:ERROR:latency_info.cc(164)] Display::DrawAndSwap, LatencyInfo vector size 102 is too big.
INFO [2018-07-23 16:18:39] [dkhadzhy]  Insufficient amount of links ~ trying again: 1
INFO [2018-07-23 16:18:56] [dkhadzhy]  Insufficient amount of links ~ trying again: 2
INFO [2018-07-23 16:19:22] [dkhadzhy]  Insufficient amount of links ~ trying again: 3
INFO [2018-07-23 16:19:24] [dkhadzhy]  '749889026/dnipro/' location POSSIBLY has less images than desired...
INFO [2018-07-23 16:19:27] [dkhadzhy]  [1/24]
INFO [2018-07-23 16:19:27] [dkhadzhy]  https://www.instagram.com/p/Blk0SmYH9T7/?taken-at=749889026
WARNING [2018-07-23 16:19:31] [dkhadzhy]  Unavailable Page: b'https://www.instagram.com/p/Blk0SmYH

Just updated to the latest version.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. > If this problem still occurs, please open a new issue

Hey! This issue still remains. I get the same out as the above post. Any solutions for this problem?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seuraltimez picture seuraltimez  ยท  3Comments

ghost picture ghost  ยท  3Comments

CharlesCCC picture CharlesCCC  ยท  3Comments

drcyber975 picture drcyber975  ยท  3Comments

deronsizemore picture deronsizemore  ยท  3Comments