Skip to content

Instantly share code, notes, and snippets.

@fnneves
Last active July 14, 2024 19:17
Show Gist options
  • Save fnneves/14fd2e05a1e39e67bae5a5ccf50c9975 to your computer and use it in GitHub Desktop.
Save fnneves/14fd2e05a1e39e67bae5a5ccf50c9975 to your computer and use it in GitHub Desktop.
hashtag_list = ['travelblog', 'travelblogger', 'traveler']
# prev_user_list = [] - if it's the first time you run it, use this line and comment the two below
prev_user_list = pd.read_csv('20181203-224633_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
prev_user_list = list(prev_user_list['0'])
new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0
for hashtag in hashtag_list:
tag += 1
webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
sleep(5)
first_thumbnail = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div')
first_thumbnail.click()
sleep(randint(1,2))
try:
for x in range(1,200):
username = webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/header/div[2]/div[1]/div[1]/h2/a').text
if username not in prev_user_list:
# If we already follow, do not unfollow
if webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
new_followed.append(username)
followed += 1
# Liking the picture
button_like = webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/div[2]/section[1]/span[1]/button/span')
button_like.click()
likes += 1
sleep(randint(18,25))
# Comments and tracker
comm_prob = randint(1,10)
print('{}_{}: {}'.format(hashtag, x,comm_prob))
if comm_prob > 7:
comments += 1
webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/div[2]/section[1]/span[2]/button/span').click()
comment_box = webdriver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/article/div[2]/section[3]/div/form/textarea')
if (comm_prob < 7):
comment_box.send_keys('Really cool!')
sleep(1)
elif (comm_prob > 6) and (comm_prob < 9):
comment_box.send_keys('Nice work :)')
sleep(1)
elif comm_prob == 9:
comment_box.send_keys('Nice gallery!!')
sleep(1)
elif comm_prob == 10:
comment_box.send_keys('So cool! :)')
sleep(1)
# Enter to post comment
comment_box.send_keys(Keys.ENTER)
sleep(randint(22,28))
# Next picture
webdriver.find_element_by_link_text('Next').click()
sleep(randint(25,29))
else:
webdriver.find_element_by_link_text('Next').click()
sleep(randint(20,26))
# some hashtag stops refreshing photos (it may happen sometimes), it continues to the next
except:
continue
for n in range(0,len(new_followed)):
prev_user_list.append(new_followed[n])
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_followed_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))
print('Commented {} photos.'.format(comments))
print('Followed {} new people.'.format(followed))
@batisteo
Copy link

Just for the sake of learning, you can use some_list.extend(other_list). So instead of:

for n in range(0,len(new_followed)):
    prev_user_list.append(new_followed[n])

could be replaced by:

prev_user_list.extend(new_followed)

@batisteo
Copy link

And as you said you where using Python 3.7, you can test the joy of F-stings (understand format string):

print(f'{hashtag}_{x}: {comm_prob}')

@hackrx
Copy link

hackrx commented Mar 9, 2020

Hii, your code is not working! I don't know why. I ran accordingly as you explained in the medium article. And the program ran successfully. When I run this program it opened chromeweb driver and then chrome is opened, then it inserts the username and password credentials. After login successfully, not now button is also triggered by program and then by the hashtag list it takes the first hashtag and then opens the first thumbnail, then the problem occurs. It is just skipping all the steps of commenting, liking, and do not follow the user. I have also tried by changing X_path values but it didn't work for me I am putting code here please see it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd

chromedriver_path = 'G:/chromedriver.exe' # Change this to your own chromedriver path!
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
username.send_keys('User_name')
password = webdriver.find_element_by_name('password')
password.send_keys('Your Password')

button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button')
button_login.click()
sleep(3)

notnow = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm')
notnow.click() #comment these last 2 lines out, if you don't get a pop up asking about notifications

