Python Instagram 自动记录器

Posted

技术标签:

【中文标题】Python Instagram 自动记录器【英文标题】:Python Instagram Auto logger 【发布时间】:2019-04-03 12:12:32 【问题描述】:

我只是想创建一个 python API,它会在我的 instagram 页面上自动记录我,但似乎该程序无法识别它必须在其中放置我的电子邮件地址和密码的框。我使用了通过 xpath 查找元素的函数。 我从 Instagram 复制了 xpath,但它不起作用。

from selenium import webdriver
from getpass import getpass
from selenium.webdriver.common.action_chains import ActionChains
chromedriver = "C:\\Users\\Utente\\Desktop\\chromedriver" 
driver = webdriver.Chrome(chromedriver)

usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')

driver.get("https://www.instagram.com")

login_elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click() 


inputs = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]')

ActionChains(driver)\
.move_to_element(inputs[0])\
.click()\
.send_keys(usr)\
.move_to_element(inputs[1])\
.send_keys(psw)\
.perform() 

login_button = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button')
ActionChains(driver)\
.move_to_element(login_button)\
.click()\
.perform()


codio = driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]')
actions = ActionChains(driver)\
.move_to_element(codio)\
.click()\
.send_keys(prfl)\
.perform()

Here the xpath i copied from instagram

Here the error message

【问题讨论】:

你能把代码和错误信息发布为文本吗? 请看***.com/help/how-to-ask! 欢迎来到 SO。您的帖子中没有任何用处。你没有描述问题,你没有解释你尝试了什么不起作用,你没有描述输出应该是什么样子。请阅读帮助部分并学习如何提出一个好的问题。 对不起@dfundako 我以前从未发布过问题。 @Austin 我将在代码中提出另一个问题,希望对您解决我的问题有用 【参考方案1】:

我已经稍微改写了你的代码,它现在可以工作了:

from selenium import webdriver
from getpass import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


chromedriver = "C:\\Users\\Utente\\Desktop\\chromedriver"
driver = webdriver.Chrome(chromedriver)

usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')

driver.get('https://www.instagram.com/accounts/login/')

wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]'))).send_keys(usr)
driver.find_element_by_xpath('//input[@type="password"]').send_keys(psw)
driver.find_element_by_xpath('//button[contains(text(), "Log in")]').click()
wait.until(EC.presence_of_element_located((By.XPATH, '//a[text()="Profile"]')))

我正在使用显式等待来使代码更稳定。

希望这将帮助您了解硒的工作原理。

【讨论】:

当然。做到了:D 另一件事,当我登录我的页面时,有一个弹出窗口询问我是否要接收通知,我复制了拒绝的按钮的 xpath。代码是这样的:wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div/div/div/div[3]/button[2]'))).click ()。但它不起作用,如果可以的话,你能解释一下为什么吗?非常感谢。 浏览器提供的 xpath 不起作用。我正在编写自己的 xpath。试试这个来拒绝通知: //button[contains(text(), "Not Now")] 哦,好的,我明白了,无论如何它都不起作用,但非常感谢您

以上是关于Python Instagram 自动记录器的主要内容,如果未能解决你的问题,请参考以下文章

记一个Selenium自动化测试网页

在 ViewPager 和 Recyclerview 中自动播放视频,如 instagram

您如何自动滚动 Instagram DM?

向下滚动列表 instagram selenium 和 python

当我们上传图像时,是不是有任何反应原生包可以像 instagram 一样自动裁剪图像?

Selenium 单击instagram Python上的所有关注按钮