[英]Python Instagram bot API issue or something else?
仍然需要具有API访问权限才能在Instagram上关注/取消关注吗? 我读到该API不支持它。 我尝试了这个selenium机器人,但是似乎没有选择用户名或跟随按钮。我尝试了CSS选择器,xpath,click(),send_keys等所有功能,但是似乎不起作用。 如果这不是API问题,就无法弄清楚问题可能是什么? 我要发布整个代码,因为它对于Insta似乎是一个非常有用的机器人。
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 = 'C:/Users/Myname/Downloads/cd79/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('username')
password = webdriver.find_element_by_name('password')
password.send_keys('password')
password.send_keys(Keys.ENTER)
sleep(3)
notnow = webdriver.find_element_by_xpath("//*[text()='Not Now']")
notnow.click()
hashtag_list = ['xyz','lockdown']
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('20201202-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(5)
try:
for x in range(1, 200):
username = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/header/div[2]/div[1]/div[1]/a').text
sleep(4)
if username not in prev_user_list:
# If we already follow, do not unfollow
if webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/header/div[2]/div[1]/div[2]/button'):
webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/header/div[2]/div[1]/div[2]/button').send_keys(Keys.SPACE)
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.