hashtag_list = ['crazy','life' ,'lifequotes','dailyquotes','quotesdaily' ,'quotes','motivation','royalenfield','bike']

prev_user_list = []   # - if it's the first time you run it, use this line and comment the two below
#prev_user_list = pd.read_csv('Desktop/20200309-025920_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
#prev_user_list = list(prev_user_list['0'])

new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0

for hashtag in hashtag_list:
    tag += 1
    webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
    sleep(5)
    first_thumbnail = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]')
    # thumbnail select //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]
    # //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[2]/a/div/div[2]
    # complete box of thumbnail //*[@id="react-root"]/section/main/article/div[1]
    
    first_thumbnail.click()
    sleep(randint(1,2))    
    try:        
        for x in range(1,200):
            username = webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a').text
            #user name x_path    /html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a
            
            if username not in prev_user_list:
                # If we already follow, do not unfollow
                if webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
                    #X_path of Follow button /html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button
                    
                    webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
                    
                    new_followed.append(username)
                    followed += 1

                    # Liking the picture
                    button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
                    #Changed above x path
                    #body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button
                    #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button
                    button_like.click()
                    likes += 1
                    sleep(randint(18,25))

                    # Comments and tracker
                    comm_prob = randint(1,10)
                    print('{}_{}: {}'.format(hashtag, x,comm_prob))
                    if comm_prob > 7:
                        comments += 1
                        webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()
                        #Comment X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button
                        comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')
                        # Comment_box X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[3] # /html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea

                        if (comm_prob < 7):
                            comment_box.send_keys('Really cool! :)')
                            sleep(1)
                        elif (comm_prob > 6) and (comm_prob < 9):
                            comment_box.send_keys('Nice work :)')
                            sleep(1)
                        elif comm_prob == 9:
                            comment_box.send_keys('Nice gallery!!')
                            sleep(1)
                        elif comm_prob == 10:
                            comment_box.send_keys('So cool! :)')
                            sleep(1)
                        # Enter to post comment
                        comment_box.send_keys(Keys.ENTER)
                        sleep(randint(22,28))

                # Next picture
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(25,29))
            else:
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(20,26))
    # some hashtag stops refreshing photos (it may happen sometimes), it continues to the next
    except:
        continue

for n in range(0,len(new_followed)):
    prev_user_list.append(new_followed[n])
    
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_followed_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))
print('Commented {} photos.'.format(comments))
print('Followed {} new people.'.format(followed))

Output is here:-
Liked 0 photos.
Commented 0 photos.
Followed 0 new people.

And the file created is also empty!

@wguto
Copy link

wguto commented Apr 26, 2020

same here

@dfk007
Copy link

dfk007 commented May 5, 2020

Hii, your code is not working! I don't know why. I ran accordingly as you explained in the medium article. And the program ran successfully. When I run this program it opened chromeweb driver and then chrome is opened, then it inserts the username and password credentials. After login successfully, not now button is also triggered by program and then by the hashtag list it takes the first hashtag and then opens the first thumbnail, then the problem occurs. It is just skipping all the steps of commenting, liking, and do not follow the user. I have also tried by changing X_path values but it didn't work for me I am putting code here please see it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd

chromedriver_path = 'G:/chromedriver.exe' # Change this to your own chromedriver path!
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
username.send_keys('User_name')
password = webdriver.find_element_by_name('password')
password.send_keys('Your Password')

button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button')
button_login.click()
sleep(3)

notnow = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm')
notnow.click() #comment these last 2 lines out, if you don't get a pop up asking about notifications

hashtag_list = ['crazy','life' ,'lifequotes','dailyquotes','quotesdaily' ,'quotes','motivation','royalenfield','bike']

prev_user_list = []   # - if it's the first time you run it, use this line and comment the two below
#prev_user_list = pd.read_csv('Desktop/20200309-025920_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
#prev_user_list = list(prev_user_list['0'])

new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0

