使用 Selenium 不使用 API 将图像发布(上传)到 Instagram [英] Posting (Uploading) an image to Instagram using Selenium not using an API

查看:27
本文介绍了使用 Selenium 不使用 API 将图像发布(上传)到 Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我已经尝试解决这个问题有一段时间了.我正在使用 Selenium (chrome webdriver) 从桌面访问 Instagram 的移动版本.我的目标是创建一个脚本来自动将图像发布到帐户.使用 Python,我已经自动化了一切,直到我需要选择文件的部分.

I've been trying to solve this for a while now. I'm using Selenium (chrome webdriver) to access the mobile version of Instagram from the desktop. My goal is to create a script to automate posting an image to an account. Using Python I have automated everything up to the part where I need to select the file.

问题:

单击创建新帖子按钮后,gui 文件选择器将打开,我无法弄清楚如何与其交互以选择文件.

Once I click the create new post button the gui file selector opens and I cannot figure out how to interact with it to select the file.

尝试过:

post_btn.send_keys(r'/Path/To/image.jpg')

post_btn.send_keys(r'/Path/To/image.jpg')

post_btn.send_keys('/Path/To/image.jpg')

post_btn.send_keys('/Path/To/image.jpg')

我认为这个方法的问题是没有输入字段可以将字符串发送到.

The trouble, I think, with this method is there is not input field to send the string to.

我阅读了一些其他关于使用 autoit 和工作的帖子,但我使用的是 Mac.

I read some other posts about using autoit and that working, but I'm on a Mac.

这是帖子按钮所在的跨度:

Here's the span the post button lives in:

<span class="glyphsSpriteNew_post__outline__24__grey_9 u-__7" aria-label="New Post"></span>

问题:

有没有办法使用python通过文件选择器gui打开"(上传)文件?更好的是,我可以绕过打开文件选择器吗?

Is there a way to 'open' (upload) a file through a file selector gui using python? Better, can I bypass opening the file selector at all?

谢谢!

我认为阻止我解决这个问题的原因是我不明白数据是如何发送和接收的.既然它不是 html 输入,这是否意味着它是 JSON?我如何弄清楚数据(图像)是如何发送到下一页的(https://www.instagram.com/create/style/)?

I think what's stopping me from solving this is that I don't understand how the data is being sent and received. Since it's not an html input does that mean it is JSON? How do I figure out how the data (image) is being sent to the next page (https://www.instagram.com/create/style/)?

推荐答案

IDK 如果这对你有用,因为它已经很久了.但是对于任何需要它的人,这里是完整的教程,

IDK if this will be useful to you because it has been so long.But to anyone who needs it here is the full tutorial,

进口:

import os
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import autoit
import time
from selenium.webdriver.common.keys import Keys

所以首先你需要一个模拟器.我发现的最佳方法是使用 chromedriver.exe,如果您使用 chrome 作为浏览器,则您已经需要它.因此,要制作模拟器,您需要为驱动程序编写以下代码:

So First you need a emulator. The best method that I found was using chromedriver.exe which you already need if you are using chrome as the browser. So to make the emulator you need this bit of code for the driver:

mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(chrome_options = chrome_options)

然后您登录:

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

time.sleep(2)
driver.find_element_by_name("username").send_keys("Your Username")
driver.find_element_by_name("password").send_keys("Your Password") 
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button""").click()

time.sleep(2)

driver.get('https://www.instagram.com/' + username)

为您的文件命名(必须是全名):

Name your file (It needs to be full name):

ImagePath = 'Your File Location'

最后一步是这样的:

ActionChains(driver).move_to_element( driver.find_element_by_xpath("""//*[@id="react-root"]/section/nav[2]/div/div/div[2]/div/div/div[3]""")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 3)
autoit.control_set_text(handle, "Edit1", dir_path)
autoit.control_click(handle, "Button1")

time.sleep(2)

driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()

time.sleep(2)

txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('')
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('test') # Descrition
txt.send_keys(Keys.ENTER)

driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()

所有这些都是转到上传页面并使用 autoit 浏览窗口以选择文件并进行选择.然后只需添加说明并分享帖子.

All this does is go the the upload page and use autoit to navigate through windows to select a file and choose. Then just adds a description and shares the post.

这篇关于使用 Selenium 不使用 API 将图像发布(上传)到 Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