for hashtag in hashtag_list:
    tag += 1
    webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
    sleep(5)
    first_thumbnail = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]')
    # thumbnail select //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]
    # //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[2]/a/div/div[2]
    # complete box of thumbnail //*[@id="react-root"]/section/main/article/div[1]
    
    first_thumbnail.click()
    sleep(randint(1,2))    
    try:        
        for x in range(1,200):
            username = webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a').text
            #user name x_path    /html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a
            
            if username not in prev_user_list:
                # If we already follow, do not unfollow
                if webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
                    #X_path of Follow button /html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button
                    
                    webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
                    
                    new_followed.append(username)
                    followed += 1

                    # Liking the picture
                    button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
                    #Changed above x path
                    #body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button
                    #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button
                    button_like.click()
                    likes += 1
                    sleep(randint(18,25))

                    # Comments and tracker
                    comm_prob = randint(1,10)
                    print('{}_{}: {}'.format(hashtag, x,comm_prob))
                    if comm_prob > 7:
                        comments += 1
                        webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()
                        #Comment X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button
                        comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')
                        # Comment_box X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[3] # /html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea

                        if (comm_prob < 7):
                            comment_box.send_keys('Really cool! :)')
                            sleep(1)
                        elif (comm_prob > 6) and (comm_prob < 9):
                            comment_box.send_keys('Nice work :)')
                            sleep(1)
                        elif comm_prob == 9:
                            comment_box.send_keys('Nice gallery!!')
                            sleep(1)
                        elif comm_prob == 10:
                            comment_box.send_keys('So cool! :)')
                            sleep(1)
                        # Enter to post comment
                        comment_box.send_keys(Keys.ENTER)
                        sleep(randint(22,28))

                # Next picture
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(25,29))
            else:
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(20,26))
    # some hashtag stops refreshing photos (it may happen sometimes), it continues to the next
    except:
        continue

for n in range(0,len(new_followed)):
    prev_user_list.append(new_followed[n])
    
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_followed_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))
print('Commented {} photos.'.format(comments))
print('Followed {} new people.'.format(followed))

Output is here:-
Liked 0 photos.
Commented 0 photos.
Followed 0 new people.

And the file created is also empty!

Do you have pandas installed?

@hackrx
Copy link

hackrx commented May 5, 2020

Hii, your code is not working! I don't know why. I ran accordingly as you explained in the medium article. And the program ran successfully. When I run this program it opened chromeweb driver and then chrome is opened, then it inserts the username and password credentials. After login successfully, not now button is also triggered by program and then by the hashtag list it takes the first hashtag and then opens the first thumbnail, then the problem occurs. It is just skipping all the steps of commenting, liking, and do not follow the user. I have also tried by changing X_path values but it didn't work for me I am putting code here please see it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd

chromedriver_path = 'G:/chromedriver.exe' # Change this to your own chromedriver path!
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
username.send_keys('User_name')
password = webdriver.find_element_by_name('password')
password.send_keys('Your Password')

button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(4) > button')
button_login.click()
sleep(3)

notnow = webdriver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm')
notnow.click() #comment these last 2 lines out, if you don't get a pop up asking about notifications

hashtag_list = ['crazy','life' ,'lifequotes','dailyquotes','quotesdaily' ,'quotes','motivation','royalenfield','bike']

prev_user_list = []   # - if it's the first time you run it, use this line and comment the two below
#prev_user_list = pd.read_csv('Desktop/20200309-025920_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
#prev_user_list = list(prev_user_list['0'])

new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0

for hashtag in hashtag_list:
    tag += 1
    webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
    sleep(5)
    first_thumbnail = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]')
    # thumbnail select //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]
    # //*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[2]/a/div/div[2]
    # complete box of thumbnail //*[@id="react-root"]/section/main/article/div[1]
    
    first_thumbnail.click()
    sleep(randint(1,2))    
    try:        
        for x in range(1,200):
            username = webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a').text
            #user name x_path    /html/body/div[3]/div[2]/div/article/header/div[2]/div[1]/div[1]/a
            
            if username not in prev_user_list:
                # If we already follow, do not unfollow
                if webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
                    #X_path of Follow button /html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button
                    
                    webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
                    
                    new_followed.append(username)
                    followed += 1

                    # Liking the picture
                    button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
                    #Changed above x path
                    #body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button
                    #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button
                    button_like.click()
                    likes += 1
                    sleep(randint(18,25))

                    # Comments and tracker
                    comm_prob = randint(1,10)
                    print('{}_{}: {}'.format(hashtag, x,comm_prob))
                    if comm_prob > 7:
                        comments += 1
                        webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()
                        #Comment X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button #/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button
                        comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')
                        # Comment_box X_path  /html/body/div[4]/div[2]/div/article/div[2]/section[3] # /html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea

                        if (comm_prob < 7):
                            comment_box.send_keys('Really cool! :)')
                            sleep(1)
                        elif (comm_prob > 6) and (comm_prob < 9):
                            comment_box.send_keys('Nice work :)')
                            sleep(1)
                        elif comm_prob == 9:
                            comment_box.send_keys('Nice gallery!!')
                            sleep(1)
                        elif comm_prob == 10:
                            comment_box.send_keys('So cool! :)')
                            sleep(1)
                        # Enter to post comment
                        comment_box.send_keys(Keys.ENTER)
                        sleep(randint(22,28))

                # Next picture
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(25,29))
            else:
                webdriver.find_element_by_link_text('Next').click()
                sleep(randint(20,26))
    # some hashtag stops refreshing photos (it may happen sometimes), it continues to the next
    except:
        continue

for n in range(0,len(new_followed)):
    prev_user_list.append(new_followed[n])
    
updated_user_df = pd.DataFrame(prev_user_list)
updated_user_df.to_csv('{}_users_followed_list.csv'.format(strftime("%Y%m%d-%H%M%S")))
print('Liked {} photos.'.format(likes))
print('Commented {} photos.'.format(comments))
print('Followed {} new people.'.format(followed))

Output is here:-
Liked 0 photos.
Commented 0 photos.
Followed 0 new people.
And the file created is also empty!

Do you have pandas installed?

No, I haven't installed pandas, since it was not mentioned on medium article

@dfk007
Copy link

dfk007 commented May 5, 2020

Do you have pandas installed?

No, I haven't installed pandas, since it was not mentioned on medium article

AND the xpath links have changed as well.

The following xpaths are working for me.
... code block below...

    1. username xpaths
           username = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[1]/a').text
        
            if username not in prev_user_list:
            if webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
                
                webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
                
                
2. Likes xpaths
                # Liking the picture
                button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
                
                

   3. comments xpaths                 webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()

comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')

@hackrx
Copy link

hackrx commented May 5, 2020

I have tried by changing the paths, it run successfully able to logged in with username and password but when it opens a hastag then opens its first post then it do not comment on the post and also don't do like it, simply move forwards on another hashtag, Also I got blocked by Instagram by running this bot, though it was not my official account and I got unblocked after 5 hrs.It is working for you?

@dfk007
Copy link

dfk007 commented May 9, 2020

Without seeing your code, the best I can assume is that like button and comment box paths aren’t found and the program enters the loop again after the sleep time i.e. check if your paths are correct.

For me the only consistent bug was pandas not always appending the csv file. I blame the webdriver.

@murrman95
Copy link

Question, what's the point of

3. comments xpaths webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()?

Also this xpath doesn't work for me. like we're clicking on the comments box, but not the text area?

@dfk007
Copy link

dfk007 commented May 15, 2020

@murrman95
Did you omit this line? The variable comment_box is necessary for the following if-else logic to work. You click on the comment button to get the text field but don’t assign the text box’s xpath to the comment_box variable.

comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')

@dfk007
Copy link

dfk007 commented May 22, 2020

Agreed. The script doesn't handle pandas well. Anyone without a intro to pandas is at a disadvantage. Tho, there is little tweaking to be done for it to work.

@fnneves
Copy link
Author

fnneves commented May 22, 2020

hi great medium article
i got error
prev_user_list = pd.read_csv('20181203-224633_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
NameError: name 'pd' is not defined

i have installed pandas and running on python 3.8.2

Thanks! Are you sure you imported pandas as pd initially? The error is pretty clear, it is saying it does not know what pd is...
Let me know!

@dfk007
Copy link

dfk007 commented May 22, 2020

Imo, every time a new csv should be created with data from old csv fetched and appended to the new file. Alternatively, refine logic so csv output issues are managed.

@fnneves
Copy link
Author

fnneves commented May 22, 2020

Agreed. The script doesn't handle pandas well. Anyone without a intro to pandas is at a disadvantage. Tho, there is little tweaking to be done for it to work.

Hi! Thanks for helping out other people with the pandas issue!
Regarding the xpaths, there's no way around it... Instagram changes the structure of the page and it can brake the code. I found a nice tool to get the css path really easy. Look for a chrome extension called SelectorGadjet.
I don't usually update these files, but I'll find some time to optimize that csv part. Thanks!

@asmirbe
Copy link

asmirbe commented May 22, 2020

hi great medium article
i got error
prev_user_list = pd.read_csv('20181203-224633_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
NameError: name 'pd' is not defined
i have installed pandas and running on python 3.8.2

Thanks! Are you sure you imported pandas as pd initially? The error is pretty clear, it is saying it does not know what pd is...
Let me know!

I haven't even tuched the code i just updated the button dom selector and gived some username and password.
so i have
import pandas as pd
in line 5

okey i tried to test it out so

import pandas as pd
pd.test()
It works.
Now when i re-run your instabot2.py i got
Traceback (most recent call last):
File "instabot2.py", line 4, in
prev_user_list = pd.read_csv('20181203-224633_users_followed_list.csv', delimiter=',').iloc[:,1:2] # useful to build a user log
NameError: name 'pd' is not defined

@a1de93
Copy link

a1de93 commented Apr 14, 2021

Do you have pandas installed?

No, I haven't installed pandas, since it was not mentioned on medium article

AND the xpath links have changed as well.

The following xpaths are working for me.
... code block below...

    1. username xpaths
           username = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[1]/a').text
        
            if username not in prev_user_list:
            if webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').text == 'Follow':
                
                webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
                
                
2. Likes xpaths
                # Liking the picture
                button_like = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
                
                

   3. comments xpaths                 webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[2]/button').click()

comment_box = webdriver.find_element_by_xpath('/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/textarea')

hey i did this as well and it doesn't work. no errors in the terminal. just not liking, following, or commenting. i checked and rechecked the xpaths... any help?

@emilrueh
Copy link

emilrueh commented Oct 7, 2022

Followed your medium post (thanks for that btw!) and made some changes on my own to make it work as of November 22.

Instagram seems to have changed slightly and I feel the csv checking whether the username is followed already is unnecessary now as when we follow the user already the follow button is not present.

Also the next button threw some errors as it kept on switching back and forth between picture 1 and 2 since the next button on the second page is not the same as on the first. Instead the bot just clicks the arrow right key to advance.

The log info while the bot progresses now gives a hint whether it left a comment and the csv saves a new file instead of appending the old to not have such a long document.


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep, strftime
from random import randint
import pandas as pd
import sys
import glob

browser = webdriver.Firefox()

browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(5)
insta_cookies = browser.find_element(By.XPATH, '/html/body/div[4]/div/div/button[1]')
insta_cookies.click()
sleep(4)
username = browser.find_element(By.XPATH,
'/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[1]/div/label/input')
username.send_keys('USERNAME')
sleep(0.5)
password = browser.find_element(By.XPATH,
'/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[2]/div/label/input')
password.send_keys('PASSWORD')
sleep(2)
login_button = browser.find_element(By.XPATH,
'/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[3]/button')
login_button.click()
sleep(8)
info_button = browser.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/section/main/div/div/div/div/button')
info_button.click()
sleep(4)
alert_button = browser.find_element(By.XPATH,
'/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/button[2]')
alert_button.click()

hashtag_list = ['digitalart', 'nftart']

timecode = strftime("%d/%m/%Y_%Hh%Mm%Ss")

path_name = r'C:\Users\USER\PycharmProjects\insta_bot\*.csv'
csv_files = glob.glob(path_name)
last_file = csv_files[-1]
list_count = int(last_file[-6:-4])

user_list = []

new_followed = []
tag = -1
followed = 0
likes = 0
comments = 0

def next_arrow():
    browser.find_element(By.XPATH, '/html').send_keys(Keys.ARROW_RIGHT)

for hashtag in hashtag_list:
    tag += 1
    browser.get('https://www.instagram.com/explore/tags/' + hashtag_list[tag] + '/')
    sleep(20)
    first_image = browser.find_element(By.XPATH,
    '/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/section/main/article/div/div/div/div[1]/div[1]/a/div/div[2]')
    first_image.click()
    sleep(randint(2, 4))
    try:
        for a in range(1, 100):
            username = browser.find_element(By.XPATH,
            '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[1]/div/div/div/span/a').text

            try:
                follow_button = browser.find_element(By.XPATH,
                '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[1]/div/header/div[2]/div[1]/div[2]/button')

                follow_button.click()
                new_followed.append(username)
                followed += 1

                like_button = browser.find_element(By.XPATH,
                              '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[2]/section[1]/span[1]/button')
                like_button.click()
                likes += 1

                sleep(randint(10, 20))

                comm_prob = randint(1, 10)

                if comm_prob >= 5:
                    comment_check = True
                    comment_button = browser.find_element(By.XPATH,
                                     '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[2]/section[1]/span[2]/button')
                    comment_button.click()

                    comment_box = browser.find_element(By.XPATH,
                                  '/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form/textarea')

                    if comm_prob == 5:
                        comment_box.send_keys("Awesome!")
                        sleep(1)
                    if comm_prob == 6:
                        comment_box.send_keys("Nice!")
                        sleep(1)
                    elif comm_prob == 7:
                        comment_box.send_keys("Cool!")
                        sleep(1)
                    elif comm_prob == 8:
                        comment_box.send_keys("Great!")
                        sleep(1)
                    elif comm_prob == 9:
                        comment_box.send_keys("Super!")
                        sleep(1)
                    elif comm_prob == 10:
                        comment_box.send_keys("Fantastic!")
                        sleep(1)

                    comment_box.send_keys(Keys.ENTER)
                    comments += 1
                    sleep(randint(5, 10))
                else:
                    comment_check = False

                if comment_check:
                    comment_proof = '| commented'
                else:
                    comment_proof = ''

                print(f'{a} | #{hashtag} | user: {username} | {strftime("%Hh%Mm%Ss")} {comment_proof}')

                next_arrow()
                sleep(randint(5, 15))

            except Exception:
                next_arrow()
                sleep(randint(10, 15))
                continue

    except Exception as e1:
        print(f"""> activated exception 2 in line: {sys.exc_info()[-1].tb_lineno} | {e1}""")
        next_arrow()
        sleep(randint(5, 10))
        continue

user_list.extend(new_followed)
list_count += 1
users_updated = pd.DataFrame(user_list)
users_updated.to_csv(f'users-followed-list_0{list_count}.csv')

print(f"""
Liked {likes} posts.
Commented on {comments} posts.
Followed {followed} new profiles.
""")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment